【问题标题】:GemBox.Spreadsheet download xlsx file using .net web serviceGemBox.Spreadsheet 使用 .net Web 服务下载 xlsx 文件
【发布时间】:2020-02-13 10:07:34
【问题描述】:

我正在编写一个 .net Web 服务来下载使用免费版 GemBox.Spreadsheet 创建的 excel 文件。 调用服务,服务器运行但没有给出任何答案。 这是代码。 任何想法(或另一个图书馆)? 谢谢

'''

<WebMethod(EnableSession:=True)>
Public Sub Download()
    SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

    Dim workbook As New ExcelFile()
    Dim worksheet = workbook.Worksheets.Add("Hello World")

    worksheet.Cells(0, 0).Value = "English:"
    worksheet.Cells(0, 1).Value = "Hello"

    worksheet.Cells(1, 0).Value = "Russian:"
    worksheet.Cells(2, 0).Value = "Chinese:"

    Dim options As SaveOptions = SaveOptions.XlsDefault
    Dim filename As String = "TestFile.xls"

    Using stream As New MemoryStream
        workbook.Save(stream, options)

        worksheet.Clear()
        worksheet = Nothing
        workbook = Nothing

        With System.Web.HttpContext.Current.Response
            .ClearHeaders()
            .ClearContent()
            .Buffer = True
            .ContentType = "application/vnd.ms-excel"
            .AddHeader("content-disposition", "attachment; filename=" + filename)
            .AppendHeader("Content-Length", stream.Length)
            stream.CopyTo(.OutputStream)
            .End()
        End With
    End Using

End Sub

'''

【问题讨论】:

  • 您如何访问您的网络服务(谁是客户)?您可能在客户端上使用 AJAX 吗?无论如何,也许您可​​以尝试的一种快速解决方法是在某个位置创建 Excel 文件,然后将 URL 返回到该文件。或者,您可以返回字节数组 (stream.ToArray()) 并在客户端重新创建文件。

标签: .net web-services gembox-spreadsheet


【解决方案1】:

最好尝试这种方式,它对我有用:

'workbook.Save("DataSet to Excel file.xlsx")


Dim responseBase As HttpResponseBase = New HttpResponseWrapper(Me.Response)

' Stream spreadsheet to browser in XLSX format.
workbook.Save(responseBase, "Spreadsheet.xlsx")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2015-04-04
    • 2013-06-17
    相关资源
    最近更新 更多