// i wrote a sample code to create xlsx format file, but it is not working properly at line number 19 private void write2007(File file) throws FileNotFoundException, IOException { XSSFWorkbook workbook = new XSSFWorkbook(); if (sheetName.isEmpty()) { sheetName = "Sheet 1"; } XSSFSheet sheet = workbook.createSheet(sheetName); XSSFRow headRow = sheet.createRow(0); // Write Head for (int i = 0; i < tableModel.getColumnCount(); i++) { XSSFCell cell = headRow.createCell(i); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(tableModel.getColumnName(i)); //Cell Style XSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER); // bug code, unable to change the background color of cell cellStyle.setFillBackgroundColor(new XSSFColor(Color.LIGHT_GRAY)); XSSFFont font = workbook.createFont(); font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); cellStyle.setFont(font); cell.setCellStyle(cellStyle); } //Write Data for (int i = 0; i < tableModel.getRowCount(); i++) { XSSFRow row = sheet.createRow(i + 1); for (int j = 0; j < tableModel.getColumnCount(); j++) { XSSFCell cell = row.createCell(j); String cellVal = null; try { cellVal = tableModel.getValueAt(i, j).toString(); } catch (NullPointerException ex) { cellVal = ""; } try { float parseFloat = Float.parseFloat(cellVal); cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue(parseFloat); } catch (NumberFormatException ex) { cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue(cellVal); } } } for (int i = 0; i < tableModel.getColumnCount(); i++) { sheet.autoSizeColumn(i); } FileOutputStream fos = new FileOutputStream(file); workbook.write(fos); fos.close(); }