导出数据到Excel不起作用

我正在使用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" } 

我已经参考了这个链接的代码

请帮忙解决这个问题

@Nilesh是正确的轨道上,但你想使用一个实际的内存stream,而不是一个临时文件。

更换

 document.Save(Response.OutputStream); 

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

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

请记住,使用这种方法,您将在内存中拥有两个电子表格副本; 一个在Excel库中,一个在内存stream中复制。 这不应该是一个问题,除非你同时创build大量的电子表格,但你应该注意它。