在运行Powershell脚本时,通过Java将PPT,XLS转换为PPT,从而无法find文件和内存

我很累,通过Java应用程序运行PowerShell脚本。

我累了两次手术

1)将ppt转换为pdf。

2)将xls转换为pdf。

我正在使用的configuration是,

Windows 2008上的MS Office 2013

将PPT转换为PDF的脚本如下所示

#Convert Powerpoint formats to pdf Param( [string]$inputPath, [string]$outputPath ) Add-type -AssemblyName Office Add-type -AssemblyName Microsoft.Office.Interop.PowerPoint $ppFormatPDF = 32 $ppQualityStandard = 0 $pp = new-object -comobject powerpoint.application $ppt =$pp.presentations.open($inputPath) $ppt.SavecopyAs($outputPath, $ppFormatPDF) # 32 is for PDF $ppt.close() $pp.Quit() $pp = $null [gc]::collect() [gc]::WaitForPendingFinalizers() 

在运行这个脚本时,我得到的内存超出了界限的exception

  Executing command: powershell -NonInteractive -NoLogo -NoProfile -ExecutionPolicy UnRestricted -File D:\WEB-INF\classes\resource\pptToPdf.ps1 -inputPath D:\Temp\testPpt1.pptx -outputPath D:\ConvertedPdf1.pdf Command Successful: code=0, stderr=Exception calling "Open" with "1" argument(s): "Not enough storage is available to complete this operation. (Exception from HRESULT: 0x8007000E (E_OUTOFMEMORY))" At D:\WEB-INF\classes\resources\pptToPdf.ps1:15 char:1 + $ppt =$pp.presentations.open($inputPath) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation You cannot call a method on a null-valued expression. At D:\WEB-INF\classes\resources\pptToPdf.ps1:16 char:1 + $ppt.SavecopyAs($outputPath, $ppFormatPDF) # 32 is for PDF + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At D:\WEB-INF\classes\resources\pptToPdf.ps1:17 char:1 + $ppt.close() + ~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull 

任何人都可以指导我出了什么问题?

其次为XLS到PDF的脚本是

 #Convert Excel formats to pdf Param( [string]$inputPath, [string]$outputPath ) $xlQualityStandard = 0 # Microsoft.Office.Interop.Excel.XlFixedFormatQuality $xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type] #$excelFiles = Get-ChildItem -Path $folderpath -include *.xls, *.xlsx -recurse $wb= $inputPath; $objExcel = New-Object -ComObject excel.application $objExcel.visible = $false $ci = [System.Globalization.CultureInfo]'en-US' $workbook = $objExcel.workbooks.open($wb) $workbook.Saved = $true $Name = $outputPath $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $Name) $objExcel.Workbooks.close() $objExcel.Quit() 

运行这个脚本得到input文件没有findexception

  Executing command: powershell -NonInteractive -NoLogo -NoProfile -ExecutionPolicy UnRestricted -File D:\WEB-INF\classes\resources \xlsToPdf.ps1 -inputPath D:\testXls.xls -outputPath D:\ConvertedPdf2.pdf Command Successful: code=0, stderr=Exception calling "Open" with "1" argument(s): "Microsoft Excel cannot access the file 'D:\testXls.xls'. There are several possible reasons: The file name or path does not exist. The file is being used by another program. The workbook you are trying to save has the same name as a currently open workbook." At D:\WEB-INF\classes\resources\xlsToPdf.ps1:21 char:2 + $workbook = $objExcel.workbooks.open($wb) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation Property 'Saved' cannot be found on this object; make sure it exists and is settable. At D:\WEB-INF\classes\resources\xlsToPdf.ps1:22 char:2 + $workbook.Saved = $true + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound You cannot call a method on a null-valued expression. At D:\WEB-INF\classes\resources\xlsToPdf.ps1:26 char:2 + $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $Name) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull 

我正在通过Java应用程序运行所有这些脚本。 代码如下

  String command = "powershell -NonInteractive -NoLogo -NoProfile -ExecutionPolicy UnRestricted -File D:\\WEB-INF\\classes\\resources\xlsToPdf.ps1 -inputPath D:\\testXls.xls -outputPath D:\\ConvertedPdf2.pdf"; System.out.println(command); // Executing the command Process powerShellProcess = Runtime.getRuntime().exec(command); // Getting the results powerShellProcess.getOutputStream().close(); String line; System.out.println("Standard Output:"); BufferedReader stdout = new BufferedReader(new InputStreamReader( powerShellProcess.getInputStream())); while ((line = stdout.readLine()) != null) { System.out.println(line); } stdout.close(); System.out.println("Standard Error:"); BufferedReader stderr = new BufferedReader(new InputStreamReader( powerShellProcess.getErrorStream())); while ((line = stderr.readLine()) != null) { System.out.println(line); } stderr.close(); System.out.println("Done"); 

电源shell命令如下

对于XLS到PDF,

  powershell -NonInteractive -NoLogo -NoProfile -ExecutionPolicy UnRestricted -File D:\WEB-INF\classes\resources\xlsToPdf.ps1 -inputPath D:\testXls.xls -outputPath D:\ConvertedPdf2.pdf 

对于PPT到PDF,

  powershell -NonInteractive -NoLogo -NoProfile -ExecutionPolicy UnRestricted -File D:\WEB-INF\classes\resources\pptToPdf.ps1 -inputPath D:\Temp\testPpt1.pptx -outputPath D:\ConvertedPdf1.pdf 

当我厌倦通过我的应用程序运行我的脚本时,我遇到了这些问题。 我的应用程序和汤姆猫在D驱动器上。 当这些不工作,我做了一个独立的Java类,并通过该类运行脚本,脚本正在通过该类。 我甚至通过命令提示符和PowerShell提示还执行了PowerShell命令。 他们在工作。 因此,Windows 2008和MS Office 2013没有问题。

我在我的应用程序中缺less一些东西。 有人可以指导我 有没有许可问题或其他什么? 这个问题困扰了我很长一段时间。

先谢谢你。