Increase the maximum length of HSSFCell in java

vineeth picture vineeth · Aug 11, 2015 · Viewed 11.4k times · Source

Actually I tried to store some data in HSSFCell using java but i got an error like

java.lang.IllegalArgumentException: The maximum length of cell contents (text) i
s 32,767 characters
        at org.apache.poi.hssf.usermodel.HSSFCell.setCellValue(HSSFCell.java:559
)
        at org.apache.poi.hssf.usermodel.HSSFCell.setCellValue(HSSFCell.java:533
)
        at application.ExtractUI.datatoexcel(ExtractUI.java:272)
        at application.ExtractUI$3.getData(ExtractUI.java:208)
        at application.ExtractUI$3.handle(ExtractUI.java:198)
        at application.ExtractUI$3.handle(ExtractUI.java:1)

can anyone suggest me a method to increase the cell length ie more than 32767 characters???

I used the following code for which I got the above error

 public void datatoexcel(ResultSet rs) {
        try {
            int iter = 0;
            ResultSetMetaData rmeta = rs.getMetaData();
            int col = rmeta.getColumnCount();
            HSSFWorkbook workbook = new HSSFWorkbook();

            Date date = new Date();

            SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy HHmmss");
            String pa = pth + "\\" + sdf.format(date) + ".xlsx";
            System.out.println(pa);
            FileOutputStream out = new FileOutputStream(new File(pa));
            HSSFSheet sheet = workbook.createSheet();
            HSSFRow myRow = null;
            HSSFCell myCell = null;
            // Font style for headers
            HSSFFont boldFont = workbook.createFont();
            boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            boldFont.setColor(HSSFFont.COLOR_RED);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFont(boldFont);
            while (rs.next()) {
                // limit the data to 1000 anad create a new sheet
                if (iter == 1000) {
                    sheet = workbook.createSheet();
                    iter = 0;
                }
                // Adding header to the first row
                if (iter == 0) {
                    myRow = sheet.createRow(iter);
                    for (int k = 1, j = 0; k <= col && j < col; k++) {
                        myCell = myRow.createCell( j);

                        myCell.setCellValue(rmeta.getColumnName(k));
                        // set style to font
                        myCell.setCellStyle(cellStyle);

                        j++;
                    }
                    iter++;
                }
                // Adding data from 2nd Row
                myRow = sheet.createRow(iter);
                for (int k = 1, j = 0; k <= col && j < col; k++) {

                    myRow.createCell( j).setCellValue(
                            rs.getString(rmeta.getColumnName(k)));
                    j++;
                }
                iter++;
            }

            workbook.write(out);

            out.close();


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

any suggestions??

Answer

Gagravarr picture Gagravarr · Aug 11, 2015

Your only option is to switch file formats. There's a hard limit in both the .xls and .xlsx file formats of 32,767 characters. Apache POI is simply enforcing the file format + Excel limit. You can see details of those limits in the Microsoft documentation, and also captured nicely in this Apache POI javadoc page

If you really need text that long, you'll need to switch to another file format such as CSV