最近看园里有几篇写有关导出导入excel的博客,我正好最近在项目中也有涉及想来一起分享一下,正好整理一下自己的思路。

一、异步的方式是通过iframe来实现,代码如下:

if ($('#downloadexcel').length <= 0)
$('body').append("<iframe id=\"downloadexcel\" style=\"display:none\"></iframe>");
$('#downloadexcel').attr('src', url);

二、生成excel文件用的第三方组件NPOI,具体如何用园子里有很多关于这方面的资料,这里就不展开了。

三、这里主要介绍一下如何简化HttpResponse到前端生成excel,下面会贴出核心代码,希望给大家有所帮助。

  1. 声明一个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
    }
  2. 声明一个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
    }
  3. 声明一个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
    }
  4. 在实际导出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的更多相关文章

  1. 多线程异步导出excel

    先新建一个执行类 @Service public class MultiService { private static final Logger logger = LoggerFactory.get ...

  2. POI导出Excel不弹出保存提示_通过ajax异步请求(post)到后台通过POI导出Excel

    实现导出excel的思路是:前端通过ajax的post请求,到后台处理数据,然后把流文件响应到客户端,供客户端下载 文件下载方法如下: public static boolean downloadLo ...

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

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

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

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

  5. Java 导出Excel的各种尝试

    最近的一个项目比较忙,一直没时间过来跟新博客.今天过来分享一下在此项目中遇到的一个小问题:导出Excel:相信导出Excel这个功能是特别常见的,也有很多的方式.好了,不多说了,直接说说自己遇到的各种 ...

  6. 使用NPOI导出Excel引发异常(IsReadOnly = “book.IsReadOnly”引发了类型“System.NotImplementedException”的异常)

    前言: 本人调式npoi导入.导出试用成功后,引入到项目中,导入完美运行,但是导出怎么样都看不到现在的页面,而且浏览器和后台都没有报任务错误,让人好事纳闷,后来去调式,发现在除了一个IsReadOnl ...

  7. java POI导出Excel文件数据库的数据

    在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.这里我简单实现导出Excel文件. POI jar ...

  8. 采用Post请求的方式提交参数并导出excel

    一般情况下,我们都是采用get请求的方式导出excel.例如采用如下方式: var exportUrl = '/xxx;'; window.open(exportUrl); 导出excel所需的逻辑参 ...

  9. PHP配合JS导出Excel大量数据

    一般使用PHP导出Excel表格都会用PHPExcel,但是当遇到要导出大量数据时,就会导致超时,内存溢出等问题.因此在项目中放弃使用这种方式,决定采用前段生成Excel的方式来解决问题. 步骤如下: ...

随机推荐

  1. mysql 使用说明-2

    3.3.4 Retrieving Information from a Table Select 命令从表格中取回信息 SELECT what_to_select FROM which_table W ...

  2. EL表达式结合页面JSTL使用 迭代显示表格

    1.迭代显示表格 <%@ page isELIgnored="false"%><%@ taglib uri="/WEB-INF/struts-bean. ...

  3. OC基础(7)

    封装 继承基本概念 继承相关特性 多态基本概念 多态的实现 *:first-child { margin-top: 0 !important; } body > *:last-child { m ...

  4. 【转】WEB测试到移动测试的转换

    移动互联网的发展毋庸置疑是必然的趋势,我们曾经传统WEB互联网测试的同学,也必然走上移动测试的道路,移动测试与pc测试到底需要怎样的思维转变才能更快的进入移动节奏呢?对比下WEB与移动的测试不同点: ...

  5. Ogre参考手册(五)3.2 合成器

    3.2 合成器Compositor 合成器框架是Ogre用于全屏后处理的API.你可以通过脚本而不是API定义合成器.你可以很容易为视口实例化合成器. 合成器基础 无论是要替换还是要与主渲染窗口混合, ...

  6. 关于static/const的作用

    这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1)在函数体内,一个被声明为静态的变量在这一函数被调用过程中维持其值不变(该变量存放在静态变量区). 2) 在模块内 ...

  7. $(function(){})与 (function(){})() (function($){})() 的区别

    1. $(function(){ }) 或 jQuery(function(){ }) 此函数也可以写成 jQuery(function(){ }), 用于存放操作DOM对象的代码,执行其中代码时DO ...

  8. openStack reboot调试

  9. WWF3入门<第一篇>

    工作流是什么东西?暂时还不是很弄得清除. 工作流是用来解决什么问题的?暂时只是形成了一个很模糊的概念,还没办法用语言描述出来. 一.入门范例 以VS2008为例,先来创建一个WWF程序. 在工具箱中, ...

  10. 学习使用LaTex排版文字输出为pdf(1)

    学习用latex写我的简历. 我在ubuntu环境下,先下载所需软件,命令百度就可以. 先创建一个a.tex,写上 \documentclass{article} \usepackage{CJK} \ ...