java excel 到 pdf 转换

发布于 2024-12-07 02:52:26 字数 365 浏览 0 评论 0原文

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

谁能帮助我吗?

I need to convert xlsx documents to pdf format.
I know that iText can save pdf documents and Docx4j can read and write xslx.
In fact our application use both for building reports.
But we have very difficult templates so I can't just read xslx(docx4j) and write it to pdf(iText). The formatting will be lost, so I need another conversion lib.
I also heard about commercial libraries like (Jxcell ) but want to use open source solution.

Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

圈圈圆圆圈圈 2024-12-14 02:52:26

我使用 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");

i have using iText and 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");
情未る 2024-12-14 02:52:26

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

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

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

Does it have to be done in Java?
If yes, maybe take a look at Apache POI

Otherwise, why not have a small native app that utilises a PDF Printer, and calls the relevant APIs to print the file directly to PDF? Surely Java is not really made for doing this kind of work...

For example here's a Non-Java framework for that: PDF Class Library from Solidworks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文