Posted on: 8/31/2014 5:06:05 PM | Views : 360


I wrote a function to export data to excel. I found that it trim zero on some cells of excel
For example, 2.10 at datatable and it show 2.1 at excel
How to format the cell to Text when exporting data to excel?
Or add a single quote to column of datatable (2.10 -> '2.10) before exporting?

Here is my function to export data to excel:
public static void ExportToExcel(string strFilename, DataTable dt)
{ if (dt.Rows.Count > 0) { string filename = strFilename; 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 = dt; dgGrid.DataBind();
//Get the HTML for the control. dgGrid.RenderControl(hw); //Write the HTML back to the browser. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext. ...

Go to the complete details ...