SIGN UP MEMBER LOGIN:    
ARTICLE

Export Gridview/Listview to Excel with Color formatting

Posted by Vasanth Articles | Files, Directories in C# April 12, 2010
In this article, I explain how to export Gridview / Listview to excel with same color formatting using .net.
Reader Level:
Download Files:
 

Introduction:

Here in this article I explain on how to export Gridview / Listview to excel with same color formatting using .net. Based on the gridivew fomatting we have to export with same row alignment and format.

Export to Excel:

It's shows that using .net we are export gridvie to excel with color formatting that we are initialized in the girdview. It explains that we extract same thing what we have in the gridview.

Initially generate the temp data to bind into the datagrid.

//Create temp datatable for bind values
DataTable dtTemp = new DataTable(); DataRow dr;
//create schema
dtTemp.Columns.Add("CustId"); dtTemp.Columns.Add("CusName");
//Add temp datas
dr = dtTemp.NewRow(); dr["CustId"] = "1"; dr["CusName"] = "AA1"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["CustId"] = "2"; dr["CusName"] = "BB2"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["CustId"] = "3"; dr["CusName"] = "CC3"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["CustId"] = "4"; dr["CusName"] = "DD4"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["CustId"] = "5"; dr["CusName"] = "EE5"; dtTemp.Rows.Add(dr);

Once get the data, just proceed the below code and export into the excel. You can alter the cells and header based on the requirements and UI.

//Reponse content
Response.AddHeader("content-disposition", "attachment; filename=ExportExcel.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
//Objects
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
//Add the control to the forms
DataGrid dgRecord= new DataGrid();
this.form1.Controls.Add(dgRecord);
//Color Setttings
dgRecord.HeaderStyle.BackColor = System.Drawing.Color.Blue;
dgRecord.DataSource = dtTemp;
dgRecord.DataBind();
//Cells color settings
foreach (DataGridItem dgi in dgRecord.Items)
    foreach (TableCell tcGridCells in dgi.Cells)
        tcGridCells.Attributes.Add("class", "sborder");
//Render the datagrid
dgRecord.RenderControl(htmlWrite);
//Add the style sheet class here
Response.Write(@"<style> .sborder { color : Red;border : 1px Solid Balck; } </style> ");
//Export
Response.Write(stringWrite.ToString());
//End
Response.End();

Login to add your contents and source code to this article
share this article :
post comment
 

How can i do the same, but in c#? tnx

Posted by Vasile Gabriel Nov 24, 2011

Hi,

with this Excel .NET component you can easily export DataGrid to Excel.

Here is a sample Excel C# code how to do it from your Excel ASP.NET project:

// Get DataTable that DataGrid is bound to.
var dataTable = (DataTable)dataGrid.DataSource;

// Create new ExcelFile.
var ef = new ExcelFile();
// Add new worksheet to the file.
var ws = ef.Worksheets.Add(dataTable.TableName);
// Insert the data from DataTable to the worksheet starting at cell "A1".
ws.InsertDataTable(dataTable, "A1", true);

// Stream file to browser.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls");
ef.SaveXls(Response.OutputStream);
Response.End();

Posted by Filip Sep 03, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor