0

我需要将xlsx文档转换为pdf格式。我知道iText可以保存 pdf 文档,而Docx4j可以读写 xslx。事实上,我们的应用程序同时使用两者来构建报告。但是我们有非常困难的模板,所以我不能只阅读 xslx(docx4j) 并将其写入 pdf(iText)。格式会丢失,所以我需要另一个转换库。我还听说过像 ( Jxcell ) 这样的商业库,但想使用开源解决方案。

谁能帮我?

4

2 回答 2

3

我使用 iText 和 apache poi:

    FileInputStream filecontent = new FileInputStream(new File(sourcepath));
    FileOutputStream out = new FileOutputStream(new File(destinationPath));
    HSSFWorkbook my_xls_workbook = null;
    HSSFSheet my_worksheet = null;
    XSSFWorkbook my_xlsx_workbook = null;
    XSSFSheet my_worksheet_xlsx = null;
    Document document = new Document(PageSize.ARCH_E, 0, 0, 0, 0);
    PdfWriter writer = PdfWriter.getInstance(document, out);
    document.open();
    PdfDestination magnify = null;
    float magnifyOpt = (float) 70.0;
    magnify = new PdfDestination(PdfDestination.XYZ, -1, -1, magnifyOpt / 100);
    int pageNumberToOpenTo = 1;
    PdfAction zoom = PdfAction.gotoLocalPage(pageNumberToOpenTo, magnify, writer);
    writer.setOpenAction(zoom);
    document.addDocListener(writer);

    Iterator<Row> rowIterator = null;
    int maxColumn = 0;
    if (fileType.equals(".xls")) {
        try {
            my_xls_workbook = new HSSFWorkbook(filecontent);
            my_worksheet = my_xls_workbook.getSheetAt(0);
            rowIterator = my_worksheet.iterator();
            maxColumn = my_worksheet.getRow(0).getLastCellNum();
        } catch (IOException ex) {
            Logger.getLogger(PdfConversion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    if (fileType.equals(".xlsx")) {
        try {
            my_xlsx_workbook = new XSSFWorkbook(filecontent);
            my_worksheet_xlsx = my_xlsx_workbook.getSheetAt(0);
            rowIterator = my_worksheet_xlsx.iterator();
            maxColumn = my_worksheet_xlsx.getRow(0).getLastCellNum();
        } catch (IOException ex) {
            Logger.getLogger(PdfConversion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    PdfPTable my_table = new PdfPTable(maxColumn);
    my_table.setHorizontalAlignment(Element.ALIGN_CENTER);
    my_table.setWidthPercentage(100);
    my_table.setSpacingBefore(0f);
    my_table.setSpacingAfter(0f);
    PdfPCell table_cell;
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next(); //Fetch CELL
            switch (cell.getCellType()) { //Identify CELL type
                case Cell.CELL_TYPE_STRING:
                    //Push the data from Excel to PDF Cell
                    table_cell = new PdfPCell(new Phrase(cell.getStringCellValue()));
                    if (row.getRowNum() == 0) {
                        table_cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        table_cell.setBorderColor(BaseColor.BLACK);
                    }
                    my_table.addCell(table_cell);
                    break;
            }
        }
    }
    document.add(my_table);
    document.close();
    System.out.println("Excel file converted to PDF successfully");
于 2014-01-14T14:50:56.483 回答
0

它必须用Java完成吗?如果是,也许看看Apache POI

否则,为什么不拥有一个使用 PDF 打印机并调用相关 API 将文件直接打印为 PDF 的小型原生应用程序呢?当然,Java 并不是真正适合做这种工作的......

例如,这里有一个非 Java 框架:Solidworks 的 PDF 类库

于 2011-09-28T10:33:31.753 回答