Answered by:
Convert excel .xlsx to .csv using Powershell

Question
-
Hi All,
I am getting and excel report with file extension .xlsx and I want to convert the file to .csv using Powershell
Can this be done ? I alreay tried but did not find any concrete way/method to do so
Please guide
Thanks in Advance, Jiniv Thakkar
Wednesday, November 16, 2016 6:49 PM
Answers
-
Hi Jiniv Thakkar,
You could try the following PowerShell:
Function ExcelCSV ($File) { $pwd = "C:\Users\Administrator.CONTOSO\Desktop" $excelFile = "$pwd\" + $File + ".xlsx" $Excel = New-Object -ComObject Excel.Application $Excel.Visible = $false $Excel.DisplayAlerts = $false $wb = $Excel.Workbooks.Open($excelFile) foreach ($ws in $wb.Worksheets) { $ws.SaveAs("$pwd\" + $File + ".csv", 6) } $Excel.Quit() } $FileName = "excel2" ExcelCSV -File $FileName
Note: Don’t forget to change the file name and the location.
Best Regards,
Linda Zhang
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Marked as answer by Jiniv Thakkar Friday, November 18, 2016 2:47 PM
Thursday, November 17, 2016 5:58 AM -
Hi Jiniv Thakkar,
Yes, you should run this cmdlet on the server which has Excel application. Otherwise, we cannot use the Excel comObject.
Another workaround, you could try the following cmdlets.
https://blogs.technet.microsoft.com/pstips/2014/06/02/get-excel-data-without-excel/
Best Regards,
Linda Zhang
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Marked as answer by Jiniv Thakkar Saturday, November 26, 2016 8:30 AM
Monday, November 21, 2016 7:55 AM
All replies
-
Hi Jiniv Thakkar,
You could try the following PowerShell:
Function ExcelCSV ($File) { $pwd = "C:\Users\Administrator.CONTOSO\Desktop" $excelFile = "$pwd\" + $File + ".xlsx" $Excel = New-Object -ComObject Excel.Application $Excel.Visible = $false $Excel.DisplayAlerts = $false $wb = $Excel.Workbooks.Open($excelFile) foreach ($ws in $wb.Worksheets) { $ws.SaveAs("$pwd\" + $File + ".csv", 6) } $Excel.Quit() } $FileName = "excel2" ExcelCSV -File $FileName
Note: Don’t forget to change the file name and the location.
Best Regards,
Linda Zhang
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Marked as answer by Jiniv Thakkar Friday, November 18, 2016 2:47 PM
Thursday, November 17, 2016 5:58 AM -
Hi Linda,
I tried this and got the following error :
class not registered com object
Do I need to install office on server to run the script and over come the error ?
Thanks in Advance, Jiniv Thakkar
Thursday, November 17, 2016 6:20 AM -
Hi
My Suggestion would be you can use PSExcel option to achieve this without installing Office on the server.
https://github.com/RamblingCookieMonster/PSExcel
https://github.com/RamblingCookieMonster/PSExcel/issues/21
http://vipinvasudevan.blogspot.in/2014/03/powershell-script-to-convert-excel-file.html
Thanks,
Thursday, November 17, 2016 9:17 AM -
Hi Jiniv Thakkar,
Yes, you should run this cmdlet on the server which has Excel application. Otherwise, we cannot use the Excel comObject.
Another workaround, you could try the following cmdlets.
https://blogs.technet.microsoft.com/pstips/2014/06/02/get-excel-data-without-excel/
Best Regards,
Linda Zhang
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Marked as answer by Jiniv Thakkar Saturday, November 26, 2016 8:30 AM
Monday, November 21, 2016 7:55 AM -
It worked great for me, I am using this with Excel 2019.
I sometimes get the "frown" on 2019, never on 2016.
Monday, December 9, 2019 9:02 AM