I'm trying to export a complete CSV to Excel by using Powershell. I stuck at a point where static column names are used. But this doesn't work if my CSV has generic unknown header names.
Steps to reproduce
Open your PowerShell ISE and copy & paste the following standalone code. Run it with F5
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell_ise.exe"
Get-Process | Export-Csv -Path $env:temp\process.csv -NoTypeInformation
$processes = Import-Csv -Path $env:temp\process.csv
$Excel = New-Object -ComObject excel.application
$workbook = $Excel.workbooks.add()
$i = 1
foreach($process in $processes)
{
$excel.cells.item($i,1) = $process.name
$excel.cells.item($i,2) = $process.vm
$i++
}
Remove-Item $env:temp\process.csv
$Excel.visible = $true
What it does
- The script will export a list of all active processes as a CSV to your temp folder. This file is only for our example. It could be any CSV with any data
- It reads in the newly created CSV and saves it under the
$processes
variable - It creates a new and empty Excel workbook where we can write data
- It iterates through all rows (?) and writes all values from the
name
andvm
column to Excel
My questions
- What if I don't know the column headers? (In our example
name
andvm
). How do I address values where I don't know their header names? - How do I count how many columns a CSV has? (after reading it with
Import-Csv
)
I just want to write an entire CSV to Excel with Powershell
This is a slight variation that worked better for me.
Why would you bother? Load your CSV into Excel like this:
You just need to make sure that the CSV export uses the delimiter defined in your regional settings. Override with
-Delimiter
if need be.Edit: A more general solution that should preserve the values from the CSV as plain text. Code for iterating over the CSV columns taken from here.
I had some problem getting the other examples to work.
EPPlus and other libraries produces OpenDocument Xml format, which is not the same as you get when you save from Excel as xlsx.
macks example with open CSV and just re-saving didn't work, I never managed to get the ',' delimiter to be used correctly.
Ansgar Wiechers example has some slight error which I found the answer for in the commencts.
Anyway, this is a complete working example. Save this in a File CsvToExcel.ps1
Execute with:
If you want to convert CSV to Excel without Excel being installed, you can use the great .NET library EPPlus (under LGPL license) to create and modify Excel Sheets and also convert CSV to Excel really fast!
Preparation
$HOME\Documents\WindowsPowerShell\Modules\EPPlus
)[Reflection.Assembly]::UnsafeLoadFrom($DLLPath) | Out-Null
Detailed Powershell Commands to import CSV to Excel
This topic really helped me, so I'd like to share my improvements. All credits go to the nixda, this is based on his answer.
For those who need to convert multiple csv's in a folder, just modify the directory. Outputfilenames will be identical to input, just with another extension.
Take care of the cleanup in the end, if you like to keep the original csv's you might not want to remove these.
Can be easily modifed to save the xlsx in another directory.
Ups, I entirely forgot this question. In the meantime I got a solution.
This Powershell script converts a CSV to XLSX in the background
Gimmicks are
=B1+B2
or0000001
.You don't see
#Name
or anything like that. No autoformating is done.PowerShell Code