zoukankan      html  css  js  c++  java
  • .Net中导出Excel中身份证等数字串的解决方式

     public static void DataTableToExcel(System.Data.DataTable dtData, String FileName)    

    {        

      GridView dgExport = null;        

        HttpContext curContext = HttpContext.Current;        

      StringWriter strWriter = null;        

      HtmlTextWriter htmlWriter = null;        

      if (dtData != null)        

       {            

         HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);

                 curContext.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");

                curContext.Response.ContentType = "application/vnd.ms-excel";

                curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");

                curContext.Response.Charset = "GB2312";

                strWriter = new StringWriter();

                htmlWriter = new HtmlTextWriter(strWriter);  

               dgExport = new GridView();

                dgExport.RowDataBound += (GridViewFormat); //在GridView绑定数据时,将数据格式化

                dgExport.DataSource = dtData.DefaultView;

                dgExport.AllowPaging = false;

                dgExport.DataBind();

                dgExport.RenderControl(htmlWriter);

                curContext.Response.Write(strWriter.ToString());

                curContext.Response.End();  

           }

    }

     protected static void GridViewFormat(object sender, GridViewRowEventArgs e)    

    {        

         //1)  文本:vnd.ms-excel.numberformat:@

            //2)  日期:vnd.ms-excel.numberformat:yyyy/mm/dd

            //3)  数字:vnd.ms-excel.numberformat:#,##0.00

            //4)  货币:vnd.ms-excel.numberformat:¥#,##0.00

            //5)  百分比:vnd.ms-excel.numberformat: #0.00%

            for (int i = 0; i < e.Row.Cells.Count; i++)

            {

                if (e.Row.RowType == DataControlRowType.DataRow)

        {

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

        }

            }

        }

  • 相关阅读:
    Educational Codeforces Round 33 (Rated for Div. 2) B. Beautiful Divisors【进制思维/打表】
    Educational Codeforces Round 33 (Rated for Div. 2) A. Chess For Three【模拟/逻辑推理】
    java中的BigInteger
    动态规划-最长上升子序列(LIS模板)多解+变形
    Rain on your Parade---hdu2389(HK求最大匹配)
    Air Raid---hdu1151(最小路径覆盖)
    Swap---hdu2819(最大匹配)
    棋盘游戏---hdu1281(最大匹配)
    The Accomodation of Students---hdu2444(二分图,最大匹配)
    COURSES---poj1469 hdu1083(最大匹配)
  • 原文地址:https://www.cnblogs.com/hanshuhe/p/3106088.html
Copyright © 2011-2022 走看看