asp.net Mvc 使用NPOI导出Excel文件
生活随笔
收集整理的這篇文章主要介紹了
asp.net Mvc 使用NPOI导出Excel文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.新建MVC項(xiàng)目,新建控制器、視圖
?添加控制器:
?添加視圖(將使用布局頁前面的復(fù)選框里的勾勾去掉)
2.在Models里新建一個(gè)類
public class Shop{/// <summary>/// 編號(hào)/// </summary>public int Number { get; set; }/// <summary>/// 商品名稱/// </summary>public string ShopName { get; set; }/// <summary>/// 商品價(jià)格/// </summary>public double Price { get; set; }/// <summary>/// 商品種類/// </summary>public string ShopType { get; set; }/// <summary>/// 時(shí)間/// </summary>public DateTime Date { get; set; }/// <summary>/// 添加數(shù)據(jù)/// </summary>/// <returns></returns>public List<Shop> AddShop(){var shops = new List<Shop>();shops.Add(new Shop() { Number = 1001, ShopName = "小熊餅干", Price = 9.9, ShopType = "零食", Date = DateTime.Now });shops.Add(new Shop() { Number = 1002, ShopName = "旺仔QQ糖", Price = 5.6, ShopType = "零食", Date = DateTime.Now });shops.Add(new Shop() { Number = 1001, ShopName = "奧利奧餅干", Price = 15.5, ShopType = "零食", Date = DateTime.Now });return shops;}}3.在控制器里新增一個(gè)方法,編寫導(dǎo)出的代碼
public FileResult ExportData(){Shop shop = new Shop();//獲得數(shù)據(jù)var Data=shop.AddShop();//創(chuàng)建一個(gè)新的excel文件HSSFWorkbook book = new HSSFWorkbook();//創(chuàng)建一個(gè)工作區(qū)ISheet sheet = book.CreateSheet("sheet1");//創(chuàng)建一行 也就是在sheet1這個(gè)工作區(qū)創(chuàng)建一行 在NPOI中只有先創(chuàng)建才能后使用IRow row = sheet.CreateRow(0);for (int i = 0; i < 5; i++){//設(shè)置單元格的寬度sheet.SetColumnWidth(i, 16 * 156);}sheet.SetColumnWidth(4, 30 * 156);sheet.SetColumnWidth(1, 21 * 156);//定義一個(gè)樣式,迎來設(shè)置樣式屬性ICellStyle setborder = book.CreateCellStyle();//設(shè)置單元格上下左右邊框線 但是不包括最外面的一層setborder.BorderLeft = BorderStyle.Thin;setborder.BorderRight = BorderStyle.Thin;setborder.BorderBottom = BorderStyle.Thin;setborder.BorderTop = BorderStyle.Thin;//文字水平和垂直對齊方式setborder.VerticalAlignment = VerticalAlignment.Center;//垂直居中setborder.Alignment = HorizontalAlignment.Center;//水平居中setborder.WrapText = true;//自動(dòng)換行//再定義一個(gè)樣式,用來設(shè)置最上面標(biāo)題行的樣式ICellStyle setborderdeth = book.CreateCellStyle();//設(shè)置單元格上下左右邊框線 但是不包括最外面的一層setborderdeth.BorderLeft = BorderStyle.Thin;setborderdeth.BorderRight = BorderStyle.Thin;setborderdeth.BorderBottom = BorderStyle.Thin;setborderdeth.BorderTop = BorderStyle.Thin;//定義一個(gè)字體樣式IFont font = book.CreateFont();//將字體設(shè)為紅色font.Color = IndexedColors.Red.Index;//font.FontHeightInPoints = 17;//將定義的font樣式給到setborderdeth樣式中 setborderdeth.SetFont(font);//文字水平和垂直對齊方式setborderdeth.VerticalAlignment = VerticalAlignment.Center;//垂直居中setborderdeth.Alignment = HorizontalAlignment.Center;//水平居中setborderdeth.WrapText = true; //自動(dòng)換行//設(shè)置第一行單元格的高度為25row.HeightInPoints = 25;//設(shè)置單元格的值row.CreateCell(0).SetCellValue("編號(hào)");//將style屬性給到這個(gè)單元格row.GetCell(0).CellStyle = setborderdeth;row.CreateCell(1).SetCellValue("商品名稱");row.GetCell(1).CellStyle = setborderdeth;row.CreateCell(2).SetCellValue("商品價(jià)格");row.GetCell(2).CellStyle = setborderdeth;row.CreateCell(3).SetCellValue("商品種類");row.GetCell(3).CellStyle = setborderdeth;row.CreateCell(4).SetCellValue("日期");row.GetCell(4).CellStyle = setborderdeth;//循環(huán)的導(dǎo)出到excel的每一行for (int i = 0; i < Data.Count; i++){//每循環(huán)一次,就新增一行 索引從0開始 所以第一次循環(huán)CreateRow(1) 前面已經(jīng)創(chuàng)建了標(biāo)題行為0IRow row1 = sheet.CreateRow(i + 1);row1.HeightInPoints = 21;//給新加的這一行創(chuàng)建第一個(gè)單元格,并且給這第一個(gè)單元格設(shè)置值 以此類推...row1.CreateCell(0).SetCellValue(Convert.ToString(Data[i].Number));//先獲取這一行的第一個(gè)單元格,再給其設(shè)置樣式屬性 以此類推...row1.GetCell(0).CellStyle = setborder;row1.CreateCell(1).SetCellValue(Data[i].ShopName);row1.GetCell(1).CellStyle = setborder;row1.CreateCell(2).SetCellValue(Convert.ToString(Data[i].Price));row1.GetCell(2).CellStyle = setborder;row1.CreateCell(3).SetCellValue(Data[i].ShopType);row1.GetCell(3).CellStyle = setborder;row1.CreateCell(4).SetCellValue(Convert.ToString(Data[i].Date));row1.GetCell(4).CellStyle = setborder;}System.IO.MemoryStream ms = new System.IO.MemoryStream();book.Write(ms);ms.Seek(0, SeekOrigin.Begin);DateTime dttime = DateTime.Now;string datetime = dttime.ToString("yyyy-MM-dd");string filename = "報(bào)表導(dǎo)出" + datetime + ".xls";return File(ms, "application/vns.ms-excel", filename);}4.視圖頁引入腳本文件及編寫相應(yīng)代碼
@{Layout = null; }<!DOCTYPE html><html> <head><meta name="viewport" content="width=device-width" /><title>Index</title><link href="http://localhost:64014/bootstrap/css/bootstrap.min.css" rel="stylesheet" /><script src="http://localhost:64014/js/jquery-3.4.0.min.js"></script><script src="http://localhost:64014/bootstrap/js/bootstrap.min.js"></script><style type="text/css">* {margin: 0px;padding: 0px;}button {margin-top: 5px;margin-left: 5px;}</style><script type="text/javascript">function exports() {//用ajax調(diào)用導(dǎo)出方法 $.ajax({type: 'GET',url: '/Export/ExportData',success: function () {//注:Ajax直接調(diào)用后臺(tái)的下載方法是導(dǎo)出不了文件的,原因是ajax無法接收后臺(tái)的文件流,所以,需要再次用window.location=url或者window.open(url)下載window.location = '/Export/ExportData'}})}</script> </head> <body><div>@*按鈕用bootstrap渲染一下,會(huì)好看一些(*^_^*)*@<button class="btn btn-info" style="width:90px" οnclick="exports()">導(dǎo)出</button></div> </body> </html>5.效果:
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhangnever/p/11074097.html
總結(jié)
以上是生活随笔為你收集整理的asp.net Mvc 使用NPOI导出Excel文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 异常-自定义异常 和 throw和thr
- 下一篇: C#中的依赖注入那些事儿