首页 > 解决方案 > 带有 Apache POI 的 excel 文件表到带有 iText 的 PDF - 具有值的单元格在 pdf 中没有公式

问题描述

我用 Apache POI 创建了一个 excel 文件表。现在我需要将其转换为 PDF。我想使用 iText,但我想将值而不是公式复制到 pdf。我怎样才能避免在我的 pdf 文件中使用该公式。

public class pdfExcelTest {

public static void main(String[] args) throws IOException {

    // source file whit formula in cells
    String file = "c:/my-excel-sheet.xls";

    try {

        if (file != null) {

            FileInputStream fileInputStream = new FileInputStream(file);
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);

            /*
             * ==================================================================
             * Iterating
             * over all the rows and columns in a Sheet (Multiple ways)
             * ==================================================================
             */

            // Getting the Sheet at index zero
            Sheet sheet = workbook.getSheetAt(0);

            // Create a DataFormatter to format and get each cell's value as String
            DataFormatter dataFormatter = new DataFormatter();

            // landscape format
            Document document = new Document(PageSize.A4.rotate());

            // portrait format
            // Document document = new Document(PageSize.A4);

            try {
                PdfWriter.getInstance(document, new FileOutputStream("z:/excel2pdfTest.pdf"));
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            document.open();
            
            //the table has 26 columns
            PdfPTable table = new PdfPTable(26);

            
            
            FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
            
            
            // obtain a rowIterator and columnIterator and iterate over them
            System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
            Iterator<Row> rowIterator = sheet.rowIterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();

                // Now let's iterate over the columns of the current row
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();

                    }

                    String cellValue = dataFormatter.formatCellValue(cell);

// 我想我必须为 CellTypes 字符串、值和公式添加不同的案例

// 然后为我的 cell1 构建不同的构造函数

                    System.out.print(cell + "\t");

                    Phrase cell1 = Phrase.getInstance(cellValue);
                    


                    table.addCell(cell1);
                    

                }

                System.out.println();

            }

            try {
                document.add(table);

            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

// 文档也可以有一个或多个页面 - 取决于我的 excel 文件中有多少张,所以我必须定义另一个表

            document.close();

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

标签: excel-formulaitextpdf-generation

解决方案


推荐阅读