Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 将Excel文件发送到客户端_Asp.net_Vb.net_Excel_.net 3.5_Asp.net 2.0 - Fatal编程技术网

Asp.net 将Excel文件发送到客户端

Asp.net 将Excel文件发送到客户端,asp.net,vb.net,excel,.net-3.5,asp.net-2.0,Asp.net,Vb.net,Excel,.net 3.5,Asp.net 2.0,我正在尝试将Excel文件发送到ASP.Net网页的客户端,但它不起作用 当用户单击按钮时,服务器端数据库中的一些数据被格式化,放入Excel文件并发送给用户(作为直接下载) 一切都很好,除了发送部分和任何帮助将不胜感激 下面是我现在正在使用的代码(客户端): 和服务器端代码: <WebMethod()> _ Public Shared Function BuildExcelFile() As String Dim localExcelPath = "C:\temp1111.

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

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

一切都很好,除了发送部分和任何帮助将不胜感激

下面是我现在正在使用的代码(客户端):

和服务器端代码:

<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
另外,如果我删除
Response.End()
调用,数据将在ajax中
success
函数的html变量中接收。但是,我希望文件被下载,而不是作为文本接收

如果我保留Response.End(),我会得到一个
内部服务器错误

因为我的Webmethod是共享的,所以它不起作用吗? 有什么线索吗?我能做些什么来解决这个问题

编辑:

如果有必要的话,我正在使用.NET3.5


我没有使用MVC,我在从客户端向服务器发送数据时也遇到了同样的问题。您可以通过动态加载一个iFrame启动下载来解决这个问题-iFrame基本上只是填充了一个空白的aspx页面,在
页面加载中启动下载。您还可以将文件保存到服务器,并提供一个链接以在客户端下载该文件。这些是我发现有效的变通方法

我不知道你是否能用你正在尝试的方式去做。我检查了所有的例子,但都不起作用。如果你不明白,这些方法对我很有效

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
  • 如果您使用的是Ajax,请在按钮上使用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.net按钮实例化下载,或者在开始下载时从服务器端代码调用方法

    我想这是因为你用ajax来称呼它。我会说:使用ajax为浏览器加载一个新的url。然后在该url中,处理http句柄并直接输出excel文件。感谢您的快速响应。有没有相关的链接或提示?我对web开发还是相当陌生,创建一个新的处理程序,然后在ajax中,弹出一个新的浏览器来显示处理程序url!我从你的答案中找到了一个解决办法。我没有通过ajax调用webmethod,而是有一个隐藏的服务器端按钮,当我想开始下载时,它会自动单击(使用jquery)。现在一切都很好:)很高兴我能帮上忙
    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
    
    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.