2
Vote

Corrupted Excel files with NPOI - asp.net

description

Hi all, I need a small help with NPOI and asp.net.
I'm trying to create an excel file from a web application, written in asp.net (vb).
Everything is fine with excel 2003 xls format, while using the XSSF object model I get corrupted files.
Opening files in Excel I get the message that says file is corrupted, and can be restored (and actually Excel repairs it). The problem occurs even if I try to save stream into a file, rather than using memorystream.
Help is appreciated...
Here's the code, see attachment for error:
    Dim wb As New XSSFWorkbook
    Dim ws As new XSSFSheet
    ws = wb.CreateSheet("Sheet1")
    Dim r As XSSFRow
    r = ws.CreateRow(0)
    Dim c = r.CreateCell(0)
    c.SetCellValue("Test")

    Dim m As New IO.MemoryStream
    m.Flush()
    wb.Write(m)

    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    Response.AddHeader("content-disposition", "attachment;filename=test.xlsx")
    Response.Clear()
    Response.BinaryWrite(m.GetBuffer())

    Response.End()

file attachments

comments

vashkenazy wrote Feb 22, 2014 at 4:30 PM

File restored attachment

wrote Oct 19, 2016 at 8:43 AM

chantszhim wrote Oct 19, 2016 at 8:45 AM

I got the same problem after migrating the site from Windows Server 2008 R2 to Windows Server 2012 R2.