最近项目用的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 }

webapi到处excel的更多相关文章

  1. Angularjs 通过WebApi 下载excel

    如果想知道 AngularJs 通过WebAPI 下载Excel.请看下文,这里仅提供了一种方案. 服务器端代码如下: protected HttpResponseMessage GenereateE ...

  2. Ionic2+WebApi 导出Excel转Pdf文件。

    步骤: 1.首先在WebApi中先把excel生成好. 2.把excel转成Pdf,并返回下载的链接. 3.Ionic2的页面做好下载的接口. 嗯~思路很清晰,那么下面就来详细的操作吧. 以下是H5的 ...

  3. webApi 导入Excel

    /// <summary> /// 导入 /// </summary> /// <param name="organizationId">< ...

  4. js到处excel

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. silverlight中datagrid数据到处excel

    首先新建一个DataGrdiExtensions类,代码为: public static class DataGridExtensions { /// <summary> /// 导出dg ...

  6. webapi 导入excel处理数据

    参考资料     https://blog.csdn.net/pan_junbiao/article/details/82935992 https://www.cnblogs.com/dansedia ...

  7. 注解到处excel

    package com.cxy.domain.poi; import java.lang.annotation.ElementType; import java.lang.annotation.Ret ...

  8. c#使用Microsoft Excel 12.0 object Libary导出的Excel文件office2003不能打开!!~~(分享)

    -----转载:http://hi.baidu.com/zhang_zhu_1/item/f3d47d1f86bf037a70d5e87e 使用C#导出数据到Excel文件时,Excel 2007组件 ...

  9. PowerDesigner 中模型设计导出Excel表格

    今天项目做设计,客户要看数据设计,需要到处Excel表格.去网上搜索下,把使用总结如下: 已经完成设计的pd设计 打开pd,快捷键Ctrl + Shift + X或者Tools>Exectue ...

随机推荐

  1. npm proxy设置网络代理 并使用taobao registry

    npm config set https-proxy http://server:portnpm config set proxy http://server:port npm set registr ...

  2. (第八场)G Counting regions 【欧拉公式】

    题目链接:https://www.nowcoder.com/acm/contest/146/G G.Counting regions | 时间限制:1 秒 | 内存限制:128M Niuniu lik ...

  3. 2018.12.21 如何在现有的Eclipse(4.9.0版本)中安装STS (Spring Tool Suite ) Mac环境下

    在Eclipse中安装STS正确步骤实现方式 1.简介说明       spring Tool Suite(sts)就是一个基于Eclipse的开发环境, 用于开发Spring应用程序.它提供了一个现 ...

  4. SQL Error: 1064, SQLState: 42000 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

    -- ::, WARN [org.hibernate.util.JDBCExceptionReporter:] - SQL Error: , SQLState: -- ::, ERROR [org.h ...

  5. Introduction to CQRS

    原文链接:  http://www.codeproject.com/Articles/555855/Introduction-to-CQRS What is CQRS CQRS means Comma ...

  6. Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)

    原文:http://www.asp.net/web-api/overview/advanced/dependency-injection 1 什么是依赖注入(Dependency Injection) ...

  7. JavaFXML实现新窗口打开

    实现原理顺着往下看就明白了,流程看红色字体.具体还有什么问题可以留言. 主页面配置文件,一共三个按钮.这里说明第一个按钮触发打开新窗口 <?xml version="1.0" ...

  8. Spring-Day02-依赖注入-作业

    配置beans约束自动提示 spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html中打开xsd-configuratio ...

  9. 一个logstash引发的连环案,关于logstash提示:Reached open files limit: 4095, set by the 'max_open_files' option or default, files yet to open: 375248

    不多说,直接上问题.版本logstash-2.4.0,启动后提示错误: !!! Please upgrade your java version, the current version '1.7.0 ...

  10. c# 调用服务返回结果模板化

    一般我们返回一个结果,主要有返回值,执行结果信息,所以定义一个类 public  class QuestResult    { /// <summary>        /// 返回值  ...