gridview导出之后身份证号是科学计数法,显示之后数据最后几位全是0了。找了半天,改不对啊!!
    private void Export(string FileName)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString() + ".xls");
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        GridView1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void printout_Click(object sender, EventArgs e)
    {
        Export("学生报名信息");
    }
}

解决方案 »

  1.   

    http://topic.csdn.net/u/20091226/13/5f6e05fb-0ebd-47eb-b7d0-26d516ef48a6.html
      

  2.   


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
                if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                e.Row.Cells[4].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                e.Row.Cells[5].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                e.Row.Cells[6].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                e.Row.Cells[7].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                e.Row.Cells[8].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#e0ffff'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            }
    }
      

  3.   

      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
                if (e.Row.RowType == DataControlRowType.DataRow)
            {
    //找到身份证是第几列,比如第一列.只需将其格式化为字符即可。
                e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");        
            }
    }
      

  4.   

    导出的时候加“'",在导出excel后会怎么显示, 还有怎么处理加“'"啊!
      

  5.   

    这是excel的问题,excel会自动将全数字的项变为科学计数法,导出的时候加个字符之类的就可以了,或者也能在excel里把这列改为字符
      

  6.   

    前台<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testdc.aspx.cs" Inherits="testdc" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server"> 
            <asp:Button ID="btnToExcel" runat="server" OnClick="btnToExcel_Click" Text="导出到Excel" />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="xm" HeaderText="姓名" />
                    <asp:BoundField DataField="xb" HeaderText="性别" />
                    <asp:BoundField DataField="sfzh" HeaderText="身份证号" />
                    <asp:BoundField DataField="zxmc" HeaderText="高中地址" />
                    <asp:BoundField DataField="jtzz" HeaderText="家庭住址"/>
                </Columns>
                <RowStyle ForeColor="#000066" />
                <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            </asp:GridView>
        </form>
        </body>
    </html>后台
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.IO;
    using System.Text;public partial class testdc : System.Web.UI.Page
    {
        DB con = new DB();
        protected void Page_Load(object sender, EventArgs e)
        {
            bind_GridView();
        }
        protected void bind_GridView()
        {
            string sql = "select * from baoming";
            con.DBopen();
            DataSet myds = con.creatdataset(sql, "xm");
            GridView1.DataSource = myds;
            GridView1.DataBind();
            con.DBclose();
        }
        private void Export(string FileName)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString() + ".xls");
            Response.ContentType = "application/ms-excel";
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
        }
        protected void btnToExcel_Click(object sender, EventArgs e)
        {
            Export("学生报名信息");
        }
    }
      

  7.   

    打开excel模板导出数据'

    string style = @"<style> .text { mso-number-format:\@; } </script> "; 
    Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
    Response.ContentType = "application/excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    gv.RenderControl(htw);
    Response.Write(style); Response.Write(sw.ToString());
    Response.End();
    public override void VerifyRenderingInServerForm(Control control){}
      

  8.   

    该事件必须点击GridView1,选择的他的事件,双击进入事件编写,即可 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
                if (e.Row.RowType == DataControlRowType.DataRow)
            {
    //你的身份证是第3列,只需将其格式化为字符即可。
                e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");        
            }
    }
      

  9.   

    该事件必须点击GridView1,选择的他的事件,双击进入事件编写,即可 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
                if (e.Row.RowType == DataControlRowType.DataRow) 
            { 
    //你的身份证是第3列,只需将其格式化为字符即可。 
                e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");        
            } 
    }
      

  10.   

        private void Export(string FileName)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                if (GridView1.Rows[i].Cells[2].Text.Length >= 15)
                    GridView1.Rows[i].Cells[2].Text = "[" + GridView1.Rows[i].Cells[2].Text.Trim() + "]";
                //GridView1.Rows[i].Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
            }
            ......
        }
    问题解决了,谢谢大家!我对身份证号列进行了处理。
      

  11.   

    前面加空格
    '<%#"&nbsp;" + Eval("id_card").ToString() %>'
      

  12.   

    这个方法好.....protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
                if (e.Row.RowType == DataControlRowType.DataRow) 
            { 
    //你的身份证是第3列,只需将其格式化为字符即可。 
                e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");        
            } 
    }