zoukankan      html  css  js  c++  java
  • GridView导出为excel数据

    protected void BtnExport_Click(object sender, EventArgs e)
        {

            EmployeeTemp bll = new EmployeeTemp();
            DataTable dt = new DataTable();
            try
            {
                pager.PageSize = 20;
                int pageCount = 0;

                dt = bll.pEmployeeTempSampleByCondition(pager.CurrentPageIndex, pager.PageSize, out pageCount, GetParameters());
            }
            finally
            {
                bll.Dispose();
            }

            ExportExcel(dt, "EmployeeTempInfomation");
        }

        public void ExportExcel(DataTable employeeDT, string fileName)
        {
            string str = fileName + ".xls";
            HttpResponse response = HttpContext.Current.Response;
            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");
            response.ContentType = "application/ms-excel";
            response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(str));
            StringWriter writer = new StringWriter(new CultureInfo("zh-CN", true));
            HtmlTextWriter writer2 = new HtmlTextWriter(writer);
            writer2.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=GB2312\">");
            writer2.WriteLine(ExportTable(employeeDT));
            response.Write(writer.ToString());
            response.Flush();
            response.Close();
            response.End();
        }

        public string ExportTable(DataTable employeeDT)
        {
            StringBuilder data = new StringBuilder();
            HHH.Jcjg_HBS.Bll.Area areaBll = new HHH.Jcjg_HBS.Bll.Area();

            data.Append("<table cellspacing=\"0\" cellpadding=\"4\" rules=\"all\" border=\"1\"  style=\"100%;border-collapse:collapse;\">"
              + "<tr class=\"tablehead\" style=\"height:29px\">"
              + "<th align=\"center\" valign=\"middle\"  style=\"5%;\">序号</th>"
              + "<th align=\"center\" valign=\"middle\" style=\"15%;\">姓名</th>"
              + "<th align=\"center\" valign=\"middle\" style=\"15%;\">身份证号码</th>"
              + "<th align=\"center\" valign=\"middle\" style=\"10%;\">机构名称</th>"
              + "<th align=\"center\" valign=\"middle\" style=\"15%;\">岗位证书</th>"
              + "<th align=\"center\" valign=\"middle\" style=\"15%;\">岗位证书编号</th>"
              + "</tr>");

            int i = 0;

            try
            {
                foreach (DataRow item in employeeDT.Rows)
                {
                    data.Append("<tr style=\"height:28px\">");
                    //序号
                    data.AppendFormat("<td  align=\"center\">{0}</td>", ++i);

                    //姓名
                    data.AppendFormat("<td>{0}</td>", IsNull(item["Name"].ToString()));

                    //身份证号码
                    data.AppendFormat("<td>{0}</td>", IsNull("'" + item["IdentityCard"].ToString()));

                    //机构名称
                    data.AppendFormat("<td>{0}</td>", IsNull(item["EnterpriseInfoTempName"].ToString()));

                    //岗位证书
                    data.AppendFormat("<td>{0}</td>", IsNull("'" + item["QualificationName"].ToString()));

                    //岗位证书编号
                    data.AppendFormat("<td>{0}</td>", IsNull(item["QualificationCode"].ToString()));

                    data.Append("</tr>");
                }
            }
            finally
            {
                areaBll.Dispose();
            }
            data.Append("</table>");
            return data.ToString();
        }

        public string IsNull(object obj)
        {
            string result = string.Empty;
            if (obj != null && obj != DBNull.Value)
            {
                result = obj.ToString();
            }
            return result;
        }

  • 相关阅读:
    Excel 常用函数
    Excel中使用VBA访问Access数据库
    Python 日期和时间戳的转换
    tensorflow中命名空间、变量命名的问题
    Git:一个简单示例
    Pyhon中运算符的使用
    python中模块包的离线下载教程
    python中的进制转换
    python几种常见的模块安装方法
    python中的模块调用
  • 原文地址:https://www.cnblogs.com/dengjd/p/3024498.html
Copyright © 2011-2022 走看看