批处理(Batch)把Excel文件xls格式改成xlsx格式以及xlsx格式改为xls格式。
Case1:xls转xlsx - 单个文件.bat
powershell
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$Workbook = $Excel.Workbooks.Open("C:\Test\Excel\1.xls")
$Workbook.CheckCompatibility = $false
$Workbook.SaveAs("C:\Test\Excel\1.xlsx", 51)
$Workbook.Close()
$Excel.Quit()
Case2:xls转xlsx - 多个文件.bat
powershell
$OldFolder = "C:\Test\From"
$NewFolder = "C:\Test\To"
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
Get-ChildItem -Path $OldFolder\*.xls -File | ForEach-Object {
$NewName = $NewFolder + "\" + $_.BaseName + ".xlsx"
"[From] " + $_.FullName
"[To] " + $NewName
$Workbook = $Excel.Workbooks.Open($_.FullName)
$Workbook.CheckCompatibility = $false
$Workbook.SaveAs($NewName, 51)
$Workbook.Close()
}
$Excel.Quit()
Case3:xlsx转xls - 单个文件.bat
powershell
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$Workbook = $Excel.Workbooks.Open("C:\Test\Excel\1.xlsx")
$Workbook.CheckCompatibility = $false
$Workbook.SaveAs("C:\Test\Excel\2.xls", 56)
$Workbook.Close()
$Excel.Quit()
Case4:xlsx转xls - 多个文件.bat
powershell
$OldFolder = "C:\Test\From"
$NewFolder = "C:\Test\To"
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
Get-ChildItem -Path $OldFolder "*.xlsx" | ForEach-Object {
$NewName = $NewFolder + "\" + $_.BaseName + ".xls"
"[From] " + $_.FullName
"[To] " + $NewName
$Workbook = $Excel.Workbooks.Open($_.FullName)
$Workbook.CheckCompatibility = $false
$Workbook.SaveAs("$NewName", 56)
$Workbook.Close()
}
$Excel.Quit()
以上仅供参考学习。