欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# Excel导出超出65536行报错 Invalid row number (65536) outside allowable range (0..65535)

程序员文章站 2023-11-10 23:23:46
C# Excel导出超出65536行报错 Invalid row number (65536) outside allowable range (0..65535) ......

  c# excel导出超出65536行报错 invalid row number (65536) outside allowable range (0..65535)

一:报错

invalid row number (65536) outside allowable range (0..65535)

原因:excel 03的限制就是65536行,超过行数就报错

二:解决方案-创建多个sheet

public string export(list<record> list, string f) {
string name = "";
hssfworkbook book = new hssfworkbook();
hssfsheet sheet = book.createsheet("stud");
// 声明一行
hssfrow row = sheet.createrow(0);
row = sheet.createrow(0);
row.createcell(0).setcellvalue("身份");
row.createcell(1).setcellvalue("信用");
int index = 0;//记录额外创建的sheet数量
for (int i = 0; i < list.size(); i++) {
if ((i + 1) % 65535 == 0) {
sheet = book.createsheet("stud" + index);
row = sheet.createrow(0);
row.createcell(0).setcellvalue("身份");
row.createcell(1).setcellvalue("信用");
index++;
}
row = sheet.createrow((i + 1) - (index * 65535));
// 第四步,创建单元格,并设置值
row.createcell((short) 0).setcellvalue(list.get(i).getstr("info"));
row.createcell((short) 1).setcellvalue(list.get(i).getstr("score"));
}
// 第六步,将文件存到指定位置
string filename = "";
try {
filename = f + "/download/导出.xls";
name = "导出.xls";
file file = new file(filename);
if (file.exists()) {
file.delete();
}
fileoutputstream fout = new fileoutputstream(filename);
book.write(fout);
fout.close();
} catch (exception e) {
e.printstacktrace();
}
return name;

}
————————————————
版权声明:本文为csdn博主「m0_37934074」的原创文章,遵循 cc 4.0 by-sa 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37934074/article/details/79409292