c npoi分批往excel追加資料

2022-01-11 06:50:42 字數 2421 閱讀 9697

直接貼**:

using

dongyang.core.model.domain;

using

dongyang.core.utils;

using

nlog;

using

npoi.xssf.usermodel;

using

system;

using

system.collections.generic;

using

system.data;

using

system.diagnostics;

using

system.io;

using

system.threading;

namespace

dongyang.core.service

.xlsx";

//獲取查詢日期

this.gettime(currenttime, out strbegintime, out

strendtime);

//查詢資料總數

int count = this

.getdatatablecount(strbegintime, strendtime);

if (count == 0) return

;

//先拷貝空檔案

file.copy(templatefilename, reportfilename);

//分頁查詢資料,在迴圈裡面,開啟拷貝的檔案並追加資料,最後關閉檔案控制代碼

var pages = math.ceiling(convert.todouble(count) /confighelper.pagesize);

for (int pageindex = 1; pageindex <= pages; pageindex++)

}xssfsheet.forceformularecalculation = true

;

//將記憶體資料寫到檔案

using (filestream fs =file.openwrite(reportfilename))

thread.sleep(

100);}}

catch

(exception ex)

,stacktrace:");

}finally

sw.stop();

_logger.info($

"日期:,耗時秒");

}#region 獲取開始時間和結束時間

//////獲取開始時間和結束時間

/// ///

//////

private

void gettime(datetime currenttime, out

string strbegintime, out

string

strendtime)

else

strbegintime = begintime.tostring("

yyyy-mm-dd hh:mm");

strendtime = endtime.tostring("

yyyy-mm-dd hh:mm");

}#endregion

#region 查詢資料

//////查詢資料

/// ///

//////

//////

private datatable getdatatable(string strbegintime, string strendtime, int startrow, int

endrow)

where e between '' and ''

) as t

where rownumber between and ";

return

dt; }

#endregion

#region 查詢總數

//////查詢總數

/// ///

//////

private

int getdatatablecount(string strbegintime, string

strendtime)

where e between '' and ''";

var count = convert.toint32(dt.rows[0][0

]);

return

count;

}#endregion

}}

一開始是一次性讀取符合條件的資料,因為資料量大,大概七八千條,而且每條記錄400個字段。所以後面優化成了分頁讀取,每次取100條資料,然後再往excel插入資料。

C NPOI匯出Excel橫向縱向顯示

複製 datatable匯出excel 縱向匯出 資料來源 儲存的路徑 表名 public static void excel2 this datatable datatable,string filepath,string documentname string filedocmentname f...

c NPOI匯出2007版本excel

2003和2007版本區別 hssfworkbook 2003 iworkbook 2007版本 寫完之後會有個問題,匯出會報錯 流已關閉 npoi生產.xlsx檔案件時,在使用book.write ms 後,會關閉流,這樣導致再次使用respons輸出流的時候就出錯了。我看到一些提供的解決辦法是 ...

Python 往json檔案中追加內容

往json檔案中追加內容 已存在的json檔案內容如下 需求 想要追加json內容 例如 追加後如下 若直接採用指定檔案開啟方式為追加模式 a表示追加 則會變成如下效果 import os from os.path import join as pjoin import json name emb ...