1

我想使用documents4j将Excel文件转换为PDF,但有两个问题:

  1. 转换时我必须打开一个 Excel 文件。当我将 Word 转换为 PDF 时,我不需要打开 doc/docx 文件。
  2. PDF 不包含 Excel 中包含的图表图像。

我该如何解决这个问题?这是重现该问题的代码:

private void convertExcelToPDF1() throws Exception {

    InputStream excelFileIS = new BufferedInputStream(new FileInputStream("C:\\test_convert\\test.xlsx"));
    File target = new File("C:\\test_convert\\sim_status_excel.pdf");

    IConverter converter = RemoteConverter.builder()
                       .baseFolder(new File("D:\\temp"))
                       .workerPool(20, 25, 2, TimeUnit.SECONDS)
                       .requestTimeout(10, TimeUnit.SECONDS)
                       .baseUri("http://localhost:9998")
                       .build();

    Future<Boolean> conversion = converter.convert(excelFileIS).as(DocumentType.XLSX)
                                    .to(target).as(DocumentType.PDF)
                                    .prioritizeWith(1000)
                                    .schedule();
}
4

1 回答 1

0

MS Excel is not necessarily intended for use of programmatic conversion. Therefore, strange issues can occure. First of all: Did you try to convert your file to PDF by directly using MS Excel? If the issues occure there, too, documents4j is powerless.

Other than that, there might be report-options set that are required for making the export successful. You can have a look at the VBS scripts that documents4j runs for triggering Excel conversion: https://github.com/documents4j/documents4j/tree/master/documents4j-transformer-msoffice/documents4j-transformer-msoffice-excel/src/main/resources

You can save these files locally and run them. First, you run excel_start.vbs from the command line in Windows. Then you run excel_convert.vbs input.xls output.pdf 999 which triggers the to-PDF conversion for the given file. You can clean up by running excel_stop.vbs. after you are done.

The two parameters that are interesting to you are:

excelApplication.Workbooks.Open(inputFile, , True, , , , , , , , , , , , 2)

and

excelDocument.ExportAsFixedFormat xlTypePDF, outputFile

which you can tweak to make your conversion run. I only tested with standard Excel files which work fine in my unit tests. If you find out a solution that works for you, I am happy to merge your changes. The methods (ExportAsFixedFormat and Open) are documented on MSDN.

于 2015-12-17T11:00:13.750 回答