Hi,
Sometimes you might need to create an export of a table as an excel file.
Of course there are thousands of tools for that but if you d like to do it manually, here is the code you need:
Sorry I have got the code from one of my collegues, I can not share the source link.
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Export" + DateTime.Now.ToString("yyyy_MM_dd") + ".xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving than
// comment out the line below
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
Table tbl = //Create your table in here !
tbl.RenderControl(htmlWrite);
//These both line are needed if you like to format the cells as text and number
string style = @"<style>.text{mso-number-format:\@;text-align:left;};.Nums{mso-number-format:0\.00;};.unwrap{wrap:false}</style>";
Response.Write(style);
Response.Write(stringWrite);
//Response.Write(stringWrite.ToString().Replace(".", ","));
Response.End();
While you were creating the table you d like to render as excel, you can give the format as below:
row.Cells[0].Attributes.Add("class", "text");
row.Cells[1].Attributes.Add("class", "Nums");
"text" and "Nums" classes should be set after you have rendered your control. (Comments can be found on the first code fragment)
Sometimes you might need to create an export of a table as an excel file.
Of course there are thousands of tools for that but if you d like to do it manually, here is the code you need:
Sorry I have got the code from one of my collegues, I can not share the source link.
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Export" + DateTime.Now.ToString("yyyy_MM_dd") + ".xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving than
// comment out the line below
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
Table tbl = //Create your table in here !
tbl.RenderControl(htmlWrite);
//These both line are needed if you like to format the cells as text and number
string style = @"<style>.text{mso-number-format:\@;text-align:left;};.Nums{mso-number-format:0\.00;};.unwrap{wrap:false}</style>";
Response.Write(style);
Response.Write(stringWrite);
//Response.Write(stringWrite.ToString().Replace(".", ","));
Response.End();
While you were creating the table you d like to render as excel, you can give the format as below:
row.Cells[0].Attributes.Add("class", "text");
row.Cells[1].Attributes.Add("class", "Nums");
"text" and "Nums" classes should be set after you have rendered your control. (Comments can be found on the first code fragment)
Hiç yorum yok:
Yorum Gönder