当前位置: 移动技术网 > IT编程>开发语言>JavaScript > WebAPI导出Excel

WebAPI导出Excel

2018年05月17日  | 移动技术网IT编程  | 我要评论

由于项目上的需求,需要导出指定条件的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 }
View Code

前段js代码,点击导出

$("#a_href").on("click",function () {
                        window.location.href = "" + $("#Url").val() + "/QRCode/RequestFormDMExcel?UserWXName=" + vm.UserWXName() + "&InstrumentName=" + vm.InstrumentName() + "&InstrumentSN=" + vm.InstrumentSN();
                    })
View Code

或者前端代码不用改变,后端写在APIController  里面

具体代码参考

https://www.cnblogs.com/zhuyapeng/p/5008766.html 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网