konvertieren Sie excel-Datei in pdf mit java, itext und POI API und behält die Einstellungen

Ich habe eine Excel-Datei mit 5 Spalten mit wenigen fusionierten Zellen, leere Zellen, Daten und anderen text-Informationen (eine normale excel-Datei).

Ich lese diese Datei mit POI-API in java. Ich bin in der Lage, um die Datei zu konvertieren, um pdf-Tabelle mittels iText jar.

Aber, das ganze format ist nicht kopiert, in der pdf-Datei. (z.B. verbundene Zellen in eine Spalte, und andere Formatierungen oder Einstellungen sind alle Weg).

Einem einfachen pdf-Tabelle erstellt.

Wie kann ich die behalten das gleiche format wie in excel? (Ich will eine exakte Kopie der excel-Tabelle im pdf)

Hier ist der code, den ich verwende

     //First we read the Excel file in binary format into FileInputStream
             FileInputStream input_document = new FileInputStream(new File("K:\\DCIN_TER\\DCIN_EPU2\\CIRCUIT FROM BRANCH\\RAINBOW ORDERS\\" + SONo.trim() + "\\" + SONo.trim() + " - Checklist.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
             com.itextpdf.text.Document iText_xls_2_pdf = new com.itextpdf.text.Document();

             PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream("K:\\DCIN_TER\\DCIN_EPU2\\CIRCUIT FROM BRANCH\\RAINBOW ORDERS\\" + SONo.trim() + "\\" + SONo.trim() + " - Checklist.pdf"));

             iText_xls_2_pdf.open();

             //we have 5 columns in the Excel sheet, so we create a PDF table with 5 columns; Note: There are ways to make this dynamic in nature, if you want to.
             PdfPTable my_table = new PdfPTable(5);

             //We will use the object below to dynamically add new data to the table
             PdfPCell table_cell;

             //Loop through rows.
             while(rowIterator.hasNext())
                    {
                     Row rowi = rowIterator.next();

                     Iterator<Cell> cellIterator = rowi.cellIterator();

                            while(cellIterator.hasNext())
                            {
                                    Cell celli = cellIterator.next(); //Fetch CELL

                                    switch(celli.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(celli.getStringCellValue()));

                                            //move the code below to suit to your needs
                                            my_table.addCell(table_cell);

                                            break;

                                            case Cell.CELL_TYPE_NUMERIC:

                                            //Push the data from Excel to PDF Cell
                                            table_cell = new PdfPCell(new Phrase("" + celli.getNumericCellValue()));

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

Ich anbei die excel-Datei als Bild

konvertieren Sie excel-Datei in pdf mit java, itext und POI API und behält die Einstellungen

Hey hast du dein problem lösen..man kann versuchen jodconverter.. ich hatte das gleiche problem statement.. ich benutzte die lib und es funktioniert wie ein Charme mit sehr wenig code..
hi Sumeet, bedeutet die Umstellung auch, dass eine Datei gespeichert wird unter einem lokalen Ordner, den Sie eingerichtet haben? können Sie einfach konvertieren und rufen Sie den stream aus?
vor dem gleichen problem, die erzeugte pdf-Datei ist hässlich mit Tabellen ohne Rahmen und passt nicht in die pdf-Ansicht.

InformationsquelleAutor user1416631 | 2014-04-04

Schreibe einen Kommentar