将数据导出到excel不起作用

时间:2014-01-04 06:56:09

标签: c# excel infragistics ignite-ui

我正在使用Infragistics Excel Export框架通过ajax post

将数据导出到excel

以下是客户端的代码

  $.ajax({
        type: 'post',           
        url: '../Common/ExcelExporter',
        data: {
            tbname: parameter, pageNumber: _pageNumber, pageSize: _pageSize
            , exportType: true, exportFormat: true
        },
        success: function (res, status, xhr) {
        alert(JSON.stringify(xhr));
    }
    });

和控制器端的代码

public void ExcelExporter(string tbname,int pageNumber, int pageSize, bool exportType, bool exportFormat)
{
    pageNumber++;
    IEnumerable exportdata = dbList.getClass(tbname);
    bool exportAllPages = exportType;

    if (exportdata == null) {
        var obj = Activator.CreateInstance(Assembly.GetExecutingAssembly().GetType(assemblyName + ".Models." + tbname));        

        if (exportAllPages)
        {
           exportdata = (from e in dbList.getClass(tbname).AsQueryable() select e).AsQueryable();
        }
        else
        {
            exportdata = (from e in dbList.getClass(tbname).AsQueryable() select e).AsQueryable().Skip(pageNumber).Take(pageSize);
        }

    WorkbookFormat excelFormat;

    if (exportFormat)
        excelFormat = WorkbookFormat.Excel2007;
    else
        excelFormat = WorkbookFormat.Excel97To2003;

    ExcelExportingModel exportModel = new ExcelExportingModel(excelFormat);
    exportModel.PopulateExcelWorkbook(exportdata);
    SendForDownload(exportModel.ExcelWorkbook, excelFormat);            
}

[SecuritySafeCritical]
private void SendForDownload(Workbook document, WorkbookFormat excelFormat)
{
    string documentFileNameRoot;          
    documentFileNameRoot = string.Format("Document.{0}", excelFormat == WorkbookFormat.Excel97To2003 ? "xls" : "xlsx");           
    Response.Clear();
    Response.AppendHeader("content-disposition", "attachment; filename=" + documentFileNameRoot);
    Response.ContentType = "application/octet-stream";           
    document.SetCurrentFormat(excelFormat);
    document.Save(Response.OutputStream);       
    Response.End();           
}

代码正在执行而没有任何错误但没有输出。正在下载没有文件。 当我检查控制器的响应时,我得到了这个

输出:

{
 "readyState":4,
 "responseText":"PK\u0002\u\<?xml version=\"1.0\" encoding=\"utf-8\"?>Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Type= \"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties\" Target=\"docProps/app.xml\" Id=\"rId1\" /><Relationship Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties\"Target=\"docProps/core.xml\" Id=\"rId2\" /><Relationship Type= \"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\"Target=\"xl/workbook.xml\" Id=\"rId3\" />/Relationships>PK\u[Content_Types].xmlPK\u0005\u0000",
 "status":200,
 "statusText":"OK"
}

我已参考此link

的代码

请帮助解决此问题

1 个答案:

答案 0 :(得分:1)

@Nilesh在正确的轨道上,但您想使用实际的内存流,而不是临时文件。

替换

document.Save(Response.OutputStream);

using (var ms = new MemoryStream())
{
    document.Save(ms);
    ms.Position = 0;
    ms.CopyTo(Response.OutputStream);
}

我在其他Excel编写器库中看到了这一点。原因是并非所有流都是平等的;某些流(如文件和内存流)允许您使用“位置”属性等内容在其中移动。其他流只能读取或写入;原则上当您写入Response.OutputStream时,您写入流的每个字符都会被发送到用户的浏览器,因此您无法“返回”(实际上,响应流可能会也可能不会被缓冲,但这是另一个讨论)。 Excel库需要能够在流内导航,因此无法直接保存到OutputStream

请记住,使用此方法,您将在内存中拥有两份电子表格; Excel库中的一个和内存流中的副本。除非你并行创建大量的大型电子表格,否则这应该不是问题,但你应该注意它。