I have an Excel file in which there are 5 columns with several merged cells, empty cells, dates and other text information (regular excel file).
I am reading this file using the POI API in java. I can convert the file to a pdf table using iText jar.
But the whole format is not copied to pdf. (for example, merged cells fall into one column, while other formatting or settings all disappeared).
A simple pdf table is created.
How to save the same format as in excel? (I want an exact copy of an Excel pdf sheet)
Here is the code I'm using
FileInputStream input_document = new FileInputStream(new File("K:\\DCIN_TER\\DCIN_EPU2\\CIRCUIT FROM BRANCH\\RAINBOW ORDERS\\" + SONo.trim() + "\\" + SONo.trim() + " - Checklist.xls"));
HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document);
HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0);
Iterator<Row> rowIterator = my_worksheet.iterator();
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();
PdfPTable my_table = new PdfPTable(5);
PdfPCell table_cell;
while(rowIterator.hasNext())
{
Row rowi = rowIterator.next();
Iterator<Cell> cellIterator = rowi.cellIterator();
while(cellIterator.hasNext())
{
Cell celli = cellIterator.next();
switch(celli.getCellType())
{
case Cell.CELL_TYPE_STRING:
table_cell = new PdfPCell(new Phrase(celli.getStringCellValue()));
my_table.addCell(table_cell);
break;
case Cell.CELL_TYPE_NUMERIC:
table_cell = new PdfPCell(new Phrase("" + celli.getNumericCellValue()));
my_table.addCell(table_cell);
break;
}
}
}
iText_xls_2_pdf.add(my_table);
iText_xls_2_pdf.close();
input_document.close();
I added excel file as image

source
share