将excel转换为pdf java

将excel转换为pdf java,java,excel,itext,Java,Excel,Itext,我有一个excel,我想把它转换成pdf。我已经使用了iText,但效率不高,因为我有很多excel模板,我使用Iterator创建pdf,这意味着每次更改excel时都必须更改代码。是否有其他方法将excel转换为pdf?有图书馆吗 谢谢您可以使用Apache POI API 以下是一个例子: public class excel2pdf { public static void main(String[] args) throws Exception{

我有一个excel,我想把它转换成pdf。我已经使用了iText,但效率不高,因为我有很多excel模板,我使用Iterator创建pdf,这意味着每次更改excel时都必须更改代码。是否有其他方法将excel转换为pdf?有图书馆吗


谢谢

您可以使用Apache POI API 以下是一个例子:

   public class excel2pdf {  
            public static void main(String[] args) throws Exception{
                    //First we read the Excel file in binary format into FileInputStream
                    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();
                    //Suppose 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
            }
    }
公共类excel2pdf{
公共静态void main(字符串[]args)引发异常{
//首先,我们将二进制格式的Excel文件读入FileInputStream
FileInputStream input\u document=新的FileInputStream(新文件(“C:\\excel\u to_pdf.xls”);
//将工作簿读入HSSF工作手册
HSSFWorkbook my_xls_工作簿=新的HSSFWorkbook(输入文档);
//将工作表读入HSSF表
HSSFSheet my_sheet=my_xls_工作簿。getSheetAt(0);
//在行上迭代
迭代器rowIterator=我的工作表。迭代器();
//此时,我们将创建输出PDF文档对象
Document iText_xls_2_pdf=新文档();
getInstance(iText_xls_2_pdf,新的FileOutputStream(“Excel2PDF_Output.pdf”);
iText_xls_2_pdf.open();
//假设Excel工作表中有两列,那么我们将创建一个包含两列的PDF表
//注意:如果你愿意的话,有一些方法可以使这个过程在本质上是动态的。
PdfPTable my_table=新PdfPTable(2);
//我们将使用下面的对象向表中动态添加新数据
PdfPCell表_单元;
//在行中循环。
while(roweiterator.hasNext()){
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell Cell=cellIterator.next();//获取单元格
开关(cell.getCellType()){//标识单元格类型
//您需要在此处根据添加更多代码
//您的需求/转换
case Cell.Cell\u类型\u字符串:
//将数据从Excel推送到PDF单元格
table_cell=newpdfpcell(新短语(cell.getStringCellValue());
//请随意移动下面的代码以满足您的需要
my_table.addCell(table_cell);
打破
}
//下一行
}
}
//最后将表添加到PDF文档中
iText_xls_2_pdf.add(我的表格);
iText_xls_2_pdf.close();
//我们创建了我们的pdf文件。。
输入_document.close();//关闭xls
}
}

你读过我的问题吗??此示例并不适用于所有excel。我的表格变形了。你能添加你的excel转储吗?不,我不能添加。但我可以说,在excel上,当文本有两个单元格时,迭代器会给我添加两个单元格(一个带文本,另一个带文本)empty@manuel_k你能用不同的字段创建一个虚拟excel吗(因为你不想给我看你原来的excel),那么您可以添加一个具有相同属性的虚拟excel吗problem@MariaKarpikova您需要Apache poi依赖项,请从maven center下载