您在上面的代码中缺少的是您没有完全限定 Excel 对象。
vbs 如何理解什么是Columns("G:G")
?
尝试这个
Dim objXLApp, objXLWb, objXLWs
Set objXLApp = CreateObject("Excel.Application")
Set objXLWb = objXLApp.Workbooks.Open("E:\Docs\Invoice.csv")
'~~> Working with Sheet1
Set objXLWs = objXLWb.Sheets(1)
With objXLWs
'/* Excel Macro starts */
.Columns("G:G").NumberFormat = "m/d/yyyy"
.Columns("H:H").NumberFormat = "0.00"
'/* Excel Macro ends */
End With
objXLWb.Save
objXLWb.Close (False)
Set objXLWs = Nothing
Set objXLWb = Nothing
objXLApp.Quit
Set objXLApp = Nothing
编辑:我唯一担心的是数字格式不会保留,因为它是 CSV 文件。您可能想将其保存为 Excel 文件?
代码
Dim objXLApp, objXLWb, objXLWs
Set objXLApp = CreateObject("Excel.Application")
objXLApp.Visible = True
Set objXLWb = objXLApp.Workbooks.Open("E:\Docs\Invoice.csv")
'~~> Working with Sheet1
Set objXLWs = objXLWb.Sheets(1)
With objXLWs
.Columns("G:G").NumberFormat = "m/d/yyyy"
.Columns("H:H").NumberFormat = "0.00"
End With
'~~> Save as Excel File (xls) to retain format
objXLWb.SaveAs "C:\Sample.xls", 56
'~~> File Formats
'51 = xlOpenXMLWorkbook (without macro's in 2007-2010, xlsx)
'52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2010, xlsm)
'50 = xlExcel12 (Excel Binary Workbook in 2007-2010 with or without macro's, xlsb)
'56 = xlExcel8 (97-2003 format in Excel 2007-2010, xls)
objXLWb.Close (False)
Set objXLWs = Nothing
Set objXLWb = Nothing
objXLApp.Quit
Set objXLApp = Nothing