MVC导出Excel,提供下载Excel

类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
+ "¶m1=" + param1;
window.open("/Query/ExportExcel?" + param);
MVC导出Excel,提供下载Excel的更多相关文章
- asp.net MVC 导出查询结果到Excel
首先在View视图中有一表单form,导出按钮<input class="btn export" type="button" value="导出 ...
- 使用DateSet下载Excel
这里我们使用Microsoft.Office.Interop.Excel.dll下载Excel,没有引用可点击下载 关键代码,ExcelHelper类 using System; using Syst ...
- Springboot+vue前后端分离项目,poi导出excel提供用户下载的解决方案
因为我们做的是前后端分离项目 无法采用response.write直接将文件流写出 我们采用阿里云oss 进行保存 再返回的结果对象里面保存我们的文件地址 废话不多说,上代码 Springboot 第 ...
- ASP.NET MVC导出excel(数据量大,非常耗时的,异步导出)
要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...
- MVC学习笔记---MVC导出excel(数据量大,非常耗时的,异步导出)
要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...
- ASP.NET MVC导出excel
ASP.NET MVC导出excel 要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式 ...
- MVC项目中怎样用JS导出EasyUI DataGrid为Excel
在做一中考评项目的时候,遇到了这么一个需求.就是把评教后得到的老师的成绩导出为Excel.事实上需求非常普通.实现起来有些复杂.由于老师考评不但有固定的考核项,还有额外加分项.于是我们就抽出来了一个表 ...
- mvc导出excel 之 新
前段时间做的mvc导出excel 老大说要进行优化,我原来导出是用npoi插件进行导出,格式是将数据放入到datatable中,然后进行导出. 说要优化的时候就想着将datatable数据导出格式改为 ...
- MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult
导出EXCEL方法总结 MVC导出数据到EXCEL的方法有很多种,常见的是: 1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可: 优点:可设置丰富的EXC ...
随机推荐
- BZOJ 3809 莫队+(分块|BIT)
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> ...
- 从c开始,小小感悟
c语言是众多编程小白进入编程大门的钥匙,不过许多人在学习一段时间以后就渐渐产生了困惑,枯燥的黑色界面渐渐让他们失去了"渴望",---我还不能制作出一款像样的软件,我还是只是在算数学 ...
- log4j.properties配置详解(转)
本篇文章转自http://it.oyksoft.com/log4j/ 非常感谢原创作者的辛勤编写与分享. 一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders ...
- OD调试篇8
那么今天需要破解的呢,是这样一款软件. 程序刚刚进去会发现一个nag弹窗 说没有注册,要花20美金才能注册.只有5天的限制期限可以用了 进去之后 点击help里的关于这款软件 也显示了这是一个 ...
- Spring 和SpringMVC 的父子容器关系
Spring和SpringMVC作为Bean管理容器和MVC层的默认框架,已被众多WEB应用采用,而实际使用时,由于有了强大的注解功能,很多基于XML的配置方式已经被替代,但是在实际项目中,同时配 ...
- 基于cocos2d-x的Android游戏中使用fmod音频引擎
cocos2d-x的音频引擎是cocosDenshion, 它的Android版比较弱, 只能播放一个背景音乐和些许音效, 如果要实现稍微复杂一点的音频播放, 比如同时播放几个音轨就不能了. 这一点远 ...
- img标签中alt和title属性的正确使用
在的img标签有两个属性分别为alt和title,对于很多初学者而言对这两个属性的正确使用都还抱有迷惑,当然这其中一部分原因也是ie浏览器所导致的.正确的使用这两个属性除了可以提高图片的搜索能力外,在 ...
- Jena对描述逻辑构造的支持
前言 本文依据"The Description Logic Handbookd"中Appendxi1 Description Terminology中基本的描述逻辑构造,考察Jen ...
- Java笔记4-do while循环,break,修饰符,方法的调用
do while循环语法:do{ //循环体}while(条件表达式); 注:它是先执行循环体,后再判断的循环结构. 如:int i = 0;do{ System.out.println(" ...
- HDU 2577(DP)
题意:要求一个字符串输入,按键盘的最少次数.有Caps Lock和Shift两种转换大小写输入的方式 思路:用dpa与dpb数组分别记录Caps Lock的开关状态,dpa表示不开,dpb表示开 代码 ...