How do you convert multiple xlsx files to csv files with a batch script?
-
@Brian Huh? I am not the original poster, and therefor I cannot accept an answer. I just retaged the question. @Ankur has to accept the answer. – Ocaso Protal Mar 29 '11 at 05:30
-
The answer from @Swampape works well. Would be good if the OP could mark an answer. – Phoebe Jan 15 '15 at 17:42
-
[in2csv](http://csvkit.readthedocs.org/en/latest/scripts/in2csv.html) is the tool – marbel Apr 23 '15 at 18:30
-
1Try this one: https://github.com/dilshod/xlsx2csv – user183038 May 03 '11 at 11:40
-
Credit to @chris-rudd Here is a version that will handle multiple files drag and dropped from windows. Based on the above works by Christian Lemer plang ScottF [https://stackoverflow.com/a/36804963](https://stackoverflow.com/a/36804963) This uses VBS and needs Excel Installed – Yakov Kantor Mar 14 '17 at 17:06
9 Answers
-
This might work but not for me. I strictly partition my OS from the apps. At least one large application I wrote failed after 2 years in production and required monthly maintenance to stay current with libraries. – Richard Nov 22 '21 at 20:33
Get all file item and filter them by suffix and then use PowerShell Excel VBA object to save the excel files to csv files.
$excelApp = New-Object -ComObject Excel.Application
$excelApp.DisplayAlerts = $false
Get-ChildItem -File -Filter '*.xlsx' | ForEach-Object {
$workbook = $excelApp.Workbooks.Open($_.FullName)
$csvFilePath = $_.FullName -replace "\.xlsx$", ".csv"
$workbook.SaveAs($csvFilePath, [Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSV)
$workbook.Close()
}
You can find the complete sample here How to convert Excel xlsx file to csv file in batch by PowerShell
-
Good answer but dead link now. Presumably $ExcelFiles should be something like `Get-ChildItem -File -Filter '*.xlsx'` – RJFalconer Sep 04 '22 at 02:29
Alternative way of converting to csv. Use libreoffice
:
libreoffice --headless --convert-to csv *
Please be aware that this will only convert the first worksheet of your Excel file.

- 329
- 2
- 9

- 1,558
- 3
- 16
- 22
-
3On a mac: `/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to csv *` – Andrew McCombe Oct 11 '19 at 07:42
To follow up on the answer by user183038, here is a shell script to batch rename all xlsx files to csv while preserving the file names. The xlsx2csv tool needs to be installed prior to running.
for i in *.xlsx;
do
filename=$(basename "$i" .xlsx);
outext=".csv"
xlsx2csv $i $filename$outext
done

- 3,216
- 25
- 35
You need an external tool, in example: SoftInterface.com - Convert XLSX to CSV.
After installing it, you can use following command in your batch:
"c:\Program Files\Softinterface, Inc\Convert XLS\ConvertXLS.EXE" /S"C:\MyExcelFile.xlsx" /F51 /N"Sheet1" /T"C:\MyExcelFile.CSV" /C6 /M1 /V

- 1,919
- 2
- 20
- 39
-
2Statement "Command line is not enough powerful to convert xlsx into csv." should be removed (untrue), and "You need some external tool" should be replaced by "You can use" etc. See answer by @marbel here for instance (but there are other solutions). – Thibaut Barrère Mar 15 '16 at 10:05
-
See http://stackoverflow.com/questions/10557360/convert-xlsx-to-csv-in-linux-command-line?rq=1 – Thibaut Barrère Mar 15 '16 at 10:07
Needs installed excel as it uses the Excel.Application
com object.Save this as .bat
file:
@if (@X)==(@Y) @end /* JScript comment
@echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */
var ARGS = WScript.Arguments;
var xlCSV = 6;
var objExcel = WScript.CreateObject("Excel.Application");
var objWorkbook = objExcel.Workbooks.Open(ARGS.Item(0));
objExcel.DisplayAlerts = false;
objExcel.Visible = false;
var objWorksheet = objWorkbook.Worksheets(ARGS.Item(1))
objWorksheet.SaveAs( ARGS.Item(2), xlCSV);
objExcel.Quit();
It accepts three arguments - the absolute path to the xlsx file, the sheet name and the absolute path to the target csv file:
call toCsv.bat "%cd%\Book1.xlsx" Sheet1 "%cd%\csv.csv"

- 55,367
- 18
- 148
- 187
-
1Worked for me with: .\toCsv.bat "%cd%\Book1.xlsx" Sheet1 "%cd%\csv.csv" – Tal Jacob - Sir Jacques Feb 05 '22 at 07:28
Adding to @marbel's answer (which is a great suggestion!), here's the script that worked for me on Mac OS X El Captain's Terminal, for batch conversion (since that's what the OP asked). I thought it would be trivial to do a for
loop but it wasn't! (had to change the extension by string manipulation and it looks like Mac's bash is a bit different also)
for x in $(ls *.xlsx); do x1=${x%".xlsx"}; in2csv $x > $x1.csv; echo "$x1.csv done."; done
Note:
${x%”.xlsx”}
is bash string manipulation which clips.xlsx
from the end of the string.- in2csv creates separate csv files (doesn’t overwrite the xlsx's).
- The above won't work if the filenames have white spaces in them. Good to convert white spaces to underscores or something, before running the script.
-
Great solution! FWIW, by quoting carefully, (including `x1="${x%".xlsx"}";`) and setting [IFS to `\n`](https://superuser.com/a/409822/86708), you can handle spaces very effectively. But, there are even [more robust ways](https://superuser.com/a/409851/86708), since filenames could have newlines, too. – jpaugh Aug 21 '18 at 22:58
-
2Great answer. The many `.xlsx` I had contained 2 blank lines at the top. Modifying the command to remove the first 2 lines (needs to be before the redirect): `for x in $(ls *.xlsx); do x1=${x%".xlsx"}; in2csv $x | sed 1,2d > $x1.csv; echo "$x1.csv done."; done` Thank you for a great option, including the bash removal of `.xlsx` avoiding the need to use bash `base` option! – jys Oct 05 '20 at 20:08
gocsv works on Mac, Linux and Windows and has no dependencies (no Microsoft Office, Libre Office or Python). Just run the xlsx command like so:
gocsv xlsx file.xlsx

- 4,073
- 3
- 28
- 27
This PowerShell solution worked for me. Other options (like csvkit) blew up due to encoding issues, but this worked:
- Run PowerShell as Administrator
Install-Module ImportExcel
- Run
Import-Module ImportExcel
to see if you are able to run it.- If you get an error like, "running scripts is disabled on this system," you need to run something like this:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
- If you get an error like, "running scripts is disabled on this system," you need to run something like this:
cd
to the directory containing all the .xlsx files- Run this to convert every xlsx file in the current directory into a CSV:
Get-ChildItem -Filter *.xlsx | ForEach-Object { Import-Excel $_.FullName | Export-Csv ($_.FullName -replace '\.xlsx$','.csv') -NoTypeInformation }
When done, you may want to put your execution permissions back. For me, that meant running Set-ExecutionPolicy -ExecutionPolicy Restricted
.

- 1,771
- 1
- 19
- 19