MVC 导出Execl 的总结几种方式 (二)

  接着上面的来,继续导出Execl 的功能

  使用FileResult 方式直接可以生产Execl ,这样我们将会写大量处理后台的代码,个人感觉不好,只是展示出来,提供参考

 

第一步:编辑控制器

 

 public FileResult ExportFile()
        {
            var list = GetList();

            var sbHtml = new StringBuilder();
            string title = "order";
            //标题
            sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>");
            sbHtml.Append("<tr>");
            sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center; font-weight:bold;' height='30' colspan='3'>{0}</td>", title);
            sbHtml.Append("</tr>");
            sbHtml.Append("</table>");
            sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0' style='font-size: 12px;'>");
            var lstTitle = new List<string> { "Year", "Week", "Date" }; //编写表头
            sbHtml.Append("<tr>");
            foreach (var item in lstTitle)
            {
                sbHtml.AppendFormat("<td style='text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>", item);
            }
            sbHtml.Append("</tr>");
            int i = 1;

            var strDataType = string.Empty;
            var strQuestion = string.Empty;

            foreach (var item in list)
            {

                sbHtml.Append("</tr>");
                sbHtml.AppendFormat("<td align='center'>{0}</td><td align='center'>{1}</td><td align='center'>{2}</td>", item.Id, item.Name,item.CreateTime.ToString("yyyy-MM-dd hh:mm:ss") );
                sbHtml.Append("</tr>");
                i++;

            }
            sbHtml.Append("</table>");

            byte[] fileContents = Encoding.Default.GetBytes(sbHtml.ToString());

            string fileName = title + ".xls";

            if (Request.Browser.Browser == "IE")
            {
                Response.Charset = "GB2312";
                Response.ContentEncoding = Encoding.GetEncoding("GB2312");
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", HttpUtility.UrlEncode(fileName, Encoding.UTF8)));
            }
            return File(fileContents, "application/ms-excel", fileName);
        }
View Code

 

第二步:前端调用

  function ExportFile() {

        window.open("/Home/ExportFile");
      
    }

 

 OK ,这样就好 控制器里面的代码也是挺简单,自己看看吧

 

转载于:https://www.cnblogs.com/lizichao1991/p/5788028.html

对于 .net mvc 导出 Excel 控制器视图全过程,可以分为以下几个步骤: 1. 创建 Excel 文件以及工作表 2. 将数据填入 Excel 工作表中 3. 保存 Excel 文件并导出 具体实现可以参考以下代码: //引入命名空间 using System.IO; using System.Text; using System.Web; using System.Web.Mvc; using NPOI.HSSF.UserModel;//注意这里的引用 using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; public class ExcelController : Controller { //导出Excel public ActionResult ExportExcel() { //模拟数据 List<Model> dataList = new List<Model>(); dataList.Add(new Model { Id = 1, Name = "张三", Age = 20 }); dataList.Add(new Model { Id = 2, Name = "李四", Age = 25 }); dataList.Add(new Model { Id = 3, Name = "王五", Age = 30 }); //创建Excel文件以及工作表 IWorkbook workbook; string filePath = Server.MapPath("~/ExportExcel/"); string fileName = "test.xlsx"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } if (System.IO.File.Exists(filePath + fileName)) { System.IO.File.Delete(filePath + fileName); } //创建新的Excel文档 workbook = new XSSFWorkbook(); //创建新的工作表 ISheet sheet = workbook.CreateSheet("Sheet1"); //创建标题行 IRow row = sheet.CreateRow(0); row.CreateCell(0).SetCellValue("编号"); row.CreateCell(1).SetCellValue("姓名"); row.CreateCell(2).SetCellValue("年龄"); //将数据填入Excel工作表中 for (int i = 0; i < dataList.Count; i++) { IRow newRow = sheet.CreateRow(i + 1); newRow.CreateCell(0).SetCellValue(dataList[i].Id.ToString()); newRow.CreateCell(1).SetCellValue(dataList[i].Name); newRow.CreateCell(2).SetCellValue(dataList[i].Age.ToString()); } //保存Excel文件并导出 FileStream outFile = new FileStream(filePath + fileName, FileMode.Create, FileAccess.Write); workbook.Write(outFile); outFile.Close(); return File(filePath + fileName, "application/vnd.ms-excel", "test.xlsx"); } } 注意,这里使用了 NPOI 这个第三方库来帮助处理 Excel 文件。要使用此库,你需要先安装它并引用它的命名空间。 至于 ".lua closure factory 完整代码" 和 "中文加密" 的问题,也属于编程类问题,可以回答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值