java - 增加java中HSSFCell的最大长度

标签 java poi-hssf

实际上,我尝试使用 java 将一些数据存储在 HSSFCell 中,但我遇到了这样的错误

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)

谁能建议我增加单元格长度的方法,即超过 32767 个字符???

我使用了以下代码,但出现了上述错误

 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();
        }

    }

有什么建议吗??

最佳答案

您唯一的选择是切换文件格式。 .xls.xlsx 文件格式都有 32,767 个字符的硬性限制。 Apache POI 只是强制执行文件格式 + Excel 限制。您可以在 Microsoft 文档中查看这些限制的详细信息,也可以在 this Apache POI javadoc page 中很好地捕捉到。

如果您真的需要那么长的文本,则需要切换到另一种文件格式,例如 CSV

关于java - 增加java中HSSFCell的最大长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31935340/

相关文章:

java - 无法实例化扩展应用程序类的应用程序错误

java - 如何在发送请求时强制 JAXB 写入 xsi :type of a javax. jws.WebParam(CXF 实现)

c# - 从一个 Json 移植到 javas org.json.JSONObject,缺少键的行为不一样

java - 使用带有前导零的 POI HSSFCell 读取 Excel

java - Java源码编译失败

java - 如何在不使用 "if-else if"的情况下编写对不同参数使用不同方法的代码?

apache-poi - Apache Poi : Converting from HSSF to SS?

java - 从 ColdFusion 服务器执行 Excel 宏

java - 向 POI XSSF 工作簿中的合并区域添加边框

Java 兴趣点 : How to find an Excel cell with a string value and get its position (row) to use that position to find another cell