类1:

using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
using System.IO;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using System.Drawing;

namespace Base.ActionResult
{
    public class ExcelResult : System.Web.Mvc.ActionResult
    {
        private DataTable _dataContext;
        private string _fileName;
        private string[] _headers = null;
        private TableStyle _tableStyle;
        private TableItemStyle _headerStyle;
        private TableItemStyle _itemStyle;

public string FileName
        {
            get { return _fileName; }
        }

public ExcelResult(DataTable dataContext, string fileName)
            : this(dataContext, fileName, null, null, null, null)
        {
        }

public ExcelResult(DataTable dataContext, string fileName, string[] headers)
            : this(dataContext, fileName, headers, null, null, null)
        {
        }

public ExcelResult(DataTable dataContext, string fileName, string[] headers, TableStyle tableStyle, TableItemStyle headerStyle, TableItemStyle itemStyle)
        {
            _dataContext = dataContext;
            _fileName = fileName;
            _headers = headers;
            _tableStyle = tableStyle;
            _headerStyle = headerStyle;
            _itemStyle = itemStyle;

// provide defaults
            if (_tableStyle == null)
            {
                _tableStyle = new TableStyle();
                _tableStyle.BorderStyle = BorderStyle.Solid;
                _tableStyle.BorderColor = Color.Black;
                _tableStyle.BorderWidth = Unit.Parse("2px");
            }

if (_headerStyle == null)
            {
                _headerStyle = new TableItemStyle();
                _headerStyle.BackColor = Color.LightGray;
            }
        }

public override void ExecuteResult(ControllerContext context)
        {
            // Create HtmlTextWriter
            StringWriter sw = new StringWriter();
            HtmlTextWriter tw = new HtmlTextWriter(sw);

// Build HTML Table from Items
            if (_tableStyle != null)
                _tableStyle.AddAttributesToRender(tw);

tw.RenderBeginTag(HtmlTextWriterTag.Table);

// Generate headers from table
            if (_headers == null)
            {
                List<string> lst = new List<string>();
                for (int i = 0; i < _dataContext.Columns.Count; i++)
                {
                    lst.Add(_dataContext.Columns[i].ColumnName);
                }
                _headers = lst.ToArray();
            }

// Create Header Row
            tw.RenderBeginTag(HtmlTextWriterTag.Thead);

foreach (string header in _headers)
            {
                if (_headerStyle != null)
                    _headerStyle.AddAttributesToRender(tw);

tw.RenderBeginTag(HtmlTextWriterTag.Th);
                tw.Write(header);
                tw.RenderEndTag();
            }

tw.RenderEndTag();

// Create Data Rows
            tw.RenderBeginTag(HtmlTextWriterTag.Tbody);

foreach (DataRow dr in _dataContext.Rows)
            {
                tw.RenderBeginTag(HtmlTextWriterTag.Tr);

foreach (string header in _headers)
                {
                    string strValue = dr[header].ToString();
                    strValue = ReplaceSpecialCharacters(strValue);

if (_itemStyle != null)
                        _itemStyle.AddAttributesToRender(tw);

tw.RenderBeginTag(HtmlTextWriterTag.Td);
                    tw.Write(HttpUtility.HtmlEncode(strValue));
                    tw.RenderEndTag();
                }

tw.RenderEndTag();
            }

tw.RenderEndTag(); // tbody
            tw.RenderEndTag(); // table
            WriteFile(_fileName, "application/ms-excel", sw.ToString());
        }

private static string ReplaceSpecialCharacters(string value)
        {
            value = value.Replace("’", "'");
            value = value.Replace("“", "\"");
            value = value.Replace("”", "\"");
            value = value.Replace("–", "-");
            value = value.Replace("…", "...");
            return value;
        }

private static void WriteFile(string fileName, string contentType, string content)
        {
            HttpContext context = HttpContext.Current;
            context.Response.Clear();
            context.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            context.Response.Charset = "";
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.ContentType = contentType;
            context.Response.Write(content);
            context.Response.End();
        }
    }

public static class ExcelControllerExtensions
    {
        public static System.Web.Mvc.ActionResult Excel(this Controller controller,
         DataTable dataContext, string fileName)
        {
            return new ExcelResult(dataContext, fileName, null, null, null, null);
        }

public static System.Web.Mvc.ActionResult Excel(this Controller controller,
       DataTable dataContext, string fileName, string[] headers)
        {
            return new ExcelResult(dataContext, fileName, headers, null, null, null);
        }

public static System.Web.Mvc.ActionResult Excel(this Controller controller,
       DataTable dataContext, string fileName, string[] headers,
     TableStyle tableStyle, TableItemStyle headerStyle, TableItemStyle itemStyle)
        {
            return new ExcelResult(dataContext, fileName, headers, tableStyle, headerStyle, itemStyle);
        }
    }
}

//public ActionResult GenerateExcel1()
//{
//   return this.Excel(dt,  "data.xls");
//}

