Wednesday, March 19, 2014

Powershell Script to convert Excel file to CSV




#--------------Script Start-----------------------------

#Convert Excel file to CSV $xlCSV=6
$Excelfilename = "C:\Temp\file.xlsx"
$CSVfilename = "C:\Temp\file.csv"
$Excel = New-Object -comobject Excel.Application
$Excel.Visible = $False
$Excel.displayalerts=$False
$Workbook = $Excel.Workbooks.Open($ExcelFileName)
$Workbook.SaveAs($CSVfilename,$xlCSV)
$Excel.Quit()
If(ps excel){kill -name excel}

#--------------Script End-----------------------------

6 comments:

Anonymous said...

Use for first two lines:
#Convert Excel file to CSV
$xlCSV=6

Unknown said...

if we have multiple files of .xlxs in same location, i want to convert all to CSV is it possible with this script?

Vipin Vasudevan said...

Sorry for the later response! But yes we can..

$path = get-childitem -path "Your path"

foreach ($file in $path) {

$Excelfilename = $file.fullname
$CSVfilename = "" + $file.Basename+ "CSV"

#Convert Excel file to CSV $xlCSV=6

$Excel = New-Object -comobject Excel.Application
$Excel.Visible = $False
$Excel.displayalerts=$False
$Workbook = $Excel.Workbooks.Open($ExcelFileName)
$Workbook.SaveAs($CSVfilename,$xlCSV)
$Excel.Quit()
If(ps excel){kill -name excel}

Vipin Vasudevan said...

Oops!!! *late reponse

dharma said...

Vipin, I am trying to use your script to convert excel to csv files. I have creditcard numbers and 16 digit is rounding off. Is there a way to work around that please?
Thanks in advance.

MaximumTrance said...

I know Excel has a built in feature to identify credit card numbers :( but there's a work-around out there.