Excel – How to open CSV in Excel using Powershell while supplying a list seperator

csvexcelpowershell

Hi I'm using a simple Powershell script to convert CSV files to XLSX files. However Excel ignores the list seperator and puts all data in the first column.

  • The list seperator is configured correctly (Start > Control Panel > Regional and Language Options -> Additional Settings)
  • Manually opening the files from Windows Explorer works fine.

However, when opening the CSV in Excel using:

Function Convert-toExcel {
   $xl = new-object -comobject excel.application
   $xl.visible = $true
   $Workbook = $xl.workbooks.OpenText("$csvfile")
   $Worksheets = $Workbooks.worksheets
}

Everything is put into the first column…

Accoriding to Powershell the list seperator is configured correctly:

(Get-Culture).textinfo
ListSeparator  : ,

Best Answer

Try adding the DataType argument to the OpenText method. It appears to take magic arguments.

VBA: Workbooks.OpenText filename:="DATA.TXT", dataType:=xlDelimited, tab:=True

I would guess in powershell it accepts a hash, so:

$xl.Workbooks.OpenText(@{Filename = $CSVFile; dataTyype = "xlDelimited", other = $true; otherchar=':' })

However, I've no way to test this currently.

Related Topic