use vb.net to create xls file from .net table using NPOI

Nov 13, 2012 at 4:52 PM
Edited Nov 13, 2012 at 5:41 PM

Hi,

using asp.net/vb.net 2010

I have a .net datatable and I would like to create an excel (xls) file using NPOI.   While I am displaying most of the columns to the user, also I would like to format a couple of the columns so I can hide the columns but the value is still in the cell.

Essentially when the user clicks on the go button I want to take the .net datatable, create the excel file and then open the excel file so the user can see it and the do whatever with it, save it or edit it.

are there are any code samples of how to do this?  I just downloaded NPOI today and have not used it before.

I added the NPOI.dll to my bin and added a reference to it.  also I added Imports to NPOI in my page.  then I found this code snippet online but this will not compile because I am getting an error that "NPOI export is not defined" when I try to instantiate my object.  what I have for now is this, first line causing compile error:

        Using exporter = New NpoiExport()
            exporter.ExportDataTableToWorkbook(dtTable, "Results")

            Dim saveAsFileName As String = String.Format("Results-{0:d}.xls", DateTime.Now)

            Response.ContentType = "application/vnd.ms-excel"
            Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", saveAsFileName))
            Response.Clear()
            Response.BinaryWrite(exporter.GetBytes())
            Response.End()
        End Using

thanks

Dave

Coordinator
Nov 29, 2012 at 8:42 PM

I have never seen NpoiExport class before. It should be a third-party wrapper for NPOI. Please reference the third-party document.