format etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
format etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

22 Mart 2012 Perşembe

Creating excel by code

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)

Excell number format styles

Hi,
Below is a useful info if you like to create an excel file by code and like to format the cells.

Source:
http://cosicimiento.blogspot.com/2008/11/styling-excel-cells-with-mso-number.html


Styling Excel cells with mso-number-format
mso-number-format:"0"NO Decimals
mso-number-format:"0\.000"3 Decimals
mso-number-format:"\#\,\#\#0\.000"Comma with 3 dec
mso-number-format:"mm\/dd\/yy"Date7
mso-number-format:"mmmm\ d\,\ yyyy"Date9
mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM"D -T AMPM
mso-number-format:"Short Date"01/03/1998
mso-number-format:"Medium Date"01-mar-98
mso-number-format:"d\-mmm\-yyyy"01-mar-1998
mso-number-format:"Short Time"5:16
mso-number-format:"Medium Time"5:16 am
mso-number-format:"Long Time"5:16:21:00
mso-number-format:"Percent"Percent - two decimals
mso-number-format:"0%"Percent - no decimals
mso-number-format:"0\.E+00"Scientific Notation
mso-number-format:"\@"Text
mso-number-format:"\#\ ???\/???"Fractions - up to 3 digits (312/943)
mso-number-format:"\0022£\0022\#\,\#\#0\.00"£12.76
mso-number-format:"\#\,\#\#0\.00_ \;\[Red\]\-\#\,\#\#0\.00\ "2 decimals, negative numbers in red and signed