发送Excel文件到客户端

我正在尝试将一个Excel文件发送到一个ASP.Net网页的客户端,但它不起作用。

当用户点击一个button时,我们服务器端数据库中的一些数据被格式化,放入Excel文件并发送给用户(作为直接下载)。

一切运作良好,除了发送部分和任何帮助将不胜感激。

这里是我现在使用的代码(客户端):

var dataAjax = {}; $.ajax({ async: false, type: "POST", url: "Default.aspx/BuildExcelFile", contentType: "application/json", data: JSON.stringify(dataAjax), dataType: "text", success: function(html) { //alert(html); }, error: function(request, status, error) { alert("An error occurred : [" + error + "]"); } }); 

和服务器端代码:

 <WebMethod()> _ Public Shared Function BuildExcelFile() As String Dim localExcelPath = "C:\temp1111.xlsx" 'Build the excel file here... '... 'Delete the old version of the Excel file System.IO.File.Delete(localExcelPath) 'Save the Excel File locally xWorkSheet.SaveAs(localExcelPath) xWorkBook.Close() exc.Quit() 'The generated excel is valid, can be opened on the server just fine 'Send the excel file to the client 'This part is not working! :( System.Web.HttpContext.Current.Response.Clear() System.Web.HttpContext.Current.Response.ContentType = "MS-Excel/xls" System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" & System.IO.Path.GetFileName(localExcelPath)) System.Web.HttpContext.Current.Response.TransmitFile(localExcelPath) System.Web.HttpContext.Current.Response.End() Return "Success" End Function 

当我尝试validationCurrent.Response的值时,出现此错误

 Response: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. 

另外,如果我删除了Response.End()调用,则数据将在ajax中的success函数的htmlvariables中被接收。 但是,我想要的文件被下载,没有收到文本…

如果我保持Response.End(),我得到一个Internal Server Error

这是不是因为我的Webmethod共享? 任何线索这里发生了什么? 我能做些什么来解决这个问题?

编辑:

如果有问题,我使用.Net 3.5

我没有使用MVC

我也有同样的问题,从客户端发送数据到服务器。 您可以通过dynamic加载一个启动下载的iFrame来解决这个问题 – iFrame基本上只是填充了一个空白的aspx页面,在Page_Load中开始下载。 您也可以将文件保存到服务器,并提供一个链接在客户端下载。 这些是我发现工作的解决方法。

我不知道你是否可以按你尝试的方式来做。 我检查了所有的例子,它从来没有工作。 如果你不知道,这些方法对我有用。

 Public Shared Function BuildExcelFile() As String Dim localExcelPath = "C:\temp1111.xlsx" 'Build the excel file here... '... xWorkSheet.SaveAs(localExcelPath) xWorkBook.Close() exc.Quit() 'The generated excel is valid, can be opened on the server just fine 'Send the excel file to the client 'This part is not working! :( System.Web.HttpContext.Current.Response.Clear() System.Web.HttpContext.Current.Response.ContentType = "MS-Excel/xls" System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" & System.IO.Path.GetFileName(localExcelPath)) System.Web.HttpContext.Current.Response.TransmitFile(localExcelPath) System.IO.File.Delete(localExcelPath) System.Web.HttpContext.Current.Response.End() Return "Success" End Function 
  1. 如果您使用的是Ajax,请在button上单击使用postbacktrigger

这适用于我:

 string fileName = "ExampleFileName.xlsx"; Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment; filename=" + fileName); Response.BinaryWrite(excelPackage.GetAsByteArray()); // Response.End(); Replaced with below 3 lines to avoid ThreadAbortException Response.Flush(); // Sends all currently buffered output to the client. Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client. ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event. 

我正在使用一个名为“EPPlus.dll”的DLL作为我的Excel库。 不需要进行Ajax调用,因为您可以从Asp.netbutton实例化下载,或者只需要从服务器端代码中调用一个方法来开始下载。