public void ToExcel()  
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="GB2312";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";
DataGrid.Page.EnableViewState =false;
System.IO.StringWriter  tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
DataGrid.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
这里是代码,
导出DataGrid到Excel中,没有任何格式设置的方面.

解决方案 »

  1.   

    DataGrid没有Page.EnableViewState这个属性啊?
    也没有RenderControl();这个方法。
      

  2.   

    是不是Execl自动作了数据转换,在数据前面加一个'如何?以前是因为类似2003-10的数据自动转换成了日期,前面加'就好了
      

  3.   

    谢谢了.
    to: Jouzen() 
    没有数据转换,数据格式仍然是数值型.和日期好像不是一个问题.
    再有,不是所有的123.00,都显示为123,有的也是正常的显示为123.00.
    所以这样才会有些奇怪.
      

  4.   

    同意Jouzen,在数字前加',将其他数据类型转换成字符串