Public Sub ExportToExcelFromList(fileName As String, listAID As List(Of AppInfoDay), Optional strArrListToSkip As String() = Nothing)
'The Clear method erases any buffered HTML output.
HttpContext.Current.Response.Clear()
'The AddHeader method adds a new HTML header and value to the response sent to the client.
HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName & ".xls"))
'The ContentType property specifies the HTTP content type for the response.
HttpContext.Current.Response.ContentType = "application/ms-excel"
'Implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder.
Using sw As New StringWriter()
'Writes markup characters and text to an ASP.NET server control output stream. This class provides formatting capabilities that ASP.NET server controls use when rendering markup to clients.
Using htw As New HtmlTextWriter(sw)
' Create a form to contain the List
Dim table As New Table()
Dim row As New TableRow()
'---ADD COLS HEADER BASED ON OBJECT PROPERTIES
For Each proinfo As PropertyInfo In New AppInfoDay().[GetType]().GetProperties()
'---SKIP UNDESIRED COLUMNS
If strArrListToSkip.Contains(proinfo.Name) Then
Continue For
Else
Dim hcell As New TableHeaderCell()
hcell.Text = proinfo.Name
row.Cells.Add(hcell)
End If
Next
table.Rows.Add(row)
'---ADD ROWS WITH DATA
For Each aid As AppInfoDay In listAID
Dim newRow As New TableRow()
For Each proinfo As PropertyInfo In aid.[GetType].GetProperties()
If strArrListToSkip.Contains(proinfo.Name) Then
Continue For
Else
Dim NewCell As New TableCell()
NewCell.Text = proinfo.GetValue(aid, Nothing).ToString()
newRow.Cells.Add(NewCell)
End If
Next
table.Rows.Add(newRow)
Next
' render the table into the htmlwriter
table.RenderControl(htw)
' render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString())
HttpContext.Current.Response.[End]()
End Using
End Using
End Sub