WebAPI导出Excel
程序员文章站
2023-02-15 21:26:27
.NET WebAPI 生成Excel并下载 由于项目上的需求,需要导出指定条件的Excel文件。经过一翻折腾终于实现了。 现在把代码贴出来分享 (直接把我们项目里面的一部份辅助类的代码分享一下) 我们项目使用的是WebAPI模式。 但是导出的方法可写在Controller 里面 1 using N ......
由于项目上的需求,需要导出指定条件的Excel文件。经过一翻折腾终于实现了。
现在把代码贴出来分享
(直接把我们项目里面的一部份辅助类的代码分享一下)
我们项目使用的是WebAPI模式。
但是导出的方法可写在Controller 里面
1 using NPOI.HSSF.UserModel; 2 using NPOI.SS.UserModel; 3 using System; 4 using System.Collections.Generic; 5 using System.Configuration; 6 using System.IO; 7 using System.Linq; 8 using System.Web; 9 using System.Web.Mvc; 10 using WebAPI.Models; 11 12 namespace WebAPI.Controllers 13 { 14 public class QRCodeController : Controller 15 { 16 17 18 19 public FileResult RequestFormDMExcel(CC_QRCodeScanInfo info) 20 { 21 string UserWXName = info.UserWXName; 22 string InstrumentName = info.InstrumentName; 23 string InstrumentSN = info.InstrumentSN; 24 var ctx = new WXEntities(); 25 List<CC_QRCodeScanInfo> List = ctx.CC_QRCodeScanInfo.OrderByDescending(p => p.ID).ToList(); 26 if (!string.IsNullOrEmpty(UserWXName)) 27 { 28 List = List.Where(p => p.UserWXName.Contains(UserWXName)).ToList(); 29 } 30 if (!string.IsNullOrEmpty(InstrumentName)) 31 { 32 List = List.Where(p => p.InstrumentName.Contains(InstrumentName)).ToList(); 33 } 34 if (!string.IsNullOrEmpty(InstrumentSN)) 35 { 36 List = List.Where(p => p.InstrumentSN.Contains(InstrumentSN)).ToList(); 37 } 38 39 //创建Excel文件的对象 40 IWorkbook book = new HSSFWorkbook(); 41 //添加一个sheet 42 ISheet sheet1 = book.CreateSheet("Sheet1"); 43 44 //给sheet1添加第一行的头部标题 45 IRow row1 = sheet1.CreateRow(0); 46 row1.CreateCell(0, CellType.STRING).SetCellValue("编号"); 47 row1.CreateCell(1, CellType.STRING).SetCellValue("用户账户"); 48 row1.CreateCell(2, CellType.STRING).SetCellValue("用户名"); 49 row1.CreateCell(3, CellType.STRING).SetCellValue("微信名"); 50 row1.CreateCell(4, CellType.STRING).SetCellValue("维度"); 51 row1.CreateCell(5, CellType.STRING).SetCellValue("经度"); 52 row1.CreateCell(6, CellType.STRING).SetCellValue("仪器名称"); 53 row1.CreateCell(7, CellType.STRING).SetCellValue("仪器序列号"); 54 row1.CreateCell(8, CellType.STRING).SetCellValue("扫码时间"); 55 56 //将数据逐步写入sheet1各个行 57 for (int i = 0; i < List.Count; i++) 58 { 59 IRow row3 = sheet1.CreateRow(i + 1); 60 row3.CreateCell(0, CellType.STRING).SetCellValue(List[i].ID.ToString()); 61 row3.CreateCell(1, CellType.STRING).SetCellValue(List[i].UserAccount.ToString()); 62 row3.CreateCell(2, CellType.STRING).SetCellValue(List[i].UserName.ToString()); 63 row3.CreateCell(3, CellType.NUMERIC).SetCellValue(List[i].UserWXName.ToString()); 64 row3.CreateCell(4, CellType.NUMERIC).SetCellValue(List[i].Lat.ToString()); 65 row3.CreateCell(5, CellType.NUMERIC).SetCellValue(List[i].Lng.ToString()); 66 row3.CreateCell(6, CellType.NUMERIC).SetCellValue(List[i].InstrumentName.ToString()); 67 row3.CreateCell(7, CellType.NUMERIC).SetCellValue(List[i].InstrumentSN.ToString()); 68 row3.CreateCell(8, CellType.NUMERIC).SetCellValue(Convert.ToDateTime(List[i].CreateTime).ToString("yyyy-MM-dd HH:mm:ss")); 69 70 } 71 string FileName = "二维码张贴统计.xls"; 72 73 // 写入到客户端 74 MemoryStream ms = new MemoryStream(); 75 book.Write(ms); 76 ms.Seek(0, SeekOrigin.Begin); 77 return File(ms, "application/vnd.ms-excel", FileName); 78 } 79 80 } 81 }
前段js代码,点击导出
$("#a_href").on("click",function () { window.location.href = "" + $("#Url").val() + "/QRCode/RequestFormDMExcel?UserWXName=" + vm.UserWXName() + "&InstrumentName=" + vm.InstrumentName() + "&InstrumentSN=" + vm.InstrumentSN(); })
或者前端代码不用改变,后端写在APIController 里面
具体代码参考
https://www.cnblogs.com/zhuyapeng/p/5008766.html
上一篇: 已无人问津!运营9年后 Intel关闭处理器超频质保计划
下一篇: 学生时代的那些搞笑事
推荐阅读
-
MATLAB怎么读取excel文件中的数据?
-
outlook怎么导入联系人?导入excel表格中的联系人的教程
-
Android Studio怎么导出设置?
-
会声会影x10输出视频太大怎么办?会声会影x10导出视频过大的解决方法
-
Matlab导出eps或jpg图片的四种方法
-
eclipse导出签名apk文件时出错该怎么办?
-
使用 Excel Services ,结合 Analysis Services 在 SharePoint 中发布报表
-
华为mate30如何导出联系人到SIM卡的方法
-
Excel Services OverView系列2 使用Excel Web Access技术在线浏览Excel工作薄
-
图解SSIS批量导入Excel文件的实现方法