简体   繁体   English

使用 Windows Powershell 在 Excel 中保存 .xlsm 文件类型

[英]Saving a .xlsm file type in Excel using Windows Powershell

Hello all I am opening a .xlsm sheet in windows powershell then writing data to various cells from read-host variables.大家好,我正在 Windows powershell 中打开一个 .xlsm 工作表,然后将数据从读取主机变量写入各种单元格。 Everything works fine except when I go to save the file using powershell.一切正常,除非我使用 powershell 保存文件。 It gives me this error quoted below and refuses to save it in .xlsm format the file is already a .xlsm file so im not converting it.它给了我下面引用的这个错误,并拒绝以 .xlsm 格式保存它,该文件已经是一个 .xlsm 文件,所以我没有转换它。 I have no problems when using this code to save .xlsx formats, which is probably my problem.使用此代码保存 .xlsx 格式时我没有问题,这可能是我的问题。 I am a bit of newb forgive my ignorance.我是个新手请原谅我的无知。

"This extension can not be used with the selected file type. Change the file extension in the File name text box or select a different file type by changing the Save as type." “此扩展名不能与选定的文件类型一起使用。在文件名文本框中更改文件扩展名,或通过更改另存为类型来选择不同的文件类型。”

Here's the code I am using to save to .xlsm.这是我用来保存到 .xlsm 的代码。 I have removed a lot of the file structure to protect privacy and made it simpler, my code works just fine but not the save to .xlsm part, please help...我删除了很多文件结构以保护隐私并使其更简单,我的代码工作正常但不是保存到 .xlsm 部分,请帮助...

            Add-Type -AssemblyName Microsoft.Office.Interop.Excel
            $xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
            $XL = New-Object -comobject Excel.Application
            $Num = Read-Host - Prompt 'What is the number?'
            $FilePath = "V:\$fileName.xlsm"
            $workbook = $XL.Workbooks.Open($FilePath)
            $WS = $workbook.Worksheets("Sheet1")
            $ws.Cells.Item(2,29) = "$Num"
            $XL.DisplayAlerts = $False
            $WB.SaveAs($FilePath)
            $WB.Close
            [System.Runtime.Interopservices.Marshal]::ReleaseComObject($XL) | Out-Null

SOLUTION:解决方案:

            Add-Type -AssemblyName Microsoft.Office.Interop.Excel
            $xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbookMacroEnabled
            $XL = New-Object -comobject Excel.Application
            $Num = Read-Host - Prompt 'What is the number?'
            $FilePath = "V:\$fileName.xlsm"
            $workbook = $XL.Workbooks.Open($FilePath)
            $WS = $workbook.Worksheets("Sheet1")
            $ws.Cells.Item(2,29) = "$Num"
            $XL.DisplayAlerts = $False
            $WB.SaveAs($FilePath,$xlFixedFormat)
            $WB.Close
            [System.Runtime.Interopservices.Marshal]::ReleaseComObject($XL) | Out-Null

From the XlFileFormat docs , you want xlOpenXMLWorkbookMacroEnabled for XLSM files.XlFileFormat文档中,您需要为 XLSM 文件xlOpenXMLWorkbookMacroEnabled xlOpenXMLWorkbookMacroEnabled。

$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbookMacroEnabled
...
$WB.SaveAs($FilePath,$xlFixedFormat)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM