Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

Convert XLS to CSV on command line Ask Question

How could I convert


an XLS file to a CSV
90 file on the windows
command line.

The machine has


Microsoft Office 2000
installed. I'm open to
34
installing OpenOffice if
it's not possible using
Microsoft Office.

windows excel csv

asked Dec 7 '09 at 6:24


Joel
5,768 10 56 70

13 Answers

Open Notepad, create


a file called
107 XlsToCsv.vbs and
paste this in:

if WScript.Arguments.C
WScript.Echo "Erro
XlsToCsv SourcePath.xl
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObj
Dim oBook
Set oBook = oExcel.Wor
oBook.SaveAs WScript.A
oBook.Close
By using our site, you acknowledge thatFalse
you have read and understand our Cookie Policy, Privacy Policy, and our
oExcel.Quit
Terms of Service. WScript.Echo "Done"

https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 1/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

Then from a
command line, go to
the folder you saved
the .vbs file in and
run:

XlsToCsv.vbs [sourcexl

This requires Excel to


be installed on the
machine you are on
though.

edited Feb 8 '17 at 22:14


Lankymart
11.9k 4 42 109

answered Dec 7 '09 at 14:22


ScottF
1,601 1 11 10

18 In case anyone was


wondering, the
parameter 6 in the
oBook.SaveAs
function is the
constant for the
CSV format. –
ScottF Dec 7 '09 at
14:23

4 Requires fully
resolves path
names on my XP
instance. – Andrew
Mar 23 '11 at 6:25

3 I have posted
below a slightly
modified version
which handles file
paths better.
Thanks ScottF! –
plang May 31 '12
at 14:26

7
The code converts
only the active
worksheet. To
select another
worksheet, add the
following line after
the
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
oExcel.Workbook
Terms of Service. s.Open line with
the desired index of
https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 2/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow
the worksheet
(starts at 1):
oBook.Worksheets
(1).Activate –
humbads Oct 30
'13 at 18:19

1 It ought to be noted
that this functional
not just on xls or
xlsx, but on any file
that Excel itself can
open. –
user1318135 Aug
23 '17 at 19:26

A slightly modified
version of ScottF
68 answer, which does
not require absolute
file paths:

if WScript.Arguments.C
WScript.Echo "Plea
<xls/xlsx source file>
Wscript.Quit
End If

csv_format = 6

Set objFSO = CreateObj

src_file = objFSO.GetA
dest_file = objFSO.Get

Dim oExcel
Set oExcel = CreateObj

Dim oBook
Set oBook = oExcel.Wor

oBook.SaveAs dest_file

oBook.Close False
oExcel.Quit

I have renamed the


By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
script ExcelToCsv,
Terms of Service.
since this script is not
https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 3/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

limited to xls at all.


xlsx Works just fine,
as we could expect.

Tested with Office


2010.

answered May 31 '12 at 14:25


plang
3,739 3 19 34

I am using this (with


few adaptations) to
convert from XML to
XLS. However, I
don't want to have
the compatibility
warning message
box from Excel
during this
conversion. Do you
know how can I
disable this warning?
– jpnavarini Aug 8
'12 at 14:18

Hi, I'm sorry, no idea!


:) – plang Aug 8 '12
at 14:28

you saved my day.


nice work –
Pushker Yadav Aug
6 '15 at 18:44

1 I put this answer


together with
@user565869 's
answer in a Gist with
simple instructions.
See: Script to
convert Excel File to
CSV –
10GritSandpaper
Jan 6 '17 at 16:43

1 That's nice put


together
@10GritSandpaper
– amrrs Nov 6 '17 at
8:13

By using our site, you acknowledge that you have


A small expansion on read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service. ScottF's groovy VB

https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 4/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

20 script: this batch file


will loop through the
.xlsx files in a directory
and dump them into
*.csv files:

FOR /f "delims=" %%i I

Note: You may change


extension .xlsx to .xls
andname of script
ExcelToCSV to
XlsToCsv

edited Jun 24 '16 at 1:21


Michael Freidgeim
13.7k 6 91 115

answered Jun 28 '12 at 20:54


user565869

1 @Rieaux: Regarding
your comment-as-
an-edit: if this gives
the files a double
extension, a second
simple batch file can
rename them. This is
drifting into a new
question, though;
please give it a try
and, if you're unable
to make it work, post
a new question here
on SU. –
user565869 Sep 30
'13 at 20:20

this automation
saved my life. :)
Thank u –
Pushker Yadav Aug
6 '15 at 18:45

1
I put this answer
together with
@plang 's answer in
a Gist with simple
instructions. See:
Script to convert
Excel File to CSV –
10GritSandpaper
By using our site, you acknowledge
Jan 6 '17that you have read and understand our Cookie Policy, Privacy Policy, and our
at 16:44
Terms of Service.
@10GritSandpaper
https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 5/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow
Using Excel 2007.
Script in your link
didn't work for me. –
Boris_yo Dec 18 '17
at 8:34

How about with


PowerShell?
16 Code should be looks
like this, not tested
though

$xlCSV = 6
$Excel = New-Object -C
$Excel.visible = $Fals
$Excel.displayalerts=$
$WorkBook = $Excel.Wor
$Workbook.SaveAs("YOUR
$Excel.quit()

Here is a post
explaining how to use
it

How Can I Use


Windows PowerShell
to Automate Microsoft
Excel?

edited Dec 7 '09 at 8:57

answered Dec 7 '09 at 6:33


YOU
87.2k 21 154 199

This looks like a


good approach.
Unfortunately, I
couldn't get it going.
I'm not familiar with
PowerShell, so when
I ran into an error I
didn't know what to
do. I couldn't find a
PowerShell-specifc
solution:
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
support.microsoft.co
Terms of Service. m/kb/320369 –

https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 6/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow
Joel Dec 7 '09 at
8:46

Here is some tips for


powershell and
excel,
blogs.technet.com/h
eyscriptingguy/archiv
e/2006/09/08/… –
YOU Dec 7 '09 at
8:53

I did a test run of this


and ran into
problems as well.
One thing I ran into
was difficulty with the
$Excel.Workbooks
.Open method. It
couldn't find the
specified file. I
worked around this
by using Get-Item
on the file and piping
it to a ForEach-
Object loop
(something I'll end
up doing in my final
implementation
anyway) for the two
lines starting with
$Workbook . – Iszi
Feb 3 '15 at 15:12

That fixed that


problem, but then I
couldn't find the
resulting
"YOURDOC.csv" - it
wasn't in the same
folder as
"YOUDOC.XLS". I
went back to old &
trusty CMD and did
CD /D C:\ && DIR
YOURDOC.csv /s .
Turns out the file
was saved into My
Documents by
default. So, you
need to put more
into the script if you
want to save the file
to the same folder
you're working in (if
other than My
Documents). – Iszi
Feb 3 '15 at 15:13
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.

https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 7/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

I had a need to extract


several cvs from
7 different worksheets,
so here is a modified
version of plang code
that allows you to
specify the worksheet
name.

if WScript.Arguments.C
WScript.Echo "Plea
ExcelToCsv <sheetName>
Wscript.Quit
End If

csv_format = 6

Set objFSO = CreateObj

src_file = objFSO.GetA
dest_file = objFSO.Get

Dim oExcel
Set oExcel = CreateObj

Dim oBook
Set oBook = oExcel.Wor

oBook.Sheets(WScript.A
oBook.SaveAs dest_file

oBook.Close False
oExcel.Quit

answered May 10 '13 at 23:30


Christian Lemer
663 8 15

Here is a version that


will handle multiple
7 files drag and dropped
from windows. Based
on the above works by

Christian Lemer
plang
ScottF

Open Notepad,
By using our site, you acknowledge create
that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service. a file called

https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 8/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

XlsToCsv.vbs and
paste this in:

'* Usage: Drop .xl* fi

'* Global Settings and


Dim gSkip
Set args = Wscript.Arg

For Each sFilename In


iErr = ExportExcel
' 0 for normal suc
' 404 for file not
' 10 for file skip
Next

WScript.Quit(0)

Function ExportExcelFi
'* Settings
Dim oExcel, oFSO,
Set oExcel = Creat
Set oFSO = CreateO
iCSV_Format = 6

'* Set Up
sExtension = oFSO.
if sExtension = ""
ExportExcelFil
Exit Function
end if
sTest = Mid(sExten
operator
if not (sTest = "
if (PromptForS
ExportExce
Exit Funct
end if
End If
sAbsoluteSource =
sAbsoluteDestinati

'* Do Work
Set oExcelFile = o
For Each oSheet in
sThisDestinati
oExcelFile.She
oExcelFile.Sav
Next

'* Take Down


oExcelFile.Close F
oExcel.Quit

ExportExcelFileToC
Exit Function
End Function

Function PromptForSkip
if not (VarType(gS
By using our site, you acknowledgePromptForSkip
that you have read and understand our Cookie Policy, Privacy Policy, and our
Exit Function
Terms of Service. end if

https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 9/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow
Dim oFSO
Set oFSO = CreateO

sPrompt = vbCRLF &


"A filename wa
& _
"Do you want t
this once)" & vbCRLF &
"" & vbCRLF &
"Yes - Will
& _
"No - Will
"Cancel - Abor
"" & vbCRLF &
"The unrecogni
sFilename & vb
"" & vbCRLF &
"The path retu
oFSO.GetAbsolu

sTitle = "Unrecogn

sResponse = MsgBo
Select Case sRespo
Case vbYes
gSkip = True
Case vbNo
gSkip = False
Case vbCancel
oExcel.Quit
WScript.Quit(1
abort (1 because wasn'
End Select

PromptForSkip = gS
Exit Function
End Function

answered Apr 22 '16 at 23:51


Chris Rudd
134 1 7

Is there a way to do
UTF-8 Encoding? –
StuBob Sep 27 '17
at 17:54

How to skip writing


header in the target
csv file. I absolutely
dont understand the
above script , but I'm
using it in my
automation. Thank
You. – TharunRaja
Nov 22 '17 at 14:58

@TharunRaja The
By using our site, you acknowledge that you
script opens have read and understand our Cookie Policy, Privacy Policy, and our
the file
Terms of Service. in excel, then does a
"save as" to CSV,
https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 10/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow
just like if you'd done
it by hand except it
hides it in the
background.
Because the script
itself isn't doing the
conversion, and
you're automating it,
my suggestion would
be to call a second
script on the csv files
this outputs,
message me if you
need help making
one that strips out
the first line of a file
you pass it. –
Chris Rudd Nov 26
'17 at 2:41

Why not write your


own?
5 I see from your profile
you have at least
some C#/.NET
experience. I'd create
a Windows console
application and use a
free Excel reader to
read in your Excel
file(s). I've used Excel
Data Reader available
from CodePlex without
any problem (one nice
thing: this reader
doesn't require Excel
to be installed). You
can call your console
application from the
command line.

If you find yourself


stuck post here and
I'm sure you'll get
help.

answered Dec 7 '09 at 6:38


Jay Riggs
By using our site, you acknowledge
47.1k 9 118that you
135have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.

https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 11/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

Actually, I have
never written any C#
ever. But I think I'll
give it a crack with
the Excel Data
Reader. – Joel Dec
7 '09 at 8:49

3 A bit overkill don't


you think. Smells of
NIH. – Mr. Boy Sep
17 '10 at 9:56

1 I don't think the


Excel Data Reader
is NIH. First of all
someone else wrote
it. Secondly, it solved
the problem better
than full blown
Excel. –
Justin Dearing Mar
24 '11 at 21:12

You can do it with


Alacon - command-
3 line utility for Alasql
database. It works
with Node.js, so you
need to install Node.js
+100
and then Alasql
package.

To convert Excel file to


CVS (ot TSV) you can
enter:

> node alacon "SELECT


{headers:true})"

By default Alasql
converts data from
"Sheet1", but you can
change it with
parameters:

{headers:false, sheeti

Alacon supports other


type of conversions
By using our site, you acknowledge
(CSV, TSV,that you have read and understand our Cookie Policy, Privacy Policy, and our
TXT,
Terms of Service. XLSX, XLS) and SQL

https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 12/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

language
constructions (see
User Manual for
examples).

answered Dec 21 '14 at 16:24


agershun
2,987 19 35

1 Wow! Alasql is really


powerful. –
Lukasz Wiktor Aug
29 '18 at 11:01

If you install AlaSQL


globally (npm install
alasql -g) then you
can use simply >
alasql "SELECT...
INTO CSV(...)
FROM XLS(...)" –
agershun Aug 29
'18 at 21:18

Building on what Jon


of All Trades has
2 provided, the following
(~n) removed the
pesky double
extension issue: FOR
/f "delims=" %%i IN
('DIR *.xlsx /b') DO
ExcelToCSV.vbs "%%i"
"%%~ni.csv"

answered Dec 2 '14 at 21:05


Charles Crous
118 1 6

There's an Excel
OLEDB data provider
1 built into Windows;
you can use this to
'query' the Excel sheet
via ADO.NET and
write the results
By using our site, you acknowledge tohave
that you a read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service. CSV file. There's a
small amount of
https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 13/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

coding required, but


you shouldn't need to
install anything on the
machine.

answered Dec 7 '09 at 8:02


Tim Robinson
44.1k 8 100 119

I tried ScottF VB
solution and got it to
1 work. However I
wanted to convert a
multi-tab(workbook)
excel file into a single
.csv file.

This did not work, only


one tab(the one that is
highlighted when I
open it via excel) got
copied.

Is any one aware of a


script that can convert
a multi-tab excel file
into a single .csv file?

answered Mar 16 '12 at 13:49


user1132593
232 1 3 8

Scott F's answer is the


best I have found on
0 the internet. I did add
on to his code to meet
my needs. I added:

On Error Resume
Next <- To account for
a missing xls files in
my batch processing
at the top.
oBook.Application.C
olumns("A:J").Numb
erFormat =that
By using our site, you acknowledge "@"you<-
have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service. Before the SaveAs
line to make sure my
https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 14/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

data is saved
formatted as text to
keep excel from
deleting leading zero's
and eliminating
commas in number
strings in my data i.e.
(1,200 to 1200). The
column range should
be adjusted to meet
your neeeds (A:J).

I also removed the


Echo "done" to make it
non interactive.

I then added the script


into a cmd batch file
for processing
automated data on an
hourly basis via a task.

edited Nov 3 '12 at 6:45


Andro Selva
42.9k 44 175 225

answered Dec 28 '11 at 16:56


Jeffrey O
1

2 I think you should


consider posting
final version of the
code with
comments. –
default locale Nov 3
'12 at 6:45

All of these answers


helped me construct
0 the following script
which will
automatically convert
XLS* files to CSV and
vice versa, by
dropping one or more
files on the script (or
via command
By using our site, you acknowledge that line).
you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service. Apologies for the janky
formatting.
https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 15/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

' https://stackoverflo
' https://gist.github.

' https://stackoverflo
'* Global Settings and
Set args = Wscript.Arg

For Each sFilename In


iErr = ConvertExce
' 0 for normal suc
' 404 for file not
' 10 for file skip
Next

WScript.Quit(0)

Function ConvertExcelF

if IsEmpty(srcFile
WScript.Echo "
WScript.ScriptName & "
ConvertExcelFo
Exit Function
'Wscript.Quit
End If

Set objFSO = Creat

srcExt = objFSO.Ge

' the 6 is the con


' https://msdn.mic
excel
' https://www.rond
Dim outputFormat,

If LCase(Mid(srcEx
outputFormat =
srcDest = "csv
Else
outputFormat =
srcDest = "xls
End If

'srcFile = objFSO.
srcFile = objFSO.G
destFile = Replace

Dim oExcel
Set oExcel = Creat
Dim oBook
Set oBook = oExcel
' preserve formatt
'oBook.Application
oBook.SaveAs destF
oBook.Close False
oExcel.Quit
WScript.Echo "Conv
objFSO.GetFileName(des

End Function
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.
answered Aug 10 '18 at 17:29
https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 16/17
4/26/2019 windows - Convert XLS to CSV on command line - Stack Overflow

drzaus
15.8k 7 90 150

protected by
Community ♦ Jan 9
'12 at 1:36
Thank you for your
interest in this
question. Because it
has attracted low-
quality or spam
answers that had to be
removed, posting an
answer now requires
10 reputation on this
site (the association
bonus does not count).

Would you like to


answer one of these
unanswered questions
instead?

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.

https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line 17/17

You might also like