Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader( "Content-Disposition", "attachment;filename=registrationData.xls" );
this.EnableViewState = false;
using ( StringWriter sw = new StringWriter() )
using ( HtmlTextWriter htw = new HtmlTextWriter( sw ) )
{
Table table = new Table();
table.GridLines = System.Web.UI.WebControls.GridLines.Both;
List columns = new List() {
"Sent", "Sent Date", "User Name", "Last Name", "First Name",
"Work Phone", "Work Email", "Number of Tickets", "Street Address 1",
"Street Address 2", "City", "State", "Zip Code" };
TableRow tRow = new TableRow();
string value;
foreach ( string name in columns )
tRow.Cells.Add( new TableCell() { Text = name } );
table.Rows.Add( tRow );
foreach ( var usr in UserData )
{
tRow = new TableRow();
value = ( usr.ticketsSent == null ) ? "N" : usr.ticketsSent.Value.ToString();
tRow.Cells.Add( new TableCell() { Text = value } );
value = ( usr.ticketsSentDate == null ) ? string.Empty : usr.ticketsSentDate.Value.ToShortDateString();
tRow.Cells.Add( new TableCell() { Text = value } );
tRow.Cells.Add( new TableCell() { Text = usr.userName } );
tRow.Cells.Add( new TableCell() { Text = usr.lastName } );
tRow.Cells.Add( new TableCell() { Text = usr.firstName } );
tRow.Cells.Add( new TableCell() { Text = usr.workNumber } );
tRow.Cells.Add( new TableCell() { Text = usr.workEmail } );
tRow.Cells.Add( new TableCell() { Text = usr.ticketsRequested.Value.ToString() } );
tRow.Cells.Add( new TableCell() { Text = usr.address1 } );
tRow.Cells.Add( new TableCell() { Text = usr.address2 } );
tRow.Cells.Add( new TableCell() { Text = usr.city } );
tRow.Cells.Add( new TableCell() { Text = usr.state } );
tRow.Cells.Add( new TableCell() { Text = usr.zipCode } );
table.Rows.Add( tRow );
}
table.RenderControl( htw );
HttpContext.Current.Response.Write( sw.ToString() );
HttpContext.Current.Response.End();
}
No comments:
Post a Comment