Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase the maximum length of HSSFCell in java

Tags:

java

poi-hssf

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??

like image 495
vineeth Avatar asked Aug 11 '15 07:08

vineeth


People also ask

How do you fix the maximum length of cell contents text is 32767 characters?

Your best option would be switching the export to CSV . That is the maximum number of characters allowed in the excel cell value. Truncate the characters if it is more than 32676.

What is getNumericCellValue?

getNumericCellValue. public double getNumericCellValue() Get the value of the cell as a number. For strings we throw an exception. For blank cells we return a 0.


1 Answers

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

like image 111
Gagravarr Avatar answered Oct 10 '22 08:10

Gagravarr