0

I'm writing a .net web service to download an excel file created with free version of GemBox.Spreadsheet. Calling the service, the server run but doesn't give any answer. This is the code. Any idea (or another library)? Thanks

'''

<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

'''

MAX_1999
  • 81
  • 5
  • How are you accessing your web service (who's the client)? Are you perhaps using AJAX on client? Anyway, perhaps a quick workaround that you could try is to instead, create the Excel file on some location and then return the URL to that file. Or, you could return the byte array (`stream.ToArray()`) and recreate the file on the client-side. – Mario Z Feb 14 '20 at 09:27

1 Answers1

0

better try this way, it worked for me:

'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")