日期:2014-05-18  浏览次数:20331 次

数据导出问题
导出数据到excel中的代码
[code=C#][/code]
                Response.Clear();
                Response.Buffer   =   true;
                //编码控制
                  //Response.Charset   =   "utf-8 ";
                //Response.ContentEncoding   =   System.Text.Encoding.GetEncoding( "utf-8 ");
                Response.Charset   =   "GB2312 ";
                Response.ContentEncoding   =   System.Text.Encoding.GetEncoding( "GB2312 ");
                Response.AppendHeader( "Content-Disposition ",   "attachment;filename=UserInfo.xls ");  
                Response.ContentType   =   "application/ms-excel ";
                this.EnableViewState   =   false;
                System.IO.StringWriter   swOut   =   new   System.IO.StringWriter();
                HtmlTextWriter   hTw   =   new   HtmlTextWriter(swOut);
                myGW.RenderControl(hTw);
                Response.Write(swOut.ToString());
                Response.End();
//上面是Button事件代码
//myGw是是一个gridview控件,有几个字段,其中有一个身份证号码字段,通过导出到excel后身份证号码不能正常显示,他显示的是5.13701E+17,展开后后三位是0,怎样才能使它正常显示呢?


------解决方案--------------------
Asp.Net WebForm中DataGrid导出的时候,在ItemDataBound内 就OK 了

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 

e.Item.Cells[0].Attributes.Add("style","vnd.ms-excel.numberformat:@"); 


------解决方案--------------------
C# code


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[4].Attributes.Add("style","vnd.ms-excel.numberformat:@"); 

    }