控制器方法:

public ActionResult ExportExcel(string param1, string startTime, string endTime)
     { 
         DateTime start = Convert.ToDateTime(startTime);
         DateTime end = Convert.ToDateTime(endTime);

DataTable dt = _srv.ExportExcel(param1,start, end);
         return this.Excel(dt, "统计111.xls");
     }

js:

点击 “导出”按钮,执行下面的js:

var param = "startTime=" + startTime + "&endTime=" + endTime
          + "&param1=" + param1;

window.open("/Query/ExportExcel?" + param);

MVC导出Excel,提供下载Excel的更多相关文章

  1. asp.net MVC 导出查询结果到Excel

    首先在View视图中有一表单form,导出按钮<input class="btn export" type="button" value="导出 ...

  2. 使用DateSet下载Excel

    这里我们使用Microsoft.Office.Interop.Excel.dll下载Excel,没有引用可点击下载 关键代码,ExcelHelper类 using System; using Syst ...

  3. Springboot+vue前后端分离项目,poi导出excel提供用户下载的解决方案

    因为我们做的是前后端分离项目 无法采用response.write直接将文件流写出 我们采用阿里云oss 进行保存 再返回的结果对象里面保存我们的文件地址 废话不多说,上代码 Springboot 第 ...

  4. ASP.NET MVC导出excel(数据量大,非常耗时的,异步导出)

    要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...

  5. MVC学习笔记---MVC导出excel(数据量大,非常耗时的,异步导出)

    要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...

  6. ASP.NET MVC导出excel

    ASP.NET MVC导出excel 要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式 ...

  7. MVC项目中怎样用JS导出EasyUI DataGrid为Excel

    在做一中考评项目的时候,遇到了这么一个需求.就是把评教后得到的老师的成绩导出为Excel.事实上需求非常普通.实现起来有些复杂.由于老师考评不但有固定的考核项,还有额外加分项.于是我们就抽出来了一个表 ...

  8. mvc导出excel 之 新

    前段时间做的mvc导出excel 老大说要进行优化,我原来导出是用npoi插件进行导出,格式是将数据放入到datatable中,然后进行导出. 说要优化的时候就想着将datatable数据导出格式改为 ...

  9. MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult

    导出EXCEL方法总结 MVC导出数据到EXCEL的方法有很多种,常见的是: 1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可: 优点:可设置丰富的EXC ...

随机推荐

  1. Python学习路程day8

    Socket语法及相关 socket概念 A network socket is an endpoint of a connection across a computer network. Toda ...

  2. GCD,用同步/异步函数,创建并发/串行队列

    队列  第一个参数:C语言字符串,标签 第二个参数: DISPATCH_QUEUE_CONCURRENT:并发队列 DISPATCH_QUEUE_SERIAL:串行队列 dispatch_queue_ ...

  3. Java连接mysql数据库

    1.先创建一个Java项目testMysql(我使用的是intellij编辑器). 2.导入mysql的驱动包. (1) (2) (4) 3.编写代码 import java.sql.Connecti ...

  4. RBL开发笔记三

    2014-08-26 20:06:24 今天就是在开发这个EPOLL来处理网络事件 封装较为健壮的EPOLL模型来处理基本的网络IO 1) 超时这个主题先没有弄 在开发EPOLL包括select/po ...

  5. day14_API第四天

    1.正则(了解) 1.基本的正则表达式(看懂即可) 字符类[abc] a.b 或 c(简单类)[^abc] 任何字符,除了 a.b 或 c(否定)[a-zA-Z] a 到 z 或 A 到 Z,两头的字 ...

  6. HDU 1312 Red and Black (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  7. [原创] 聊聊X-Forwared-For和关于他的几种非主流安全问题

    关于这个X-FORWARED-FOR 有很多非主流漏洞都和他有关  之前我和我的基友misty以为关于这个标头的漏洞会有很多会被很多开发者忽视 会出现很多关于他的安全漏洞 可是由于我精力不足 就没继续 ...

  8. 外观模式(Facade Pattern)

    一.引言 在软件开发过程中,客户端程序经常会与复杂系统的内部子系统进行耦合,从而导致客户端程序随着子系统的变化而变化,然而为了将复杂系统的内部子系统与客户端之间的依赖解耦,从而就有了外观模式,也称作 ...

  9. 大道至简---软件工程实践者的思想------------java伪代码形式读后感第一章

    import.java.大道至简.*; 1.编程的精义----愚公移山 /* 原始需求的产生:惩山北之塞,出入之迂 项目沟通的基本方式:聚室而谋曰 项目的目标:毕力平险,指通豫南,达于汉阴 技术方案: ...

  10. VS2013添加使用lib的方法

    使用第三方厂家的库,一般都会有三个文件: xxx.h xxx.lib xxx.dll 头文件很明显,就在需要用到的文件里面include就好. lib文件,可以直接在IDE中添加,具体步骤有两个: 1 ...