Java Apache POI Excel 另存为 PDF [英] Java Apache POI Excel save as PDF

查看:129
本文介绍了Java Apache POI Excel 另存为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I convert/save excel file to pdf? I'm using java play framework to generate some excel files and now the requirement changes to pdf. I don't want to recode everything.

有没有办法转换成pdf?

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

The excel files I'm generating are from a template; I read the excel template file, write changes, and save as new excel file. That way, the template is unchanged. It contains border, image, and other formatting.

推荐答案

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

You would need the following Java libraries and associated JAR files for the program to work. POI v3.8 iText v5.3.4

试试这个例子把 XLS 转换成 PDF

Try this Example to convert XLS to PDF

下面提供了接受 Excel 电子表格数据作为输入并将其转换为 PDF 表格数据的完整 Java 代码:

The complete Java code that accepts Excel spreadsheet data as an input and transforms that to a PDF table data is provided below:

 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