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 ...
随机推荐
- JS rem 设置
(function () { var docEl = document.documentElement; var resize = 'orientationchange' in window ? 'o ...
- Mousejack Hacking : 如何利用MouseJack进行物理攻击
0×00 前言 近期安全公司Bastille Networks(巴士底狱)安全研究员发现大多数无线鼠标和接收器之间的通信信号是不加密的.黑客可对一百米范围内存在漏洞的蓝牙无线键鼠进行嗅探甚至劫持,从而 ...
- iOS 根据UIImage 修改UIImageView Frame (包括截取图片中间部分)
iOS UIImageView 根据需求调整frame 1.图片的宽和高不相等,截取图片的中间部分,截取的部分Size明确 2.图片的宽度要等于其父视图的类的宽度,然后根据宽度计算高度,保证 图片不变 ...
- H.264简介
H.264/MPEG-4 AVC (H.264) 是1995年自MPEG-2视频压缩标准发布以后最新的, 最有前途的视频压缩标准. H.264是由ITU-U和ISO/IEC联合开发组共同开发的最新国际 ...
- yii2-搜索带分页,分页的两种方式
1.文章表关联 <?php //...other code //关联 public function getCate(){ return $this->hasOne(ArticleCate ...
- POJ 1236 SCC+缩点
题意:一张有向图,一问至少给几个点发送软件,才能让所有点都能收到软件:二问是至少添加几条边才能让整个图是一个连通分量: 分析:一般求连通分量都会求缩点,在这里缩点之后,生成一张新的图,在新的图中求每一 ...
- Windows和Unix下的编码问题
今天测试shell脚本时,执行报错: ./report.sh: /tmp/tmp.E8ekx6r5Qq/report.sh: /bin/bash^M: bad interpreter: No such ...
- MEDIA-SYSSERVICES媒体播放
1 简单的音乐播放器 1.1 问题 本案例结合之前所学的网络和数据解析等知识完成一个网络音乐播放器,如图-1所示: 图-1 1.2 方案 首先创建一个SingleViewApplication应用,在 ...
- python实现拷贝指定文件到指定目录
python实现这个功能非常简单,因为库太强大了 import os import shutil alllist=os.listdir(u"D:\\notes\\python\\资料\\&q ...
- 计算纯文本情况下RichTextBox实际高度的正确方法(.NET)
2016-07-17重大更新 其实有更好.更系统的方法,也是最近才发现的,分享给大家!! /// <summary> /// /// </summary> ...