Arno_Wang 2014-01-15 07:44
浏览 6737

NPOI写入大数据到excel中,如何降低使用的内存,解决内存占用过大的问题

SystemManage.q_station_log qStationLogsys = new SystemManage.q_station_log();
//int allcount = qStationLogsys.GetAllCount();
int allcount = 100000;
#region
string filename = "xxxx.xls";
string path = "/exceldata/" + filename;
string FilePathName = HttpContext.Current.Server.MapPath(path);
System.IO.File.Delete(FilePathName);
FileStream Fs = new FileStream(FilePathName, FileMode.Create);
IWorkbook workbook = new HSSFWorkbook();
int sheetindex = 1;
ISheet sheet = workbook.CreateSheet("￈xx" + sheetindex.ToString() + "");
int pagesize = 1000;
int currentIndex = 1;
int sheetcount = 0;
#endregion
int fornum = (allcount - allcount % pagesize) / pagesize + 1;
while (currentIndex <= fornum)
{
DataSet ds = qStationLogsys.GetAllLogPart(pagesize, currentIndex);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (sheetcount % 65535 == 0)
{
if (sheetindex != 1)
{
workbook.Write(Fs);
//workbook.Clear();
Fs.Flush();
Fs.Close();
Fs.Dispose();
Fs = new FileStream(FilePathName, FileMode.Open);
sheetcount = 0;
sheet = workbook.CreateSheet("￈xxx" + sheetindex.ToString() + "");

                        }
                        creat_title(sheet);//写入头的内容
                        sheetcount++;
                        sheetindex++;
                        creat_content(sheet, sheetcount, ds.Tables[0].Rows[i]);

//写入内容
}
else
{
creat_content(sheet, sheetcount, ds.Tables[0].Rows[i]);
sheetcount++;
}

                }
            }
            currentIndex++;
            ds.Clear();
        }

        workbook.Write(Fs);
        workbook.Clear();
        Fs.Flush();
        Fs.Close();
        Fs.Dispose();

//-------------------------------------------------
说明:代码如上,实现写入100000条数据,每次从数据库读取1000条,循环写入,当数据超过65535时,会新建一个工作簿。功能已经实现,但是每次写入内容占用过高,有什么方法可以改进,请教下,感激。。。。。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示