java - 使用 java、itext 和 POI API 将 excel 文件转换为 pdf 并保留设置

标签 java apache-poi itext

我有一个包含 5 列的 Excel 文件,其中包含很少的合并单元格、空白单元格、日期和其他文本信息(一个普通的 Excel 文件)。

我正在使用 Java 中的 POI API 读取此文件。我可以使用 iText jar 将文件转换为 pdf 表。

但是,整个格式并没有复制到 pdf 中。 (例如,合并的单元格变成一列,其他格式或设置都消失了)。

创建了一个简单的 pdf 表格。

如何保留与 Excel 中相同的格式? (我想要 pdf 格式的 excel 表的精确副本)

这是我使用的代码

     //First we read the Excel file in binary format into FileInputStream
             FileInputStream input_document = new FileInputStream(new File("K:\\DCIN_TER\\DCIN_EPU2\\CIRCUIT FROM BRANCH\\RAINBOW ORDERS\\" + SONo.trim() + "\\" + SONo.trim() + " - Checklist.xls"));

             // Read workbook into HSSFWorkbook
             HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document);

             // Read worksheet into HSSFSheet
             HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0);

             // To iterate over the rows
             Iterator<Row> rowIterator = my_worksheet.iterator();

             //We will create output PDF document objects at this point
             com.itextpdf.text.Document iText_xls_2_pdf = new com.itextpdf.text.Document();

             PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream("K:\\DCIN_TER\\DCIN_EPU2\\CIRCUIT FROM BRANCH\\RAINBOW ORDERS\\" + SONo.trim() + "\\" + SONo.trim() + " - Checklist.pdf"));

             iText_xls_2_pdf.open();

             //we have 5 columns in the Excel sheet, so we create a PDF table with 5 columns; Note: There are ways to make this dynamic in nature, if you want to.
             PdfPTable my_table = new PdfPTable(5);

             //We will use the object below to dynamically add new data to the table
             PdfPCell table_cell;

             //Loop through rows.
             while(rowIterator.hasNext())
                    {
                     Row rowi = rowIterator.next();

                     Iterator<Cell> cellIterator = rowi.cellIterator();

                            while(cellIterator.hasNext())
                            {
                                    Cell celli = cellIterator.next(); //Fetch CELL

                                    switch(celli.getCellType())
                                    {
                                            //Identify CELL type you need to add more code here based on your requirement / transformations
                                     case Cell.CELL_TYPE_STRING:

                                            //Push the data from Excel to PDF Cell
                                            table_cell = new PdfPCell(new Phrase(celli.getStringCellValue()));

                                            //move the code below to suit to your needs
                                            my_table.addCell(table_cell);

                                            break;

                                            case Cell.CELL_TYPE_NUMERIC:

                                            //Push the data from Excel to PDF Cell
                                            table_cell = new PdfPCell(new Phrase("" + celli.getNumericCellValue()));

                                            //move the code below to suit to your needs
                                            my_table.addCell(table_cell);

                                            break;
                                    }
                                    //next line
                            }
             }

             //Finally add the table to PDF document
             iText_xls_2_pdf.add(my_table);
             iText_xls_2_pdf.close();

             //we created our pdf file..
             input_document.close(); //close xls  

我已将 excel 文件作为图像附上

excel file as .png file. As you can see, the file is a simple one. I want the same styles as in Excel in pdf also. Please guide me

最佳答案

你用过ExcelToHtmlConverter吗?它在 Apache POI 的 3.13 版本中。它的用法与 WordToHtmlConverter 相同。将 Excel 转换为 HTML 后,您可以使用 iText 将 HTML 转换为 PDF。这是我使用这些工具获得的 PDF:

sample

关于java - 使用 java、itext 和 POI API 将 excel 文件转换为 pdf 并保留设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22856183/

相关文章:

iText 7 : How to build a paragraph mixing different fonts?

java - 如何使用java从PDF中提取数据并分成特定的类别

java - java.io.IOException : Underlying input stream returned zero bytes 的原因是什么

java - ArrayList<Short> 指向 ShortBuffer (java)

java - 在 java 8 foreach 中使用字符串替换

java - 如何使用java api合并两个具有图表和表格的pptx文件?

java - 根据参数在java中舍入 double 值

java - POI - Excel 仍由某些进程保留且无法编辑

java - 迭代 Map 时,Map 值被覆盖

java - iText 验证 java 中 pdf 的完整性