Was trying to extract about 50k rows from a database to .xlsx where one column is formatted as Date and noticed that after 32767 rows Date format disappears. It does not matter if you start with rownum 0 or rownum 500, after 32767 rows formatting disappears. ------------------- Code: package orat_reports; import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; import java.sql.ResultSet; import java.sql.PreparedStatement; import java.io.FileOutputStream; import java.io.IOException; import java.io.FileNotFoundException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFDataFormat; public class SQL_SH_to_XLSX { public static void main(String[] args) { // TODO Auto-generated method stub String url = "jdbc:oracle:thin:@//localhost:1521/orcl"; String username = "User"; String password = "Password"; String sqlQuerry = "Select * from customers"; // Create new workbook SXSSFWorkbook wb = new SXSSFWorkbook(); Sheet sh = wb.createSheet(); wb.setSheetName(0, "SQL Test"); int rownum = 0; try{ Connection con = DriverManager.getConnection(url, username, password); PreparedStatement st = con.prepareStatement(sqlQuerry); ResultSet rs = st.executeQuery(); while(rs.next()) { Row row = sh.createRow(rownum); Cell c = row.createCell(0); c.setCellValue(rs.getDouble(1)); c = row.createCell(1); c.setCellValue(rs.getString(2)); c = row.createCell(2); c.setCellValue(rs.getDate(21)); // Date format for cell XSSFDataFormat df = (XSSFDataFormat)wb.createDataFormat(); CellStyle cs = wb.createCellStyle(); cs.setDataFormat(df.getFormat("dd-mm-yyyy")); c.setCellStyle(cs); rownum++; } } catch (SQLException e) { System.out.println(e.getErrorCode()); } //Write file try { FileOutputStream out = new FileOutputStream("SQL Test.xlsx"); try { wb.write(out); out.close(); wb.close(); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } } catch (FileNotFoundException fnfe) { System.out.println(fnfe.getMessage()); } System.out.println("Happy Days"); } }
Cell Styles are workbook scoped, not cell scoped, so you're running out of available styles. You need to move the cell style creation out of your loop