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=\"width:100%;border-collapse:collapse;\">"
          + "<tr class=\"tablehead\" style=\"height:29px\">"
          + "<th align=\"center\" valign=\"middle\"  style=\"width:5%;\">序号</th>"
          + "<th align=\"center\" valign=\"middle\" style=\"width:15%;\">姓名</th>"
          + "<th align=\"center\" valign=\"middle\" style=\"width:15%;\">身份证号码</th>"
          + "<th align=\"center\" valign=\"middle\" style=\"width:10%;\">机构名称</th>"
          + "<th align=\"center\" valign=\"middle\" style=\"width:15%;\">岗位证书</th>"
          + "<th align=\"center\" valign=\"middle\" style=\"width: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;
    }

posted on 2013-04-16 18:42  dengjd  阅读(146)  评论(0编辑  收藏  举报

导航