Click here to Skip to main content
11,436,156 members (63,909 online)
Click here to Skip to main content

Excel reporting in ASP.NET using C# and DataGrid

, 31 Mar 2005
Rate this:
Please Sign up or sign in to vote.
This article demonstrates Excel report generation in ASP.NET using C# and DataGrid.

Sample Image - Excel_Report_ASPNET_C_.jpg

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;

  //Report Header
  hw.WriteLine("<b><u><font size='5'>" + 
     "Report for the Fiscal year: " + txtFY.Text + 
     "</font></u></b>");
  hw.WriteLine("<br>&mp;nbsp;");
  // Get the HTML for the control.
  dgGrid.HeaderStyle.Font.Bold = true; 
  dgGrid.DataBind();
  dgGrid.RenderControl(hw);

  // Write the HTML back to the browser.
  Response.ContentType = "application/vnd.ms-excel";
  this.EnableViewState = false;
  Response.Write(tw.ToString());
  Response.End();
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Amalorpavanathan Yagulasamy(AMAL)
Web Developer
United States United States
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

Comments and Discussions

 
GeneralMy vote of 1 Pin
Clifford Nelson14-May-12 12:36
memberClifford Nelson14-May-12 12:36 
GeneralProblems with some characters Pin
angel_java9-Feb-11 21:14
memberangel_java9-Feb-11 21:14 
QuestionHow to export excel with multiple sheets respectively with the data tables in a dataset Pin
siva gangadhar6-Apr-10 4:15
membersiva gangadhar6-Apr-10 4:15 
AnswerRe: How to export excel with multiple sheets respectively with the data tables in a dataset Pin
CikaPero19-Apr-11 23:34
memberCikaPero19-Apr-11 23:34 
GeneralTEMPLATES Pin
Peter Sanders26-Feb-07 5:38
memberPeter Sanders26-Feb-07 5:38 
GeneralReading CSV file and load in to ASP.NET Data Set Pin
Sivakumar_Ramadoss2-Sep-06 0:49
memberSivakumar_Ramadoss2-Sep-06 0:49 
GeneralRe: Reading CSV file and load in to ASP.NET Data Set Pin
Amalorpavanathan Yagulasamy(AMAL)6-Sep-06 6:46
memberAmalorpavanathan Yagulasamy(AMAL)6-Sep-06 6:46 
try this,

private void LoadBooks ()
{
string delimiter = ",";
string tableName = "BooksTable";
string fileName = Server.MapPath("books.csv");

DataSet dataset = new DataSet();
StreamReader sr = new StreamReader(fileName);

dataset.Tables.Add(tableName);
dataset.Tables[tableName].Columns.Add("product");
dataset.Tables[tableName].Columns.Add("price");

string allData = sr.ReadToEnd();
string[] rows = allData.Split("\r\n".ToCharArray());

foreach(string r in rows)
{
string[] items = r.Split(delimiter.ToCharArray());
dataset.Tables[tableName].Rows.Add(items);
}
}



Regards,
Amal

GeneralRe:Reporting of ms-excel useing c# Pin
tiger4205-Apr-08 4:13
membertiger4205-Apr-08 4:13 
GeneralRe: Reading CSV file and load in to ASP.NET Data Set Pin
gg423721-Aug-09 6:43
membergg423721-Aug-09 6:43 
GeneralFile Unreadable Error Pin
Anonymous11-May-05 11:54
sussAnonymous11-May-05 11:54 
GeneralSystem.Web.HttpException Pin
brnwdrng21-Mar-05 13:21
memberbrnwdrng21-Mar-05 13:21 
GeneralRe: System.Web.HttpException Pin
Knut Hamang23-Aug-05 3:03
memberKnut Hamang23-Aug-05 3:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 31 Mar 2005
Article Copyright 2005 by Amalorpavanathan Yagulasamy(AMAL)
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid