如何使用 VBS 从命令提示符格式化现有的 XLSX 文件?
How do you use VBS to format an existing XLSX file from command prompt?
我正在尝试格式化现有的 xlsx 文件,例如 table,包括使用命令提示符自动调整列大小和交替行颜色。
我知道这是可以做到的,但我丢失了我拥有的 .vbs 脚本。
我所能找到的只是像这样的脚本,它们不引用现有的 xlsx,而是创建它们。
任何帮助将不胜感激。
'Bind to the Excel object
Set objExcel = CreateObject("Excel.Application")
'Create a new workbook.
objExcel.Workbooks.Add
'Select the first sheet
Sheet = 1
'Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(Sheet)
'Name the worksheet
objSheet.Name = "VBS_Excel_Example"
'Set the save location
strExcelPath = "d:\Vbs_Excel_Example.xlsx"
'--------------------------------------------------------
'Populate the worksheet with data
'--------------------------------------------------------
' objSheet.Cells(row, column).Value = "Whatever"
'Add some titles to row 1
objSheet.Cells(1, 1).Value = "Name" 'Row 1 Column 1 (A)
objSheet.Cells(1, 2).Value = "Description" 'Row 1 Column 2 (B)
objSheet.Cells(1, 3).Value = "Something Else" 'Row 1 Column 3 (C)
'Add some data using a loop
For row = 2 to 10
objSheet.Cells(row, 1).Value = "Item Name"
objSheet.Cells(row, 2).Value = "Item Description"
objSheet.Cells(row, 3).Value = "Item Something Else"
Next
'--------------------------------------------------------
' Format the spreadsheet
'--------------------------------------------------------
'Put the first row in bold
objSheet.Range("A1:C1").Font.Bold = True
'Change the font size of the first row to 14
objSheet.Range("A1:C1").Font.Size = 14
'Freeze the panes
objSheet.Range("A2").Select
objExcel.ActiveWindow.FreezePanes = True
'Change column A and B to use a fixed width
objExcel.Columns(1).ColumnWidth = 20
objExcel.Columns(2).ColumnWidth = 30
'Change column C to autofit
objExcel.Columns(3).AutoFit()
'Change the background colour of column A to a light yellow
objExcel.Columns(1).Interior.ColorIndex = 36
'Change the font colour of column C to blue
objExcel.Columns(3).Font.ColorIndex = 5
'--------------------------------------------------------
' Save the spreadsheet and close the workbook
'--------------------------------------------------------
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close
'Quit Excel
objExcel.Application.Quit
'Clean Up
Set objSheet = Nothing
Set objExcel = Nothing
绑定对象后,需要加载现有文件,如下所示:
'Bind to the Excel object
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open ("d:\Vbs_Excel_Example.xlsx")
'Select the first sheet
Sheet = 1
'Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(Sheet)
objSheet.Cells(row, column).Value = "Whatever"
然后做剩下的...
我不确定你卡在了哪一部分,告诉我是否需要更多调整
我正在尝试格式化现有的 xlsx 文件,例如 table,包括使用命令提示符自动调整列大小和交替行颜色。 我知道这是可以做到的,但我丢失了我拥有的 .vbs 脚本。 我所能找到的只是像这样的脚本,它们不引用现有的 xlsx,而是创建它们。 任何帮助将不胜感激。
'Bind to the Excel object
Set objExcel = CreateObject("Excel.Application")
'Create a new workbook.
objExcel.Workbooks.Add
'Select the first sheet
Sheet = 1
'Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(Sheet)
'Name the worksheet
objSheet.Name = "VBS_Excel_Example"
'Set the save location
strExcelPath = "d:\Vbs_Excel_Example.xlsx"
'--------------------------------------------------------
'Populate the worksheet with data
'--------------------------------------------------------
' objSheet.Cells(row, column).Value = "Whatever"
'Add some titles to row 1
objSheet.Cells(1, 1).Value = "Name" 'Row 1 Column 1 (A)
objSheet.Cells(1, 2).Value = "Description" 'Row 1 Column 2 (B)
objSheet.Cells(1, 3).Value = "Something Else" 'Row 1 Column 3 (C)
'Add some data using a loop
For row = 2 to 10
objSheet.Cells(row, 1).Value = "Item Name"
objSheet.Cells(row, 2).Value = "Item Description"
objSheet.Cells(row, 3).Value = "Item Something Else"
Next
'--------------------------------------------------------
' Format the spreadsheet
'--------------------------------------------------------
'Put the first row in bold
objSheet.Range("A1:C1").Font.Bold = True
'Change the font size of the first row to 14
objSheet.Range("A1:C1").Font.Size = 14
'Freeze the panes
objSheet.Range("A2").Select
objExcel.ActiveWindow.FreezePanes = True
'Change column A and B to use a fixed width
objExcel.Columns(1).ColumnWidth = 20
objExcel.Columns(2).ColumnWidth = 30
'Change column C to autofit
objExcel.Columns(3).AutoFit()
'Change the background colour of column A to a light yellow
objExcel.Columns(1).Interior.ColorIndex = 36
'Change the font colour of column C to blue
objExcel.Columns(3).Font.ColorIndex = 5
'--------------------------------------------------------
' Save the spreadsheet and close the workbook
'--------------------------------------------------------
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close
'Quit Excel
objExcel.Application.Quit
'Clean Up
Set objSheet = Nothing
Set objExcel = Nothing
绑定对象后,需要加载现有文件,如下所示:
'Bind to the Excel object
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open ("d:\Vbs_Excel_Example.xlsx")
'Select the first sheet
Sheet = 1
'Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(Sheet)
objSheet.Cells(row, column).Value = "Whatever"
然后做剩下的...
我不确定你卡在了哪一部分,告诉我是否需要更多调整