使用batch file重命名和格式化Excel工作表

是否有可能创build一个batch file ,将执行以下操作?

  1. 重命名Excel文件中的单个工作表( 而不是 Excel工作簿/文件本身)
  2. 将简单格式应用于Excel文件 – 例如,将字体和字号应用于整个工作簿。

谢谢

创build两个文件:batch file和由batch file调用的VBScript文件。 在VBS中操作Excel的工作很容易。

modder.bat

:Start @Echo off CScript modder.vbs :End 

modder.vbs

 'launch Excel and open file Set xlObj = CreateObject("Excel.Application") Set xlFile = xlObj.WorkBooks.Open("c:\temp\filename.xls") 'turn off screen alerts xlObj.Application.DisplayAlerts = False 'loop through sheets For Each Worksheet In xlFile.Worksheets 'change sheet to desired worksheet name If Worksheet.Name = "SheetToRename" Then Worksheet.Name = "NewName" End If 'change all sheets to desired font With Worksheet.Cells.Font .Name = "Verdana" .Size = 12 End With Next 'save, close, then quit xlFile.Close True xlObj.Quit 

modder.vbs (带有workday ()函数,按要求)

 'launch Excel and open file Set xlObj = CreateObject("Excel.Application") Set xlFile = xlObj.WorkBooks.Open("c:\temp\filename.xls") 'turn off screen alerts xlObj.Application.DisplayAlerts = False 'loop through sheets For Each Worksheet In xlFile.Worksheets 'change sheet to desired worksheet name If Worksheet.Name = "SheetToRename" Then 'get prior workday dPriorWorkday = xlObj.Application.WorksheetFunction.WorkDay(Now, -1) 'format as necessary (I used ISO 8061) Worksheet.Name = Year(dPriorWorkday) & "-" & Right("0" & Month(dPriorWorkday),2) & "-" & Right("0" & Day(dPriorWorkday),2) End If 'change all sheets to desired font With Worksheet.Cells.Font .Name = "Verdana" .Size = 12 End With Next 'save, close, then quit xlFile.Close True xlObj.Quit 

修改部分表单名称

  If Left(Worksheet.Name,12) = "Target Sheet" Then 'get prior workday dPriorWorkday = xlObj.Application.WorksheetFunction.WorkDay(Now, -1) 'format as necessary (I used ISO 8061) Worksheet.Name = "Target Sheet " & Year(dPriorWorkday) & "-" & Right("0" & Month(dPriorWorkday),2) & "-" & Right("0" & Day(dPriorWorkday),2) End If