使用NPOI匯出Excel檔案

2021-08-14 10:19:21 字數 3654 閱讀 4431

使用npoi匯出excel檔案,本例項使用了asp.net mvc。

1、使用npoi匯出excel檔案

例項:匯出商品列表。

要求:1、通過npoi匯出匯出商品列表資訊;

2、使用excel函式計算商品總金額;

在controllers控制器目錄中建立exportcontroller.cs控制器

using system.io;  

using npoi;

using npoi.poifs;

using npoi.hssf;

using npoi.util;

using npoi.hssf.usermodel;

using npoi.hpsf;

using npoi.ss.usermodel;

using npoi.ss.util;

using npoi.hssf.util;

/// /// 匯出商品列表

///

public fileresult exportproduct()

*$e", rowindex+1));

rowcontent.getcell(5).cellstyle = style;

rowindex++;

xh++;

} //輸出

system.io.memorystream ms = new system.io.memorystream();

workbook.write(ms);

ms.seek(0, seekorigin.begin);

}

2、基於.xls模板匯出excel檔案例項:基於.xls模板匯出訂單資訊(如圖 1)

要求:1、使用基於.xls模板匯出訂單資訊;

2、使用excel函式計算訂單和商品的總金額;

圖 1 基於.xls模板匯出訂單資訊

[csharp]view plain

copy

/// 

/// 匯出訂單資訊

/// 

public

fileresult exportorder()  

*$e"

, rowindex + 1));  

//設定函式,計算總金額

rowindex++;  

xh++;  

}  }  

//輸出 

system.io.memorystream ms = new

system.io.memorystream();  

workbook.write(ms);  

ms.seek(0, seekorigin.begin);  

return

file(ms, 

, "訂單資訊.xls"

);  

}  

3、其他**

3.1 實體類

在models模型目錄中建立productmodel.cs和ordermodel.cs實體類

[csharp]view plain

copy

/// 

/// 商品資訊實體類

/// 

public

class

productmodel  

/// 

/// 商品品牌

/// 

public

string

productbrand   

/// 

/// 商品**

/// 

public

decimal

productprice   

/// 

/// 數量

/// 

public

intquantity   

}  

[csharp]view plain

copy

/// 

/// 訂單資訊實體類

/// 

public

class

ordermodel  

/// 

/// 下單時間

/// 

public

datetime ordertime   

/// 

/// 總金額

/// 

public

decimal

amount   

/// 

/// 客戶名稱

/// 

public

string

customname   

/// 

/// 客戶位址

/// 

public

string

customaddress   

/// 

/// 商品列表

/// 

public

listproductlist   

}  

3.2 獲取商品和訂單資料

[csharp]view plain

copy

/// 

/// 獲取商品列表

/// 

public

listgetproductlist()  

;  productmodel product2 = new

productmodel()  

;  productmodel product3 = new

productmodel()  

;  productlist.add(product1);  

productlist.add(product2);  

productlist.add(product3);  

return

productlist;  

}  /// 

/// 獲取訂單資訊

/// 

public

ordermodel getorderinfo()  

;  return

order;  

}  

3.3 檢視

在views檢視目錄中建立index.cshtml

[html]view plain

copy

<

h2>

使用npoi生成excel檔案

h2>

<

ahref

="@url.action("exportproduct","export

")">

匯出商品

a>

<

ahref

="@url.action("exportorder","export

")">

匯出訂單

a>

使用NPOI匯出Excel檔案

using comm.library.xml using npoi.hssf.usermodel using npoi.hssf.util using npoi.ss.usermodel using npoi.xssf.usermodel using system using system.coll...

使用NPOI匯出Excel檔案

使用npoi匯出excel檔案,本例項使用了asp.net mvc。1 使用npoi匯出excel檔案 例項 匯出商品列表。要求 1 通過npoi匯出匯出商品列表資訊 2 使用excel函式計算商品總金額 在controllers控制器目錄中建立exportcontroller.cs控制器 csha...

C ,使用NPOI,匯出excel檔案

匯出excel檔案 table表資料 存放路徑 string path system.environment.currentdirectory public void exportwps excel datatable dt,string path endregion region 資料填充 int...