触发场景描述 操作系统:mac
版本:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
</dependency>
excel版本:
使用EasyExcel的 CustomWriteHandler
生成下拉框时,下拉框大数据量会导致execl打开时报错。
业务代码中,如果是正常点的字符串,经测试二三十条数据就无法打开了。下面的例子是88条记录正常打开,但是如果下拉框有89条数据,则excel打开报错。
触发Bug的代码
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.util.CellRangeAddressList;
import java.util.Collections;
/**
* @author superlee
* @since 2020-03-06
*/
public class TestEasyExcel {
public static void main(String[] args) {
generateExcel("/Users/superlee/test1.xlsx", 88);
generateExcel("/Users/superlee/test2.xlsx", 89);
}
private static void generateExcel(String fileName, int count) {
String[] array1 = new String[count];
for (int i = 0; i < count; i++) {
array1[i] = i + "";
}
TestEasyExcel testEasyExcel = new TestEasyExcel();
EasyExcel
.write(fileName, Model.class)
.registerWriteHandler(testEasyExcel.new CustomWriteHandler(array1))
.sheet("sheet").doWrite(Collections.emptyList());
}
class Model {
@ExcelProperty(value = "第一列", index = 0)
private String column;
public String getColumn() {
return column;
}
public void setColumn(String column) {
this.column = column;
}
}
public class CustomWriteHandler implements SheetWriteHandler {
private String[] data;
public CustomWriteHandler(String[] data) {
this.data = data;
}
@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
DataValidationHelper helper = writeSheetHolder.getSheet().getDataValidationHelper();
CellRangeAddressList cellRangeAddressList =
new CellRangeAddressList(1, 500, 0, 0);
DataValidationConstraint constraint =
helper.createExplicitListConstraint(data);
DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
writeSheetHolder.getSheet().addValidationData(dataValidation);
}
}
}
提示的异常或者没有达到的效果
请问有没有解决方案,如何修改,谢谢。