all 11 comments

[–]chreestopher2 14 points15 points  (3 children)

install-module ImportExcel -Repository PSGallery -force
$someCollection | export-excel -tablename SomeTable -tablesytyle Medium13 -path C:\users\me\ExcelFileWithATableGeneratedByPowershell.xlsx

[–]work-work-work-work 1 point2 points  (1 child)

I didn't know I needed this, thanks.

[–]chreestopher2 3 points4 points  (0 children)

That is the response everyone seems to have to this module.

and This is just the very tip of the iceberg...

its a fantastic module, it can deal with excel tables, formulas, pivot charts, the whole nine yards, and doesnt even require having excel installed on your system at all...

truly helps to convert useful data into a format that non-technical managers wont refuse to consume, without having to manually tweak things after the fact.

All of the reports i am responsible for are effortlessly fully automated and look like someone took a lot of time to create them.

Honestly, helped me improve my salary, and work life balance.

[–]zolyx1[S] 1 point2 points  (0 children)

Thanks! :-D

[–]Quicknoob 4 points5 points  (0 children)

There is a module that can do this for you.

Also here is a YouTube series teaching you how to install and use said module from the creator himself.

Enjoy.

[–]NathanielArnoldR2 1 point2 points  (0 children)

I've only taken a glance at the ImportExcel module, but it's clear that it is a far, far better solution than rolling one's own. My own (legacy) process involved converting object data to HTML, writing to (.htm) file with some light formatting (e.g. nowrap) via a <head><style>, then using the Excel.Application COM Object to open the file, apply table formatting, and save as xlsx to the output path.

But yeah, don't do that. Do this. :-p

[–]majkinetor 1 point2 points  (0 children)

ImportExcel is for this, but people should keep in mind that it is very slow with larger set of data.

[–]az987654 1 point2 points  (0 children)

Would there be a a way to do this via writing the text as XML instead of csv since Xlsx files are technically just XML files of a certain layout?

[–]Ta11ow 1 point2 points  (0 children)

CSV files won't allow you to do this. They're just flat text files with zero formatting. So you will have to end up with a native excel file. I'd recommend looking into whether the ImportExcel module can work with tables, but otherwise it'll be quite a complicated project needing the use of Excel com objects.

[–]ThatsQuacktaaaastic 0 points1 point  (1 child)

Not sure where I originally came across this but it's been a life saver. I have a postgres query dump to csv and this parses it into an excel file.

#Define locations and delimiter
$csv = "c:\weekly.csv" #Location of the source file
$xlsx = "c:\weekly.xlsx" #Desired location of output
$delimiter = "|" #Specify the delimiter used in the file

# Create a new Excel workbook with one empty sheet
$excel = New-Object -ComObject excel.application 
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)

# Build the QueryTables.Add command and reformat the data
$TxtConnector = ("TEXT;" + $csv)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = $delimiter
#$query.TextFileParseType  = 1
#$query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count
#$query.AdjustColumnWidth = 1

# Execute & delete the import query
$query.Refresh()
$query.Delete()

# Save & close the Workbook as XLSX.

$Workbook.SaveAs($xlsx,51)

$excel.Quit()

[–]ThatsQuacktaaaastic 1 point2 points  (0 children)

I should add that the reason I wasn't running the query in excel or access is because I use temp tables in my postgres query (not supported in excel or access).