Home » Java » java – How to save jtable to excel including headers using poi?-Exceptionshub

java – How to save jtable to excel including headers using poi?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

How can I get the JTable header to save in excel i have a code that only the values are saved but header is not including im using POI 3.14 heres my code

for (int i = 0; i <  model.getRowCount(); i++) {
  XSSFRow excelRow = excelSheet.createRow(i);
   for (int j = 0; j <  model.getColumnCount(); j++) {
     XSSFCell excelCell = excelRow.createCell(j);
     excelCell.setCellValue(model.getValueAt(i, j).toString());
     }
 }
How to&Answers:

i got my solution thanks

` for (int i = 0; i <  model.getRowCount()+1; i++) {
      SSFRow excelRow = excelSheet.createRow(i);
      for (int j = 0; j <  model.getColumnCount(); j++) {
          if(i == 0){
                XSSFCell excelCell = excelRow.createCell(j);
                excelCell.setCellValue(model.getColumnName(j));
           }else{
                 XSSFCell excelCell = excelRow.createCell(j);
                 excelCell.setCellValue(model.getValueAt(i-1, j).toString());
                 }
        }
  }`