mvc4 用NPOI匯出Excel

來源:互聯網
上載者:User

標籤:io   ar   os   使用   for   on   檔案   資料   cti   

第一次做資料匯出Excel,剛看到這個的時候,首先想到的就是建立一個excel的檔案,然後將它下載,後來詢問別人,他們都推薦使用NPOI,發現這個很好用,就用部落格記錄一下

1、引用NPOI包

2、匯出Excel

/// <summary>
/// 匯出Excel
/// </summary>
/// <param name="collection"></param>
/// <returns></returns>
public FileResult DownExcel(FormCollection collection)
{
var supplierName = collection["Supplier_SelectSupplierName"];//頁面查詢條件
var goodsName = collection["Supplier_SelectGoodsName"];//頁面查詢條件
var list = ServiceClients.SupplierClient.SelectBandingSupplier(_shopid.ShopID, goodsName, supplierName);//資料來源
var wb = new NPOI.HSSF.UserModel.HSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet = wb.CreateSheet("Sheet1");//建立sheet
NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);//先建立標題那一行
row.CreateCell(0).SetCellValue("商品名稱");//建立儲存格
row.CreateCell(1).SetCellValue("供應商");
row.CreateCell(2).SetCellValue("價格");
for (int i = 0; i < list.Data.ToList().Count; i++)//填充資料
{
NPOI.SS.UserModel.IRow rowMap = sheet.CreateRow(i + 1);
rowMap.CreateCell(0).SetCellValue(list.Data.ToList()[i].GoodsName);
rowMap.CreateCell(1).SetCellValue(list.Data.ToList()[i].SupplierName);
rowMap.CreateCell(2).SetCellValue(list.Data.ToList()[i].Price.ToString(CultureInfo.InvariantCulture));
}
var ms = new System.IO.MemoryStream();
wb.Write(ms);
ms.Seek(0, System.IO.SeekOrigin.Begin);
var date = DateTime.Now.ToString("yyyyMMddHHmmss");
return File(ms, "application/vnd.ms-excel", "外購商品綁定" + date + ".xls");//輸出
}

mvc4 用NPOI匯出Excel

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.