如何以自动方式将 Excel 文档(文件)转换为 PDF?我正在尝试使此处找到的解决方案适应Excel。到目前为止,我有这个:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var docPath = WScript.Arguments(0);
docPath = fso.GetAbsolutePathName(docPath);
var pdfPath = docPath.replace(/\.xls[^.]*$/, ".pdf");
var objExcel = null;
try
{
WScript.Echo("Saving '" + docPath + "' as '" + pdfPath + "'...");
objExcel = new ActiveXObject("Excel.Application");
objExcel.Visible = false;
var objExcel = objExcel.Workbooks.Open(docPath);
var wdFormatPdf = 17;
objExcel.SaveAs(pdfPath, wdFormatPdf);
objExcel.Close();
WScript.Echo("Done.");
}
finally
{
if (objExcel != null)
{
objExcel.Quit();
}
}
我得到这个输出:
Saving 'somefile.xlsx' as 'somefile.pdf'...
Done.
..\SaveXLSXAsPDF.js(27, 9) (null): The object invoked has disconnected from its clients.
PDF 文件已创建,但它无效并且无法在阅读器中打开。我在想格式 17 可能不适合 Excel。有谁知道正确的号码或如何使它工作?
编辑:我在这里发现了正确的数字(57),但现在我收到了这个错误:
r:\Web\Roman\SaveXLSXAsPDF.js(27, 13) Microsoft JScript runtime error: Class doesn't support Automation