0

I am using powershell to generate some reports. The reports are generally created in CSV format, with double quotes around each field.

However, some recipients want to receive these in Excel format. I have created a script to take the csv output, and create a xls file from this.

$ExtractFilePath = "Directory/Original_File_Name.csv"
$ExcelFilePath = "Directory/Excel_File_Name.xls"

$xl = new-object -comobject excel.application
$xl.visible = $true
$Workbook = $xl.workbooks.open("$ExtractFilePath")
$Worksheets = $Workbooks.worksheets

$Workbook.SaveAs("$ExcelFilePath",1)
$Workbook.Saved = $True

$xl.Quit()

This works well for most instances, however, I have one report that contains fields with leading zeros. These are dropped in the excel file.

Is there a way to format all of the cells in the workbook as text so that the leading zeros are preserved.

Thanks.

OliverBurke
  • 51
  • 1
  • 5
  • `$xl.Cells(1,1).NumberFormat = "00000"` – Cole9350 May 13 '14 at 19:06
  • Could have sworn this had an answer already. See [this answer](http://stackoverflow.com/questions/17688468/how-to-export-a-csv-to-excel-using-powershell/21169406#21169406) in another StackOverflow question. – TheMadTechnician May 13 '14 at 23:01

0 Answers0