Convert an excel file (xls, xlsx) to PDF

Asked

Viewed 678 times

0

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

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

I couldn’t find good posts and answers here, someone has some better examples:

https://stackoverflow.com/questions/39572244/java-save-xls-file-as-pdf

In a simple way I just need to convert excel to pdf in java in the best possible way, without reading the whole excel.

I found this example, which does exactly what I need, however I cannot use this because of the license:

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

From now on, thank you !

2 answers

1

You can convert Excel to PDF only by changing the file type at the time of Save As. Change the file type to PDF. Just below the file name.

janela de salvar como

  • It is not acceptable, IE, the process has to be automated, is already saved a PDF, but they want to save from Excel, as if I was going to send to printer, I have done this in the past creating a virtual printer with POI API and Itext, but do not accept this time, it has to be like the service save as pdf, but I can not click on Excel, I have also done Iterating the Excel file with Itext, but want saving without creating a virtual printer, I found this on the internet https://github.com/caryyu/excel2pdf

  • Still, thanks for your help !

0


After many tests, I was able to convince 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
            }
    }

Thank you to those who helped me !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.