0

hello I am exporting a jtable to excel. And it exports all the data fine except the dates. At the end of the public void exportToExcel function, a series of else ifs are made with some conversions, but I am missing those of the dates. This is the code:

public void exportToExcel(JTable table, String filePath) throws Exception {
  TableModel model = table.getModel();
  Workbook workbook = new XSSFWorkbook();
  Sheet sheet = workbook.createSheet();
  Row row;
  Cell cell;

  // write the column headers
  row = sheet.createRow(0);
  for (int c = 0; c < model.getColumnCount(); c++) {
   cell = row.createCell(c);
   cell.setCellValue(model.getColumnName(c));
  }

  // write the data rows
  for (int r = 0; r < model.getRowCount(); r++) {
   row = sheet.createRow(r+1);
   for (int c = 0; c < model.getColumnCount(); c++) {
    cell = row.createCell(c);
    Object value = model.getValueAt(r, c);
    if (value instanceof String) 
    {
     cell.setCellValue((String)value);
    } 
    else if (value instanceof Double) 
    {
     cell.setCellValue((Double)value);
    }
    else if (value instanceof Integer)
    {
     cell.setCellValue((Integer)value);
    }
    else if (value instanceof Long)
    {
     cell.setCellValue((Long)value);
    }
    else if (value instanceof BigDecimal)
    {       
     cell.setCellValue(((BigDecimal) value).doubleValue());
    }
    else if (value instanceof BigInteger)
    {       
     cell.setCellValue(((BigInteger) value).longValue());
    }
   }
  }

  FileOutputStream out = new FileOutputStream(filePath);
  workbook.write(out);
  out.close();
  //workbook.close();

 }

0 Answers0