I'm creating an excel sheet and trying to open it without saving it but it doesn't seem to work. If I save the file first and then open the file everything works fine though. Any suggestions? Here's my code...
Private Sub ExporttoExcel(table As DataTable)
'your datatable
Dim wb As New XLWorkbook()
table.TableName = "export"
wb.Worksheets.Add(table)
'wb.SaveAs("export.xlsx")
' Prepare the response
Dim httpResponse As HttpResponse = Response
httpResponse.Clear()
httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
httpResponse.AddHeader("content-disposition", "attachment;filename=""export.xlsx""")
' Flush the workbook to the Response.OutputStream
Using memoryStream As New MemoryStream()
wb.SaveAs(memoryStream)
memoryStream.Position = 0
memoryStream.CopyTo(httpResponse.OutputStream)
'memoryStream.WriteTo(httpResponse.OutputStream)
memoryStream.Close()
End Using
httpResponse.End()
End Sub