Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.16/5 (4 votes)
See more:
Hi
I am developing a web application in C#.I want to export data from my application to Excel.I need it badly,plz help me out in this regard

Thanking you
Posted
Updated 28-May-19 1:16am

 
Share this answer
 
v2
Comments
VJ Reddy 16-May-12 19:41pm    
Good references. 5!
Prasad_Kulkarni 16-May-12 23:43pm    
Thank you VJ!
Member 10850583 2-Sep-14 15:17pm    
I am having an issue where the EXCEL file needs to be saved before I can open it. If I open it before saving I get an error: http://www.codeproject.com/Questions/814410/Why-is-exporting-GridView-data-to-Excel-exports-th?arn=0
And another article :rolleyes:
Creating basic Excel workbook with Open XML[^]
 
Share this answer
 
Comments
VJ Reddy 16-May-12 19:42pm    
Useful reference. 5!
Wendelius 17-May-12 4:20am    
Thanks :)
Take a look: Export Excel File for C#[^]
 
Share this answer
 
Comments
jagadeeshmn 16-May-12 6:41am    
Thanks a lot for ur help and it worked well for me.
This is the best working example

The DataGridViewPrinter Class[^]
 
Share this answer
 
In this article, i have explained how to export data to excel with formatting.

Export DataTable to Excel with Formatting in C#[^]
 
Share this answer
 
#region Export Grid to Excel
public static void Export(string File, GridView GridView1, string CompanyName, string ReportName, string Criteria1, string Criteria2,int FirstColSpan,int SecColSpan,int ThirdColSpan,int ForthColSpan)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + File);
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Table table = new Table();

// Report Header Start
TableRow Tr = new TableRow();
TableCell TC = new TableCell();
TC.Text = CompanyName;
TC.ColumnSpan = FirstColSpan;
TC.Font.Size = 22;
TC.Font.Bold = true;


TableCell TC1 = new TableCell();
TC1.Text = ReportName;
TC1.ColumnSpan = SecColSpan;
TC1.Font.Size = 22;
TC1.Font.Bold = true;
TC1.HorizontalAlign = HorizontalAlign.Right;
Tr.Cells.Add(TC);
Tr.Cells.Add(TC1);

TableRow Tr1 = new TableRow();
TableCell TC2 = new TableCell();
TC2.Text = Criteria1;
TC2.ColumnSpan = ThirdColSpan;
TC2.HorizontalAlign = HorizontalAlign.Left;
TC2.Font.Size = 12;
TC2.Font.Bold = true;

TableCell TC3 = new TableCell();
TC3.Text = Criteria2;
TC3.ColumnSpan = ForthColSpan;
TC3.Font.Size = 12;
TC3.Font.Bold = true;
TC3.HorizontalAlign = HorizontalAlign.Right;
Tr1.Cells.Add(TC2);
Tr1.Cells.Add(TC3);

table.Rows.Add(Tr);
table.Rows.Add(Tr1);
// Report Header End

// include the gridline settings
table.GridLines = GridView1.GridLines;
if (GridView1.HeaderRow != null)
{
//GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
PrepareControlForExport(GridView1.HeaderRow);
table.Rows.Add(GridView1.HeaderRow);
}

for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
//Apply style to Individual Cells of Alternating Row

//if (i % 2 != 0)
//{
//for (int y = 0; y <= row.Cells.Count - 1; y++)
//{
// row.Cells[y].BackColor = System.Drawing.Color.FromName(GetCellcolor(row.Cells[y].Text));

//}
// }

}

foreach (GridViewRow row in GridView1.Rows)
{
//GridViewExportUtil.PrepareControlForExport(row);
PrepareControlForExport(row);
table.Rows.Add(row);
GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
//TableCell oTableCell = new TableCell();
//oTableCell.Text = "";
//oGridViewRow.Cells.Add(oTableCell);
//PrepareControlForExport(oGridViewRow);
// table.Rows.Add(oGridViewRow);
}

// add the footer row to the table
if (GridView1.FooterRow != null)
{
//GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
PrepareControlForExport(GridView1.FooterRow);
table.Rows.Add(GridView1.FooterRow);
}

table.RenderControl(hw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();



}

private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}

if (current.HasControls())
{
Util.PrepareControlForExport(current);
//GridViewExportUtil.PrepareControlForExport(current);
PrepareControlForExport(current);
}
}
}
#endregion
 
Share this answer
 
You could use FreeDataExports found here:
NuGet Gallery | FreeDataExports 1.0.11
 
Share this answer
 
Comments
OriginalGriff 2-May-21 1:00am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 9 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.
CHill60 4-May-21 8:41am    
Further to the advice from @OriginalGriff I notice that that package is only 3 days old. That makes me think that you are the author. So, also be aware that using QA Solutions to "drive" people to your site is spam. And that will get you banned from the site!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900