导出Excel 有身份证时注意

if (this.GridView1.Rows.Count != 0)
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");     //这里是用日期做名称
                HttpContext.Current.Response.Charset = "utf-8";
                HttpContext.Current.Response.ContentType = "application/excel";
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                this.GridView1.AllowPaging = false;                 //GridView不启用分页
                GridView1.RenderControl(htmlWrite);
                HttpContext.Current.Response.Write(stringWrite.ToString());
            }

如果你的 GridView 中有 【身份证号】 那你就要注意喽!将身份证号那一列设置为以下这种格式,就OK拉!

for (int i = 1; i <= GridView1.Rows.Count; i++)
        {
            GridView1.Rows[i - 1].Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
            GridView1.Rows[i - 1].Cells[18].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
        }

posted @ 2014-03-13 17:30  旅途。  阅读(363)  评论(0编辑  收藏  举报