Java Apache POI Excel save as PDF

0 votes
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.

Is there a way to convert to pdf?

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.
Mar 30 in Database by Edureka
• 13,640 points
2,152 views

1 answer to this question.

0 votes

For the software to run, you'll need the Java libraries and JAR files listed below. iText v5.3.4 POI v3.8

To convert XLS to PDF, use this example.

The following is the whole Java code that receives Excel spreadsheet data as an input and converts it to PDF table data:

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
            }
    }
answered Mar 31 by gaurav
• 22,040 points

Related Questions In Database

0 votes
0 answers

Merging Two excel files as two sheets in one workbook in java

I have two xlsx files at folder ...READ MORE

Mar 10 in Database by Edureka
• 13,640 points
660 views
0 votes
0 answers

How to get address, Column Name and Row Name of all marked rows in Excel table as rows in new worksheet

 need the row/column combinations marked with an ...READ MORE

Feb 24 in Database by Edureka
• 13,640 points
799 views
0 votes
1 answer

Are there such things as variables within an Excel formula?

Yes. However, not directly. a less complicated method You ...READ MORE

answered Mar 25 in Database by gaurav
• 22,040 points
68 views
0 votes
1 answer

Is Excel Alt+Enter same as Newline character \n ?

According to the excel documentation, pressing alt+enter ...READ MORE

answered Mar 30 in Database by gaurav
• 22,040 points
506 views
0 votes
1 answer

Pasting an Excel range into an email as a picture

Hold down the ALT + F11 keys ...READ MORE

answered Mar 30 in Database by gaurav
• 22,040 points
1,383 views
0 votes
1 answer

Conditional Sum in Excel that counts values above X as X

B1 formula is: =SUMIF(A1:A5,"<=5") +COUNTIF(A1:A5,">5") *5 Fill in the blanks ...READ MORE

answered Mar 31 in Database by gaurav
• 22,040 points
56 views
0 votes
1 answer

Save dynamically created table content in excel file using SheetJS

text inside tr instead of td in dynamic content. This results ...READ MORE

answered Apr 4 in Database by Edureka
• 13,640 points
1,535 views
0 votes
1 answer

Save excel as pdf changing its orientation to horizontal

To begin, go to the page layout ...READ MORE

answered Mar 30 in Database by gaurav
• 22,040 points
13,900 views
0 votes
1 answer

Merging Two excel files as two sheets in one workbook in java

Basically for this, you need to create ...READ MORE

answered Feb 21 in Database by gaurav
• 22,040 points
183 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP