excel 导出 The maximum length of cell contents (text) is 32767 characters
导出excel功能,报错。错误日志提示::The maximum length of cell contents (text) is 32767 characters
调查后,poi会有单元格最大长度校验超过32767会报错。
需求调研:
调研发现,excel和csv文件本身存在单元格有32767的最大字符限制。
解决方案:
解决办法是程序不报错,但是生成excel或者csv 会自动截取单元格最大32767 characters。
1、通过java反射机制,设置单元格最大校验限制为Integer.MAX_VALUE(2147483647)。
导出生成excel文件之前调用设置单元格最大限制方法。
public void resetCellMaxTextLength() {
SpreadsheetVersion excel2007 = SpreadsheetVersion.EXCEL2007;
if (Integer.MAX_VALUE != excel2007.getMaxTextLength()) {
Field field;
try {
field = excel2007.getClass().getDeclaredField("_maxTextLength");
field.setAccessible(true);
field.set(excel2007,Integer.MAX_VALUE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、修改源码,修改源码文件枚举类,同样也是设置最大校验限制为Integer.MAX_VALUE
package org.apache.poi.ss;
import org.apache.poi.ss.util.CellReference;
/**
* @description :Excel枚举
*/
public enum SpreadsheetVersion {
/**
* excel 枚举
**/
EXCEL97(65536, 256, 30, 3, 4000, 32767),
EXCEL2007(1048576, 16384, 255, 2147483647, 64000, Integer.MAX_VALUE);
private final int _maxRows;
private final int _maxColumns;
private final int _maxFunctionArgs;
private final int _maxCondFormats;
private final int _maxCellStyles;
private final int _maxTextLength;
private SpreadsheetVersion(int maxRows, int maxColumns, int maxFunctionArgs, int maxCondFormats, int maxCellStyles, int maxText) {
this._maxRows = maxRows;
this._maxColumns = maxColumns;
this._maxFunctionArgs = maxFunctionArgs;
this._maxCondFormats = maxCondFormats;
this._maxCellStyles = maxCellStyles;
this._maxTextLength = maxText;
}
public int getMaxRows() {
return this._maxRows;
}
public int getLastRowIndex() {
return this._maxRows - 1;
}
public int getMaxColumns() {
return this._maxColumns;
}
public int getLastColumnIndex() {
return this._maxColumns - 1;
}
public int getMaxFunctionArgs() {
return this._maxFunctionArgs;
}
public int getMaxConditionalFormats() {
return this._maxCondFormats;
}
public int getMaxCellStyles() {
return this._maxCellStyles;
}
public String getLastColumnName() {
return CellReference.convertNumToColString(this.getLastColumnIndex());
}
public int getMaxTextLength() {
return this._maxTextLength;
}
}