Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

New Post: Excel cannot open file because the file format or file extension is not valid.

$
0
0
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

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>