转换Excel文件(xls,xlsx)为PDF [英] Convert an excel file (xls,xlsx) for PDF

查看:118
本文介绍了转换Excel文件(xls,xlsx)为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将一个Excel文件(xls,xlsx)转换为PDF,但是我正在寻找一种更好的方法,但是我不确定以下示例对我来说是否最合适:

I have to convert an excel file (xls,xlsx) for PDF, however I'm looking for a better way, however I'm not sure if the following example are the best for me:

https ://www.grapecity.com/zh-CN/blogs/use-excel-api-to-convert-spreadsheets-to-pdfs-in-java

我在这里找不到好的帖子和答案,有人提供了一些更好的示例吗?

I didn't find good posts and answers here, someone has some better examples ?

Java将xls文件另存为PDF

以一种简单的方式,我只需要以一种更好,更简单的方式将java中的excel转换为pdf,而无需阅读整个excel.

In a simple way I just need to convert excel to pdf in java in the better andsimple way possible, without reading the entire excel.

我找到了这个示例,它确实满足了我的需要,但是由于许可证的缘故,无法使用它:

I found this example, that does exactly what I need, however can't use that because of the license:

https://kbdeveloper.qoppa.com/sample-java-code-to-convert-excel-to-pdf-using-jofficeconvert/

提前谢谢!

推荐答案

经过大量测试,我确信他们使用的第一个解决方案是正确的:

After so much tests I convinced them that the first solution I used was correct:

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

非常感谢您的帮助!

这篇关于转换Excel文件(xls,xlsx)为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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