异步导出excel
最近看园里有几篇写有关导出导入excel的博客,我正好最近在项目中也有涉及想来一起分享一下,正好整理一下自己的思路。
一、异步的方式是通过iframe来实现,代码如下:
if ($('#downloadexcel').length <= 0)
$('body').append("<iframe id=\"downloadexcel\" style=\"display:none\"></iframe>");
$('#downloadexcel').attr('src', url);
二、生成excel文件用的第三方组件NPOI,具体如何用园子里有很多关于这方面的资料,这里就不展开了。
三、这里主要介绍一下如何简化HttpResponse到前端生成excel,下面会贴出核心代码,希望给大家有所帮助。
- 声明一个excel返回实体,代码如下:
/// <summary>
/// 表示返回的流数据。
/// </summary>
[DataContract]
public class ExcelResultMessage
{
#region [ Privates ] private MessageType type = MessageType.Info;
private string description = string.Empty; #endregion #region [ Properteis ] /// <summary>
/// 返回结果信息提示类型。
/// </summary>
[DataMember(Order=)]
public MessageType Type
{
get { return this.type; }
set { this.type = value; }
} /// <summary>
/// 返回结果信息提示。
/// </summary>
[DataMember(Order=)]
public string Description
{
get { return this.description; }
set { this.description = value; }
} /// <summary>
/// 返回结果数据。
/// </summary>
[DataMember(Order=)]
public MemoryStream Data { get; set; } /// <summary>
/// 文件名
/// </summary>
[DataMember(Order = )]
public string FileName { get; set; } #endregion
} - 声明一个excel数据容器,代码如下:
/// <summary>
/// 表示返回的excel数据容器。
/// </summary>
[DataContract]
public class ExcelResult
{
#region [ Properteis ] /// <summary>
/// 编码格式。
/// </summary>
public Encoding ContentEncoding { get; set; } /// <summary>
/// 内容类型。
/// </summary>
public string ContentType { get; set; } /// <summary>
/// 数据。
/// </summary>
[DataMember]
public ExcelResultMessage Message { get; set; } #endregion #region [ Methods ] /// <summary>
/// 执行结果。
/// </summary>
/// <param name="context"></param>
public void ExecuteResult(HttpContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
} HttpResponse response = context.Response;
response.ClearContent();
if (!String.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
else
{
response.ContentType = "application/vnd.ms-excel";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
} response.Clear();
if (this.Message != null)
{
response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", this.Message.FileName));
response.BinaryWrite(this.Message.Data == null ? new byte[] : this.Message.Data.GetBuffer());
response.End();
}
} #endregion
} - 声明一个excel页面基类,代码如下:
/// <summary>
/// excel页面基类。
/// </summary>
public abstract class ExcelPageBase : PageBase
{
#region [ Privates ] #endregion #region [ Contructors ] /// <summary>
///
/// </summary>
public ExcelPageBase()
: base()
{
} #endregion #region [ Properties ] /// <summary>
/// excel数据结果。
/// </summary>
protected ExcelResult ExcelResult { get; set; } #endregion #region [ Events ] /// <summary>
/// 页面加载。
/// </summary>
protected override void OnPreRender(EventArgs e)
{
SetNoCache();
this.ExcelResult = new ExcelResult();
GenerateExcelResult(this.ExcelResult);
ExcelResult.ExecuteResult(this.Context);
} /// <summary>
/// 集成Json结果数据。
/// </summary>
protected abstract void GenerateExcelResult(ExcelResult result); #endregion
} - 在实际导出excel中,只要实现这个excel页面基类,然后关注如何生成excel的MemoryStream就可以了,实例代码如下:
public partial class ExportFile : ExcelPage
{
protected override void GenerateExcelResult(ExcelResult result)
{
result.Message = new ExcelResultMessage();
SaleOrderResponse response = FinanceService.GetSaleOrderResponse(SaleModel.BuildSaleOrderQueryCondition(this.UserInfo), true);
MemoryStream stream = SaleModel.ExportToExcel(response);
result.Message.Data = stream;
result.Message.FileName = HttpUtility.UrlEncode(string.Format("{0}{1}.xls", this.BuildContent("AuditSaleOrder"), DateTime.Now.ToString("yyyyMMddhhmmss")));
}
}
异步导出excel的更多相关文章
- 多线程异步导出excel
先新建一个执行类 @Service public class MultiService { private static final Logger logger = LoggerFactory.get ...
- POI导出Excel不弹出保存提示_通过ajax异步请求(post)到后台通过POI导出Excel
实现导出excel的思路是:前端通过ajax的post请求,到后台处理数据,然后把流文件响应到客户端,供客户端下载 文件下载方法如下: public static boolean downloadLo ...
- ASP.NET MVC导出excel(数据量大,非常耗时的,异步导出)
要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...
- MVC学习笔记---MVC导出excel(数据量大,非常耗时的,异步导出)
要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...
- Java 导出Excel的各种尝试
最近的一个项目比较忙,一直没时间过来跟新博客.今天过来分享一下在此项目中遇到的一个小问题:导出Excel:相信导出Excel这个功能是特别常见的,也有很多的方式.好了,不多说了,直接说说自己遇到的各种 ...
- 使用NPOI导出Excel引发异常(IsReadOnly = “book.IsReadOnly”引发了类型“System.NotImplementedException”的异常)
前言: 本人调式npoi导入.导出试用成功后,引入到项目中,导入完美运行,但是导出怎么样都看不到现在的页面,而且浏览器和后台都没有报任务错误,让人好事纳闷,后来去调式,发现在除了一个IsReadOnl ...
- java POI导出Excel文件数据库的数据
在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.这里我简单实现导出Excel文件. POI jar ...
- 采用Post请求的方式提交参数并导出excel
一般情况下,我们都是采用get请求的方式导出excel.例如采用如下方式: var exportUrl = '/xxx;'; window.open(exportUrl); 导出excel所需的逻辑参 ...
- PHP配合JS导出Excel大量数据
一般使用PHP导出Excel表格都会用PHPExcel,但是当遇到要导出大量数据时,就会导致超时,内存溢出等问题.因此在项目中放弃使用这种方式,决定采用前段生成Excel的方式来解决问题. 步骤如下: ...
随机推荐
- PL/0与Pascal-S编译器程序详细注释
学校编译课的作业之一,要求阅读两个较为简单的编译器的代码并做注释, 个人感觉是一次挺有意义的锻炼, 将自己的心得分享出来与一同在进步的同学们分享. 今后有时间再做进一步的更新和总结,其中可能有不少错误 ...
- idea 破解服务器
idea 最新破解服务器 http://idea.iteblog.com/key.php
- 慕课网-安卓工程师初养成-1-3 使用记事本编写Java程序
来源:http://www.imooc.com/video/1501 step1: myProgram.java 源文件 使用记事本编辑 step2: compiler ...
- 支撑5亿用户、1.5亿活跃用户的Twitter最新架构详解及相关实现
如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 摘要:Twitter出道之初只是个奋斗在RoR上的小站点,而如今已拥有1.5亿的活跃 ...
- 学习总结 DML数据库增删改语句
insert into score t values('111','3-105',88)--插入一行数据 insert into score(sno,cno) values('111','3-105' ...
- 学习练习 java面向对象梯形面积
package com.hanqi; public class Ladder { double ShangDi; double XiaDi; double Gao; double MianJi; La ...
- JSTL的全称:JSP Standard Tag Library, jsp 标准标签库
JSTL的全称:JSP Standard Tag Library, jsp 标准标签库 JSTL的作用 提供给Java web开发人员一个标准通过的标签函数库和EL来取代传统直接在页面上嵌入j ...
- 设置lable文本内容的行间距
NSMutableParagraphStyle *paragraphStyle =[ [NSMutableParagraphStyle alloc] init]; paragraphStyle.lin ...
- Delphi Form的释放和隐藏:free,hide,close
form.Free - 释放Form占用的所有资源.Free后,Form指针不能再使用,除非对Form重新赋值. form.Hide - 隐藏Form.可以调用form.Show再 ...
- 【Python】django表单与提交
参考:http://djangobook.py3k.cn/2.0/chapter07/ 本文的内容应属于django的表单模块,没有涉及到的后端request对象的处理方法可以单独深入学习表单. UR ...