Java Apache POI Excel 另存为 PDF

标签 java excel pdf playframework-2.0 apache-poi

如何将 excel 文件转换/保存为 pdf?我正在使用 java play framework 生成一些 excel 文件,现在要求更改为 pdf。我不想重新编码一切。

有没有办法转换成pdf

我生成的 excel 文件来自模板;我读取了 excel 模板文件,写入更改,然后另存为新的 excel 文件。这样,模板就不会改变。它包含边框、图像和其他格式。

最佳答案

您需要以下 Java 库和关联的 JAR 文件才能使程序运行。 兴趣点 v3.8 iText v5.3.4

试试这个例子将 XLS 转换为 PDF

接受 Excel 电子表格数据作为输入并将其转换为 PDF 表格数据的完整 Java 代码如下所示:

 import java.io.FileInputStream;
    import java.io.*;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.ss.usermodel.*;
    import java.util.Iterator;
   import com.itextpdf.text.*;
    import com.itextpdf.text.pdf.*;

    public class excel2pdf {  
            public static void main(String[] args) throws Exception{

                    FileInputStream input_document = new FileInputStream(new File("C:\\excel_to_pdf.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
                    Document iText_xls_2_pdf = new Document();
                    PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream("Excel2PDF_Output.pdf"));
                    iText_xls_2_pdf.open();
                    //we have two columns in the Excel sheet, so we create a PDF table with two columns
                    //Note: There are ways to make this dynamic in nature, if you want to.
                    PdfPTable my_table = new PdfPTable(2);
                    //We will use the object below to dynamically add new data to the table
                    PdfPCell table_cell;
                    //Loop through rows.
                    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
                                                    //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(cell.getStringCellValue()));
                                                     //feel free to 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
            }
    }

希望对你有帮助

关于Java Apache POI Excel 另存为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26056485/

相关文章:

java - 使用 Java GMT+5 :30 获取时间戳

vba - 迭代 Excel VBA 或 VSTO 2005 中的所有单元格

java - 如何用Java将控制台输出写入Excel文件

python - 如何使用python下载网页并导出为PDF

java - 是否可以在 Spring 中为注入(inject)方法创建 junit 测试?

java - 当我尝试在 NetBeans 上运行简单程序时出现错误 "No main class found"。

java - 如何仅在 Java 程序需要时才将 'y' 或 'n' 传递给 shell 脚本?

c# - Excel公式T.INV计算错误使用C#

.net - 在 MVC 和 .NET 中使用 iText 7 生成供下载的 PDF

c# - 显示 "editable"PDF 表单