您的位置:首页 > 大数据

大数据插入Excel报错处理

2016-01-07 21:22 375 查看
发现问题:

最近运行程序时,发现了一个问题,就是在导出excel时,报了一下错误





分析问题:

原来是由于NPOI这个动态库导致的,然后看了下版本,发现是1.2.5。然后百度了下,发现这个版本的NPOI只支持office2003,二office2003最多支持65536行,找到问题,下面就开始处理问题

解决问题:

从NPOI考虑:继续用这个版本的动态库,只是在插入数据的时候,加个判断,如果数据条数大于65536时,就在创建一个sheet

从office考虑:考虑使用跟高的Office,发现NPOI的2.1.3支持更高版本的office

我的处理方式:

方式一:不换动态库,还是老古董

static void Main(string[] args)
{
IWorkbook wk=new HSSFWorkbook();
ISheet sheet = wk.CreateSheet("StudentK");
ISheet sheet2 = wk.CreateSheet("TeacherL");

Stopwatch sw =new Stopwatch();
sw.Start();
for (int i = 0; i < 75535; i++)
{
if (i<=65535)
{
IRow row = sheet.CreateRow(i);
row.CreateCell(0).SetCellValue("Kimisme");
row.CreateCell(1).SetCellValue(i.ToString());
row.CreateCell(2).SetCellValue(DateTime.Now.ToString("s"));
}
else if (i > 65535)
{
IRow row = sheet2.CreateRow(i-65536);
row.CreateCell(0).SetCellValue("Kimisme");
row.CreateCell(1).SetCellValue(i.ToString());
row.CreateCell(2).SetCellValue(DateTime.Now.ToString("s"));
}
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Restart();
using (FileStream writer =File.OpenWrite("老古董.xls"))
{
wk.Write(writer);
}
Console.WriteLine(sw.ElapsedMilliseconds);
Console.WriteLine("ok");
Console.ReadKey();
}


成果图





方式二:我就用新东西

static void Main(string[] args)
{
//改动的地方有两处,这是第一处
IWorkbook wk = new XSSFWorkbook();
ISheet sheet = wk.CreateSheet("FrientS");

Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 75535; i++)
{
IRow row = sheet.CreateRow(i);
row.CreateCell(0).SetCellValue("Kimisme");
row.CreateCell(1).SetCellValue(i.ToString());
row.CreateCell(2).SetCellValue(DateTime.Now.ToString("s"));
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Restart();
//这是第二处
using (FileStream writer = File.OpenWrite("新事物.xlsx"))
{
wk.Write(writer);
}
Console.WriteLine(sw.ElapsedMilliseconds);
Console.WriteLine("ok");
Console.ReadKey();
}


成果图:





最后,感谢面向对象,感谢里氏替换原则,理由如下

HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls

XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: