批处理(Batch)把Excel文件xls格式和xlsx格式进行互换

批处理(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()

以上仅供参考学习。

http://www.bathome.net/thread-53806-1-6.html

相关推荐
wangsongzxcvbnm23 分钟前
从CreateDialogIndirectParam起---我与大模型对话
microsoft
java1234_小锋28 分钟前
MyBatis如何处理延迟加载?
java·开发语言
FeboReigns1 小时前
C++简明教程(10)(初识类)
c语言·开发语言·c++
学前端的小朱1 小时前
处理字体图标、js、html及其他资源
开发语言·javascript·webpack·html·打包工具
摇光932 小时前
js高阶-async与事件循环
开发语言·javascript·事件循环·宏任务·微任务
沐泽Mu2 小时前
嵌入式学习-QT-Day09
开发语言·qt·学习
小猿_002 小时前
C语言实现顺序表详解
c语言·开发语言
余~~185381628002 小时前
NFC 碰一碰发视频源码搭建技术详解,支持OEM
开发语言·人工智能·python·音视频
GOATLong2 小时前
c++智能指针
开发语言·c++