2014-02-07 26 views
0

我希望将我的数据表导出为ex​​cel。导出为Excel字符串列转换为日期

我有具有价值像2/5 1/5 ... 导出后这个数据到Excel它看起来02.May 01.May(日期格式)

我怎么能导出此列列,以便excel将其视为字符串而不是将其转换为日期。

这是我ExportToExcel方法

protected void ExportToExcel() 
{ 

    DataTable dt = new DataTable(); 

    dt = Session["aaa"] as DataTable; 

    if (dt.Rows.Count > 0) 
    { 
     string filename = "DownloadMobileNoExcel.xls"; 
     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. 
     //Response.ContentType = application/vnd.ms-excel; 
     Response.ContentType = "application/vnd.ms-excel"; 
     Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ""); 
     this.EnableViewState = false; 
     Response.Write(tw.ToString()); 
     Response.End(); 
    } 
} 

public override void VerifyRenderingInServerForm(Control control) 
{ 
    /* Verifies that the control is rendered */ 
} 
+0

请看这里:http://aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx - 它基本上使用一个textmode css类来将列格式设置为文本格式。 –

回答

0

一种解决方案可能是遍历人,是造成这一问题的细胞在DataTable的列和当前值前面附加一个单引号字符'

换句话说2/5将变成'2/5。这应该强制Excel将数据视为文本。

+0

感谢您的想法,我改变了列的百分比像%40,然后现在解决了问题。 – ercan