NPOI高效匯出Excel

2021-09-08 19:14:08 字數 3576 閱讀 6325

using

system.collections.generic;

using

system.data;

using

system.io;

using

system.linq;

using

npoi.hssf.usermodel;

using

npoi.ss.usermodel;

using

npoi.xssf.usermodel;

public

class

excelhelper

else

dt.columns.add(

newdatacolumn(obj.tostring()));

columns.add(i);

}//資料

for (int i = sheet.firstrownum + 1; i <= sheet.lastrownum; i++)

}if(hasvalue)}}

return

dt; }

//////

將datatable資料匯出到excel檔案中(xls)

/// ///

///public

static

void tabletoexcelforxls(datatable dt, string

file)

//資料

for (int i = 0; i < dt.rows.count; i++)

}//轉為位元組陣列

memorystream stream = new

memorystream();

hssfworkbook.write(stream);

var buf =stream.toarray();

//儲存為excel檔案

using (filestream fs = new

filestream(file, filemode.create, fileaccess.write))

}//////

獲取單元格型別(xls)

/// ///

///private

static

object

getvaluetypeforxls(hssfcell cell)

}#endregion

}

public

class

x2007

else

dt.columns.add(

newdatacolumn(obj.tostring()));

columns.add(i);

}//資料

for (int i = sheet.firstrownum + 1; i <= sheet.lastrownum; i++)

}if(hasvalue)}}

return

dt; }

//////

將datatable資料匯出到excel檔案中(xlsx)

/// ///

///public

static

void tabletoexcelforxlsx(datatable dt, string

file)

//資料

for (int i = 0; i < dt.rows.count; i++)

}//轉為位元組陣列

memorystream stream = new

memorystream();

xssfworkbook.write(stream);

var buf =stream.toarray();

//儲存為excel檔案

using (filestream fs = new

filestream(file, filemode.create, fileaccess.write))

}//////

獲取單元格型別(xlsx)

/// ///

///private

static

object

getvaluetypeforxlsx(xssfcell cell)

}#endregion

}

public

static datatable getdatatable(string

filepath)

else

return

dt; }

}

using

system;

using

system.collections.generic;

using

system.web;

using

system.web.ui;

using

system.web.ui.webcontrols;

using

system.data;

using

system.linq;

public

partial

class

_default : system.web.ui.page

protected

void btn_read_03_click(object

o, eventargs e)

protected

void btn_read_07_click(object

o, eventargs e)

protected

void btn_import_03_click(object

o, eventargs e)

excelhelper.x2003.tabletoexcelforxls(dt, path);

downloadfile(path);

}protected

void btn_import_07_click(object

o, eventargs e)

excelhelper.x2007.tabletoexcelforxlsx(dt, path);

downloadfile(path);

}void downloadfile(string

s_path)

}

如果要提高效能千萬切記,不要在內迴圈中亂用下面的設定,要不效能就是幾秒與幾分鐘的差異。

sheet.autosizecolumn(cellindex, true);

icellstyle style = workbook.createcellstyle();

style.borderbottom = borderstyle.thin;

style.borderleft = borderstyle.thin;

style.borderright = borderstyle.thin;

style.bordertop = borderstyle.thin;

cell.cellstyle = style;

npoi 單元格格式設定 

npoi自適應寬度不支援中文解決方案 

NPOI匯出EXCEL樣式

public void export datarequestrequest,dataresponseresponse endregion region 單元格列值屬性樣式 for int jj 0 jj row1.cells.count jj style endregion cell.setcell...

npoi匯入匯出excel

1.匯入 將datatable資料匯入到excel中 要匯入的資料 datatable的列名是否要匯入 要匯入的excel的sheet的名稱 匯入資料行數 包含列名那一行 public int datatabletoexcel datatable data,string sheetname,bool...

利用NPOI操作excel匯出

npoi外掛程式要.netframe4.0 開啟excel按鈕事件 private void button1 click object sender,eventargs e private void button2 click object sender,eventargs e if datearr...