异步导出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的方式来解决问题. 步骤如下: ...
随机推荐
- Hdfs增量导入小文件合并的思路
1.使用mr进行合并 2.使用getmerge 将文件拉取到本地,再上传到hdfs,注意nl参数 3.使用appendToFile 4.使用hadoop提供的打包压缩技术 Usage: hadoop ...
- Intent Receiver
使用Intent Receiver让自己的应用对一个外部事件做出响应,比如当电话呼入时,或者当数据网络可用时,或者时间到晚上了.Intent Receiver不能显示用户界面,它只能通过Notific ...
- Cocos2d-x下Lua调用自定义C++类和函数的最佳实践[转]
Cocos2d-x下Lua调用C++这事之所以看起来这么复杂.网上所有的文档都没讲清楚,是因为存在5个层面的知识点: 1.在纯C环境下,把C函数注册进Lua环境,理解Lua和C之间可以互相调用的本质 ...
- DFS
HDU1181 http://acm.hdu.edu.cn/showproblem.php?pid=1181 #include<stdio.h> #include<algorith ...
- 压缩上传并预览 flash
最近研究一个功能:用as3写的上传图片并实现预览.觉得花了很多时间也学到很多知识,将自己的所得记录下来供大家分享. 首先是预览功能的实现,大家自然而然就想到了loader来加载图片并显示,由于项目没有 ...
- 无法读取配置节“protocolMapping”,因为它缺少节声明
无法读取配置节“protocolMapping”,因为它缺少节声明 1.正常情况 : Web.config文件中有protocolMapping节点, 发现在IIS部署时使用了.NET 2.0的 ...
- 记录网上资源URL
FQ(使用路由器): http://kennylee26.iteye.com/blog/1887753 http://www.iteye.com/search?type=all&query=s ...
- java 中的fanal
三.java中有final final 修饰符 关键字可用于修饰类,变量和方法,final关键字有点类似于C#里的sealed 关键字,用于表示它修饰的类,方法和变量不可改变. fina修饰变量时,表 ...
- 堆栈的实现(c语言)
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define EmptyTOS (-1) ...
- 蛮考验基础的JS笔试题(有坑小心!)
1. 考察this var length = 10 function fn(){ alert(this.length) } var obj = { length: 5, method: functi ...