Powershell Script to convert Excel files to CSV


Taken from my old blog site “Vipinvasudevan.blogspot.com”

Below script help you to convert any excel files in to csv format

#————–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—————————–

In case if you have multiple files of .xlsx in same location, and want to convert all in to CSV. Chang the script like

#————–Script Start—————————–

#Convert Excel file to CSV
Path = get-childitem -path “<Your path>”
$xlCSV=6
foreach ($file in $path) {
$Excelfilename = $file.fullname
$CSVfilename = “” + $file.Basename+ “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—————————–

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s