• [.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:@");
    
        }
    
            }
    
        }

    --------------------------------------

    欢迎您,进入 我系程序猿 的cnBlog博客。

    你不能改变你的过去,但你可以让你的未来变得更美好。一旦时间浪费了,生命就浪费了。

    You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.

    --------------------------------------

    分享到QQ空间  

  • 相关阅读:
    循环的注意点
    c语言实践输出某个区间中不是3的倍数的偶数
    while循环for循环优缺点和应用
    while 和do while循环的区别
    多重if else和switch case的区别
    if else的执行流程
    多个if和一个ifelse的区别
    对两个变量排序,从小到大输出
    【译】第四篇 Integration Services:增量加载-Updating Rows
    【译】第三篇 Integration Services:增量加载-Adding Rows
  • 原文地址:https://www.cnblogs.com/jqmtony/p/4111296.html
Copyright © 2020-2023  润新知