使用Powershell格式化excel文件

时间:2018-01-05 21:18:25

标签: excel powershell

我目前正在尝试从VBScript生成一个csv文件,并使用powershell将其转换为xls然后保存它。 (将其用作脱机数据库文件)问题是,一列(列E)是否适用于SKU,并且在翻译过程中会丢失前导0。尝试以下操作时出错。 Powershell对我来说是新手,所以我可能犯了一个简单的错误:

$xl = new-object -comobject excel.application
$xl.visible = $true
$Workbook = $xl.workbooks.open("file location.csv")
$Worksheets = $Workbooks.worksheets
$Worksheets.Columns.("E").NumberFormat = "00000000000"
$Workbook.SaveAs("file location.xls",1)
$Workbook.Saved = $True
$xl.Quit()
编辑:我让它上班了!如果有人有任何指示,请参考以下内容:

$excel = new-object -comobject excel.application
$excel.visible = $true
$Workbook = $excel.workbooks.open("file location.csv")
$Worksheets = $Workbooks.worksheets
$Worksheet = $Workbook.Worksheets.Item(1)
$Range = $Excel.Range("E:E").EntireColumn
$Range.NumberFormat = "00000000000"
$Workbook.SaveAs("file location.xls",1)
$Workbook.Saved = $True
$excel.Quit()

1 个答案:

答案 0 :(得分:0)

您可以通过更改

来实现同样的目标

$Worksheets.Columns.("E").NumberFormat = "00000000000"F15

$workbook.ActiveSheet.Columns.Item("E").NumberFormat = "00000000000"

从第一个示例中获得更少的代码。