C web 利用NPOI進行生成excel

2021-09-18 02:24:33 字數 3765 閱讀 1616

第一步:引用npoi.dll,命名空間引用

using npoi.hssf.usermodel;

using npoi.ss.usermodel;

using npoi.ss.util;

第二步:傳入查詢的到的

@1單純的以行匯出的格式datatable,作為引數,得到資料流

/// /// 資料匯出到excel

///

/// 資料表集合

/// 輸出流

public static memorystream excelout(listdts)

hssfsheet sheet = (hssfsheet)wb.createsheet(dts[i].tablename);

sheet.defaultcolumnwidth = 15;

sheet.defaultrowheight = 22 * 20;

hssfrow row = (hssfrow)sheet.createrow(0);

hssfcellstyle cellstyle = (hssfcellstyle)wb.createcellstyle();

cellstyle.alignment = horizontalalignment.center;

cellstyle.verticalalignment = verticalalignment.center;

cellstyle.bordertop = borderstyle.medium;

cellstyle.borderright = borderstyle.medium;

cellstyle.borderbottom = borderstyle.medium;

cellstyle.borderleft = borderstyle.medium;

hssffont font = (hssffont)wb.createfont();

font.isbold = true;

cellstyle.setfont(font);

for (int j = 0; j < dts[i].columns.count; j++)

hssfcellstyle cellstyle2 = (hssfcellstyle)wb.createcellstyle();

cellstyle2.verticalalignment = verticalalignment.center;

cellstyle2.bordertop = borderstyle.thin;

cellstyle2.borderright = borderstyle.thin;

cellstyle2.borderbottom = borderstyle.thin;

cellstyle2.borderleft = borderstyle.thin;

for (int k = 0; k < dts[i].rows.count; k++)}}

memorystream ms = new memorystream();

wb.write(ms);

return ms;

}

@2:一對多的關係,如:

/// /// 複雜資料匯出到excel

///

/// 資料表集合

/// 輸出流

public static memorystream excelout2(listdts)

sheet.defaultcolumnwidth = 15;

sheet.defaultrowheight = 22 * 20;

hssfrow row = (hssfrow)sheet.createrow(0);

hssfcellstyle cellstyle = (hssfcellstyle)wb.createcellstyle();

cellstyle.alignment = horizontalalignment.center;

cellstyle.verticalalignment = verticalalignment.center;

cellstyle.bordertop = borderstyle.medium;

cellstyle.borderright = borderstyle.medium;

cellstyle.borderbottom = borderstyle.medium;

cellstyle.borderleft = borderstyle.medium;

hssffont font = (hssffont)wb.createfont();

font.isbold = true;

cellstyle.setfont(font);

for (int j = 0; j < dts[i].columns.count; j++)

hssfcellstyle cellstyle2 = (hssfcellstyle)wb.createcellstyle();

cellstyle2.verticalalignment = verticalalignment.center;

cellstyle2.bordertop = borderstyle.thin;

cellstyle2.borderright = borderstyle.thin;

cellstyle2.borderbottom = borderstyle.thin;

cellstyle2.borderleft = borderstyle.thin;

for (int k = 0; k < dts[i].rows.count; k++)}}

#endregion

#region 合併行

datatable dt = new datatable();

dt = dts[0];

//合併行

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

else if(value==value1 && j == dt.rows.count)

}sheet.addmergedregion(new cellrangeaddress(i, end, 0, 0));

sheet.addmergedregion(new cellrangeaddress(i, end, 1, 1));

i = end;

}

#endregion

memorystream ms = new memorystream();

wb.write(ms);

return ms;}}

第三步:把資料流返回你呼叫的方法,並輸出頁面。

// 設定編碼和附件格式

利用NPOI生成word文件 c

wordtest.aspx.cs using system using system.io using system.text using system.web using system.web.ui using npoi.openxmlformats.wordprocessing using np...

C web生成EXCEL檔案

response.clearcontent response.addheader content disposition attachment filename test.xls stringwriter swr new stringwriter for int i 0 i 10 i t3 twew...

C 利用NPOI以模板形式生成EXCEL

在開發中,有時候需要生成的excel樣子比較複雜,如果用 形式去控制excel比較繁瑣費時,所以事先做好需要生成excel的模板,再生成的時候往裡面填充資料這樣子比較簡便。如下 if file.exists templatexlspath int i 4,j 4 using filestream f...