在通过java运行Powershell脚本以将PPT、XLS转换为PPT时,无法找到文件且内存超出限制
我厌倦了通过Java应用程序运行powershell脚本 我两次手术都很累 1) 将ppt转换为pdf 2) 将xls转换为pdf 我使用的配置是 Windows 2008上的MS Office 2013 将PPT转换为PDF的脚本如下在通过java运行Powershell脚本以将PPT、XLS转换为PPT时,无法找到文件且内存超出限制,java,excel,powershell,pdf,Java,Excel,Powershell,Pdf,我厌倦了通过Java应用程序运行powershell脚本 我两次手术都很累 1) 将ppt转换为pdf 2) 将xls转换为pdf 我使用的配置是 Windows 2008上的MS Office 2013 将PPT转换为PDF的脚本如下 #Convert Powerpoint formats to pdf Param( [string]$inputPath, [string]$outputPath ) Add-type -AssemblyName Office Add-ty
#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()
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");
运行此脚本时,我的内存超出了绑定异常
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
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
谁能告诉我出了什么问题吗
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()
运行此脚本时未找到输入文件异常
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
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应用程序运行所有这些脚本。
代码如下
#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()
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");
powershell命令如下所示
#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()
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");
对于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没有问题
我的申请中遗漏了一些东西。谁能给我指点路吗。是否存在任何权限问题或其他问题?这个问题长期困扰着我
提前谢谢。您是否尝试过使用其他executionpolicy选项,如-executionpolicy ByPass?是的,我使用过-executionpolicy ByPass。你有没有试过使用其他的executionpolicy选项,比如-executionpolicy旁路?是的,我用过-executionpolicy旁路。但这并不意味着它会成功。