最近项目用的webapi前几天做了个导出excel功能,给大家分享下,自己也记录下。。。
在用的过程中,可以直接请求就可以得到下载的excel文件,在实际的项目中可以通过js打开新页面,encodeURI请求,得到下载excel
NPOI类库的引用,可以直接通过vs直接下载安装
1 WebApplication3.Controllers 2 { 3 public class SomeModel { public int id { get; set; } }//用于接收传递过来的参数 4 public class DownLoadController : ApiController 5 { 6 // GET: DownLoad 7 public string Index() 8 { 9 return ""; 10 } 11 12 //导出excel功能控制器 13 //[Authorize] 14 [HttpGet] 15 [ActionName("Export")]//控制器名称重新定义,加上可以直接请求Exprot 16 public HttpResponseMessage PostExportData(SomeModel model) 17 { 18 var file = ExcelStream(); 19 //string csv = _service.GetData(model); 20 HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); 21 result.Content = new StreamContent(file); 22 //a text file is actually an octet-stream (pdf, etc) 23 //result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); 24 25 result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel"); 26 //we used attachment to force download 27 result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); 28 result.Content.Headers.ContentDisposition.FileName = "file.xls"; 29 return result; 30 } 31 32 //得到excel文件流 33 private System.IO.Stream ExcelStream() 34 { 35 //var list = dc.v_bs_dj_bbcdd1.Where(eps).ToList(); 36 HSSFWorkbook hssfworkbook = new HSSFWorkbook(); 37 38 ISheet sheet1 = hssfworkbook.CreateSheet("保税订单"); 39 40 41 IRow rowHeader = sheet1.CreateRow(0); 42 43 //生成excel标题 44 rowHeader.CreateCell(0).SetCellValue("汇通单号"); 45 rowHeader.CreateCell(1).SetCellValue("单据日期"); 46 rowHeader.CreateCell(2).SetCellValue("订单号"); 47 rowHeader.CreateCell(3).SetCellValue("收件人"); 48 rowHeader.CreateCell(4).SetCellValue("收件人电话"); 49 rowHeader.CreateCell(5).SetCellValue("收件人地址"); 50 rowHeader.CreateCell(6).SetCellValue("物流公司"); 51 rowHeader.CreateCell(7).SetCellValue("运单号"); 52 rowHeader.CreateCell(8).SetCellValue("数量"); 53 rowHeader.CreateCell(9).SetCellValue("状态"); 54 55 //生成excel内容 56 //for (int i = 0; i < list.Count; i++) 57 //{ 58 // NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1); 59 // rowtemp.CreateCell(0).SetCellValue(list[i].bh_user); 60 // rowtemp.CreateCell(1).SetCellValue(list[i].rq.Value.ToString("yyyy-MM-dd HH:mm:dd")); 61 // rowtemp.CreateCell(2).SetCellValue(list[i].bh_khdd); 62 // rowtemp.CreateCell(3).SetCellValue(list[i].re_name); 63 // rowtemp.CreateCell(4).SetCellValue(list[i].re_tel); 64 // rowtemp.CreateCell(5).SetCellValue(list[i].re_fulladdress); 65 // rowtemp.CreateCell(6).SetCellValue(list[i].bm_kdgs); 66 // rowtemp.CreateCell(7).SetCellValue(list[i].kddh); 67 // rowtemp.CreateCell(8).SetCellValue((int)list[i].sl_total); 68 // rowtemp.CreateCell(9).SetCellValue(list[i].mc_state_dd); 69 //} 70 71 for (int i = 0; i < 10; i++) 72 sheet1.AutoSizeColumn(i); 73 74 MemoryStream file = new MemoryStream(); 75 hssfworkbook.Write(file); 76 file.Seek(0, SeekOrigin.Begin); 77 78 return file; 79 80 //return File(file, "application/vnd.ms-excel", "保税订单.xls"); 81 } 82 83 } 84 }
时间: 2024-10-22 17:44:36