-
Notifications
You must be signed in to change notification settings - Fork 404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Send-SqlDataToExcel.ps1 #555
Comments
I think this cannot be done in excel, can you do it in Microsoft Excel? |
Thanks. I'm creating too many excel files using the importexcel module. So i need to do this work with using the script. I tried -TableName but it doesn't work. |
Sorry I wasn't clear, I didn't mean you do it manually I meant that as far as I know this cannot be done even in Microsoft Excel or any other Excel client as this is not supported by the xlsx format. |
TableNameI didn't look to see if/how One challenge with tables is, as you filter them the other tables that are close will also change size because Excel adjusts the visible rows . $xlfile="$env:TEMP\testTable.xlsx"
rm $xlfile -ErrorAction SilentlyContinue
$d1=convertfrom-csv @"
Id,Item,Sold
1,Apple,100
2,Pear,200
3,Grape,150
"@
$d2=convertfrom-csv @"
Id,Region,Item,Sold
1,East,Nails,100
2,North,Hammer,200
3,South,Bolts,150
4,East,Nails,100
5,North,Hammer,200
6,South,Bolts,150
"@
$d2 | Export-Excel $xlfile -TableName Hardware -StartColumn 5
$d1 | Export-Excel $xlfile -TableName Fruit -show |
Tested it now $newFilename = "C:\Users\illym\Desktop\Temp\Test.xlsx"
$Date = Get-Date
$Time = [TimeSpan]::FromHours(16)
$DataTable = [Data.DataTable]::new('Test')
$null = $DataTable.Columns.Add('IDD', [Int32])
$null = $DataTable.Columns.Add('Name')
$null = $DataTable.Columns.Add('Date', [DateTime])
$null = $DataTable.Columns.Add('Time', [TimeSpan])
$DataTable.Rows.Add(8, 'Test', $Date, $Time)
Send-SQLDataToExcel -DataTable $DataTable -Path $newFilename -WorkSheetname "F" -AutoSize -TableName bbb -StartRow 6 -StartColumn 6
Send-SQLDataToExcel -DataTable $DataTable -Path $newFilename -WorkSheetname "F" -AutoSize -TableName AAA -StartRow 6 -StartColumn 1
But this problem dose not exist in my fork https://github.com/ili101/ImportExcel as Regarding filtering neighboring Tables yes it can be confusing, this is probably why they limited |
@ili101 I would be surprised if it "knows exactly what to apply the format on", because the data is written to sheet, and then export-Excel is called, and because that call is applying tables etc without adding data first it applies things to the whole sheet. This will break things. |
I've moved the creation of ranges and tables into Send-SQLDataToExcel , and it seems to work better. Hopefully @dfinke will include this in the next merge. |
@dfinke Can you approve my pull request please? #298 |
@jhoneill Thoughts? |
@ili101 the calls to Add-ExcelTable And Add-ExcelName have moved into Send-SQLData … very little duplication, just moved where the range is determined. That was the idea behind making them separate functions ... Sorry , I'm grumpy and need to get to some sleep. That probably reads as being over-critical of someone elses work, and there are bits I haven't examined at all. But those are my thoughts for the moment. |
@jhoneill All the changes and parameters are documented here https://github.com/ili101/ImportExcel/blob/master/CHANGELOG.md and I added some examples here https://github.com/ili101/ImportExcel/blob/master/Examples/DataTable/DataTableAndAutoSizePerformance.ps1 (And there are the tests add). If you think some more or a different type of explanation is needed I can add it. (I will add the ".PARAMETER" parts I forgot) I already updated the code from master twice (As I use it in my environment) which is why I appreciate it if it will get merged so I wouldn't need to do it any more. If there is an option to get it merged I will happily update it again.
I totally agree the changes do not replace
I'm not sure what are you referring to in here, the correct version of piping more than one datatable. Do we want to support this? What will we want it to do in this case? Looking foreword to your feedback, so I will know what to change. Edit: Regarding what I said on duplicate code, I was wrong regarding Add-ExcelTable, I didn't noticed you used it. but there are other parts from
|
The solutions. Overnight I remembered something about the behaviour of a datatable with the pipeline. You can't pipe a whole table - when you try you get its rows. You can't return a datatable via the Pipeline either (you get rows), I remember changing Get-SQL so it could put the table into a variable to work round this. So that question goes away ... I think. There's another issue with Export-Excel and I keep avoiding it. It has a targetData parameter where other commands would have input object which can be multiple rows. You can't do The styles thing. Excel has some built in styles which are referred to by number. If you choose short date format it will change how the date is displayed when the localization changes. If you tell EPPlus to use 'm/d/yy h:mm' it is converted to "Format 22" which will always be localized. So on my machine setting what looks like US format (least significant in the middle) displays in European (least significant first). And if I change the culture of my machine to japanese the date be displayed as least significant last. I still haven't worked my way through what happens with the combinations of -table -tableName and |
@jhoneill Thank you, you should read my notes to save you some time https://github.com/ili101/ImportExcel/blob/master/CHANGELOG.md $newFilename = "C:\Users\illym\Desktop\Temp\Test.xlsx"
function Get-Data
{
param (
)
$Date = Get-Date
$Time = [TimeSpan]::FromHours(16)
$DataTable = [Data.DataTable]::new('Test')
$null = $DataTable.Columns.Add('IDD', [Int32])
$null = $DataTable.Columns.Add('Name')
$null = $DataTable.Columns.Add('Date', [DateTime])
$null = $DataTable.Columns.Add('Time', [TimeSpan])
$null = $DataTable.Rows.Add(8, 'Test', $Date, $Time)
$null = $DataTable.Rows.Add(9, 'Test2', $Date, $Time)
, $DataTable
}
$Data = Get-Data
# This will not work correctly, it will throw you the warning I made.
$Data | Export-Excel -Path $newFilename
# This 3 works.
Export-Excel -Path $newFilename -TargetData $Data
, $Data | Export-Excel -Path $newFilename
Get-Data | Export-Excel -Path $newFilename Regarding the non DataTable problem you mention |
Hi,
I want to add multiple tables to a single excel sheet and then, filter and edit the format. But there is a problem in the filtering stage. Only the last table is being filtered.
Thank you for your help in this matter.
Send-SQLDataToExcel -MSSQLSERVER -Connection $serverName -SQL $query16first -Database $DBNameSL -Path $newFilename -WorkSheetname "F" -AutoSize -AutoFilter -StartRow 6 -StartColumn 1
Send-SQLDataToExcel -MSSQLSERVER -Connection $serverName -SQL $query16second -Database $DBNameSL -Path $newFilename -WorkSheetname "F" -AutoSize -AutoFilter -StartRow 6 -StartColumn 6
Send-SQLDataToExcel -MSSQLSERVER -Connection $serverName -SQL $query16third -Database $DBNameSL -Path $newFilename -WorkSheetname "F" -AutoSize -AutoFilter -StartRow 6 -StartColumn 11
Send-SQLDataToExcel -MSSQLSERVER -Connection $serverName -SQL $query16fourth -Database $DBNameSL -Path $newFilename -WorkSheetname "F" -AutoSize -AutoFilter -StartRow 6 -StartColumn 16
Send-SQLDataToExcel -MSSQLSERVER -Connection $serverName -SQL $query16fifth -Database $DBNameSL -Path $newFilename -WorkSheetname "F" -AutoSize -AutoFilter -StartRow 6 -StartColumn 21
The text was updated successfully, but these errors were encountered: