tags:

views:

2260

answers:

6

How could I convert an XLS file to a CSV file on the windows command line.

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

A: 

This looks like it might work.

Sneakyness
Thanks for the link. It does look promising. I'm a bit cautious about installing software from unknown third parties, so I'll wait to see if someone has a MSOffice / OpenOffice solution. I'll definitely consider this though. Thanks.
Joel
+2  A: 

How about with PowerShell?

Code should be looks like this, not tested though

$xlCSV = 6
$Excel = New-Object -Com Excel.Application 
$Excel.visible = $False 
$Excel.displayalerts=$False 
$WorkBook = $Excel.Workbooks.Open("YOUDOC.XLS") 
$Workbook.SaveAs("YOURDOC.csv",$xlCSV) 
$Excel.quit()

Here is a post explaining how to use it

How Can I Use Windows PowerShell to Automate Microsoft Excel?

S.Mark
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: http://support.microsoft.com/kb/320369
Joel
Here is some tips for powershell and excel, http://blogs.technet.com/heyscriptingguy/archive/2006/09/08/how-can-i-use-windows-powershell-to-automate-microsoft-excel.aspx
S.Mark
+1  A: 

Why not write your own?

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.

Jay Riggs
Actually, I have never written any C# ever. But I think I'll give it a crack with the Excel Data Reader.
Joel
A bit overkill don't you think. Smells of NIH.
John
A: 

There's an Excel OLEDB data provider built into Windows; you can use this to 'query' the Excel sheet via ADO.NET and write the results to a CSV file. There's a small amount of coding required, but you shouldn't need to install anything on the machine.

Tim Robinson
+3  A: 

Open Notepad, create a file called XlsToCsv.vbs and paste this in:

if WScript.Arguments.Count < 2 Then
    WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
    Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit
WScript.Echo "Done"

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

XlsToCsv.vbs [sourcexlsFile].xls [destinationcsvfile].csv

This requires Excel to be installed on the machine you are on though.

ScottF
In case anyone was wondering, the parameter 6 in the oBook.SaveAs function is the constant for the CSV format.
ScottF
A: 

Hi, When I run the code at the top of this thread, the file converts fine but the date format in the CSV file is set to MM/DD/YYYY. It needs to be DD/MM/YYYY. What do I need to do to edit the script to correct this please? Thanks

Wynn Owen