Introduction
Excel is a very powerful tool for data analysis and reporting. People who work in the financial industry know that one way or the other Excel spreadsheets are used heavily. The focus of this article is on exposing a few techniques and concepts when dealing with ASP.NET and MS Excel.
Report Generation:
The following sample code explains about Excel report generation from a DataGrid
, and also how to add HTML code in that report:
private void btnExcelReport_Click(object sender, System.EventArgs e)
{
DataSet dsExport = GetData();
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw =
new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dsExport;
hw.WriteLine("<b><u><font size='5'>" +
"Report for the Fiscal year: " + txtFY.Text +
"</font></u></b>");
hw.WriteLine("<br>∓nbsp;");
dgGrid.HeaderStyle.Font.Bold = true;
dgGrid.DataBind();
dgGrid.RenderControl(hw);
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
Amalorpavanathan Yagulasamy (AMAL) is a Software Engineer for Protech Solution Inc. and has designed and implemented projects for Arkansas Child Support, Arkansas Budgetting, Michigan Child Support , Massachusetts Child Support, National Informatics Center-India and Indian Oil Corporation. His background is in developing relational databases and n-tier applications on Windows platforms in CMM Level Standards. He can be reached at amal_forum@hotmail.com