首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >java excel到pdf转换

java excel到pdf转换
EN

Stack Overflow用户
提问于 2011-09-28 10:14:15
回答 2查看 11.9K关注 0票数 0

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

有谁可以帮我?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-28 10:33:31

必须用Java来完成吗?如果是,请看一下阿帕奇POI

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

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

票数 0
EN

Stack Overflow用户

发布于 2014-01-14 14:50:56

我使用了iText和apache:

代码语言:javascript
运行
复制
    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");
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7581635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档