1

In my java application I am trying to convert excel to pdf using apache-poi.In my excel file,the number of columns are different in some rows.First and second rows contain one column,remaining rows contains 8 columns.Here is my code

while (rowIterator.hasNext()) {
Row row = rowIterator.next();
int cellNumber = 0;
if (flag) {
            table = new PdfPTable(row.getLastCellNum());
            flag = false;
}

// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();

            switch (cell.getCellType()) {  
            case Cell.CELL_TYPE_STRING:
                        if (temp == 0) {                                 
                                    numberOfColumns = row.getLastCellNum();
                                    PdfPCell c1 = new PdfPCell(new Phrase(
                                                            cell.getStringCellValue()));
                                    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
                                    table.addCell(c1);
                                    table.setHeaderRows(1);

                        }else{
                                    cellNumber =checkEmptyCellAndAddCellContentToPDFTable(cellNumber,cell,table);
                        }                                                         
                        cellNumber++;
                        break;

            case Cell.CELL_TYPE_NUMERIC:
                        cellNumber =checkEmptyCellAndAddCellContentToPDFTable(cellNumber,cell,table);
                        cellNumber++;
                        break;
            }                     
}
temp = 1;
if(numberOfColumns != cellNumber){
            for(int i=0;i<(numberOfColumns-cellNumber);i++){
                        table.addCell(" ");
            }
}

}

So when I execute this,I will get the pdf file with only one column.But I want same as my excel format.How can I achieve this in my code dynamically(I have to apply this logic for other excel files also)?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Alex
  • 790
  • 1
  • 7
  • 22

1 Answers1

0

You can use this approach to iterate each cell of every row .

     Sheet sheet = workbook.getSheetAt(0);
            //Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.iterator();
    while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                if(isRowEmpty(row)){
                    System.out.println("Row "+row.getRowNum()+"is a empty row"); 
                    continue;
                }
    Iterator<Cell> cellIterator = row.cellIterator();
                    Cell cell =null;
                    while (cellIterator.hasNext()) {
                         cell = cellIterator.next();

                            switch (cell.getCellType()) {
                                case Cell.CELL_TYPE_BOOLEAN:
                                    String value = String.valueOf(cell.getBooleanCellValue());
                                    break;
                                case Cell.CELL_TYPE_NUMERIC: 
                                    Double d = cell.getNumericCellValue();
                                    break;
                                case Cell.CELL_TYPE_STRING:
                                    String value = cell.getStringCellValue().trim();
                                    break;
                                case Cell.CELL_TYPE_BLANK:
                                    break;
                            }

                    }
}
Krishna Kant
  • 105
  • 9