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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22856183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:24:55  来源:igfitidea点击:

convert excel file to pdf using java, itext and POI API and retain the settings

javaapache-poiitext

提问by user1416631

I have an Excel file that has 5 columns having few merged cells, blank cells, dates, and other text information (a normal excel file).

我有一个 Excel 文件,它有 5 列,几乎没有合并单元格、空白单元格、日期和其他文本信息(一个普通的 Excel 文件)。

I am reading this file using POI API in java. I am able to convert the file to pdf table using iText jar.

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

But, the whole format is not copied into the pdf. (e.g., merged cells come into one column, and other formatting or settings are all gone).

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

A simple pdf table is created.

创建了一个简单的pdf表。

How do i retain the same format as in excel? (I want exact copy of excel sheet in pdf)

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

Here is the code that I am using

这是我正在使用的代码

     //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  

I have attached the excel file as an image

我已将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

excel 文件作为 .png 文件。 如您所见,该文件是一个简单的文件。 我也想要与 pdf 中的 Excel 相同的样式。 请指导我

回答by user1416631

With Apache Tika, you can convert xlsx file to html format and via apache pdfbox you can convert html formatted text to pdf.

使用 Apache Tika,您可以将 xlsx 文件转换为 html 格式,通过 apache pdfbox 您可以将 html 格式的文本转换为 pdf。

回答by Chen

Have you used ExcelToHtmlConverter? It's in 3.13 release of the Apache POI. It has the same usage as WordToHtmlConverter. After converting Excel to HTML you can use iText to convert HTML to PDF. This is a PDF I got by using those tools:

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

sample

样本