zoukankan      html  css  js  c++  java
  • 把DataTable中的身份证号导出excel的解决方案

         这两天为了把web页面中的datagrid中的数据导出为Excel的问题费了不少劲(也可能是新手的原因吧),现在总算把这个问题解决了。现在把解决的方法贴出来,请大家多多指教

         /// <summary>
            /// 把DataTable内容导出为excel并返回客户端
            /// </summary>
            /// <param name="dgData">待导出的DataTable</param>
            /// 创 建 人:wgq 
            /// 创建日期:2008年9月18日
            /// 修 改 人:
            /// 修改日期:
            public void DataTableToExcel(System.Data.DataTable dtData)
            {
                System.Web.UI.WebControls.DataGrid dgExport = null;
                // 当前对话
                System.Web.HttpContext curContext = System.Web.HttpContext.Current;
                // IO用于导出并返回excel文件
                System.IO.StringWriter strWriter = null;
                System.Web.UI.HtmlTextWriter htmlWriter = null;
                if (dtData != null)
                {
                    // 设置编码和附件格式
                    //Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html
                    curContext.Response.ContentType = "application/vnd.ms-excel";
                    curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.Charset = "GB2312";
                    //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
                    //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc  .xls .txt .htm  

                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode("员工信息表.xls"));
                    //    curContext.Response.Charset = "";

                    // 导出excel文件
                    strWriter = new System.IO.StringWriter();
                    htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

                    // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
                    dgExport = new System.Web.UI.WebControls.DataGrid();
                   
                    dgExport.DataSource = dtData.DefaultView;
                    dgExport.AllowPaging = false;

        //身份证号的处理的关键在此事件           

        dgExport.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(gridHrInfo_ItemDataBound1);

                    dgExport.DataBind();

                    // 返回客户端
                    dgExport.RenderControl(htmlWriter);
                    curContext.Response.Write(strWriter.ToString());
                    //curContext.flush();
                    curContext.Response.End();
                }
            }

             //此事件为处理18位身份证号 此事件在datagrid绑定时

            protected void gridHrInfo_ItemDataBound1(object sender, DataGridItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                     //cells[10]是指表中要进行理的数据列(第10列为身份证列)
                    e.Item.Cells[10].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                }           
            }

  • 相关阅读:
    论文初稿(二)标题样式:如何做到章节标题自动排序、批量修改正文字号字体
    论文初稿(一)布局:创建论文首先要做的事情
    论文中稿:摘要和关键词
    论文初稿(七)图片格式设置:如何解决修改了正文图片却跑了
    论文终稿(二)分节符:不同页面设置不同的页眉页脚
    论文终稿(一)封面、扉页和独创性声明
    CMD 查看 TCP&UDP 端口占用
    科研结果小论文审核
    如何统计论文纯字数(不包含标点符号和空格)
    人大商学院同等学力在职研究生论文经验文章合集
  • 原文地址:https://www.cnblogs.com/zzdxpq/p/2457724.html
Copyright © 2011-2022 走看看