码迷,mamicode.com
首页 > 其他好文 > 详细

数据表的导出

时间:2016-03-15 18:56:01      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        string Type = string.Empty;
        int PageRecord = int.MaxValue;
        int Page = 1;
        string SearchHtml = this.txtSearchVal.Value;
        DataSet ds = ChengJian.Com.UI.sz_hr_EducationManage.GetMemberEducationList(PageRecord, Page, SearchHtml, SessionInclude.Id);
        string fileName = "教育经历";//设置导出文件的名称 
        string sbTable = ExportTable(ds, Type);
        ChengJian.Com.UI.ExportExcel.tryToExcel(this.repea, fileName, sbTable.ToString());//调用方法在下面
    }
    #region 导出Excel table表格
    private string ExportTable(DataSet _ds, string _type)
    {

        StringBuilder sbTable = new StringBuilder();
        string bgColor = "style=‘‘";
        #region 表格Header
        sbTable.Append("<table border=\"1\" width=\"100%\" style=\" font-size:14px; \"  ");
        sbTable.Append("<tr>");
        sbTable.AppendFormat("<td {0}>编号</td>", bgColor);
        sbTable.AppendFormat("<td {0}>员工编号</td>", bgColor);
        sbTable.AppendFormat("<td {0}>姓名</td>", bgColor);
        sbTable.AppendFormat("<td {0}>单位</td>", bgColor);
        sbTable.AppendFormat("<td {0}>学习经历</td>", bgColor);
        sbTable.AppendFormat("<td {0}>入学日期</td>", bgColor);
        sbTable.AppendFormat("<td {0}>毕业日期</td>", bgColor);
        sbTable.AppendFormat("<td {0}>学校</td>", bgColor);
        sbTable.AppendFormat("<td {0}>学校类型</td>", bgColor);
        sbTable.AppendFormat("<td {0}>专业</td>", bgColor);
        sbTable.AppendFormat("<td {0}>学历</td>", bgColor);
        sbTable.AppendFormat("<td {0}>学位</td>", bgColor);
        sbTable.Append("</tr>");
        #endregion

        if (_ds != null && _ds.Tables[0].Rows.Count > 0)
        {
            int count = _ds.Tables[0].Rows.Count;
            for (int i = 0; i < count; i++)
            {
                string SysId = _ds.Tables[0].Rows[i]["SysId"].ToString();
                string MemberNum = _ds.Tables[0].Rows[i]["MemberNum"].ToString();
                string MemberName = _ds.Tables[0].Rows[i]["MemberName"].ToString();
                string NowCompanyName = _ds.Tables[0].Rows[i]["NowCompanyName"].ToString();
                string BeginDate = _ds.Tables[0].Rows[i]["BeginDate"].ToString();
                string EndDate = _ds.Tables[0].Rows[i]["EndDate"].ToString();
                string School = _ds.Tables[0].Rows[i]["School"].ToString();
                string Discipline = _ds.Tables[0].Rows[i]["Discipline"].ToString();
                string Degree = _ds.Tables[0].Rows[i]["Degree"].ToString();
                string XueWei = _ds.Tables[0].Rows[i]["XueWei"].ToString();
                string SchoolType = _ds.Tables[0].Rows[i]["SchoolType"].ToString();
                string Learning = _ds.Tables[0].Rows[i]["Learning"].ToString();

                sbTable.Append("<tr>");
                sbTable.AppendFormat("<td>{0}</td>", (i + 1));
                sbTable.AppendFormat("<td>{0}</td>", MemberNum);
                sbTable.AppendFormat("<td>{0}</td>", MemberName);
                sbTable.AppendFormat("<td>{0}</td>", NowCompanyName);
                sbTable.AppendFormat("<td>{0}</td>", Learning);
                sbTable.AppendFormat("<td>{0}</td>", BeginDate);
                sbTable.AppendFormat("<td>{0}</td>", EndDate);
                sbTable.AppendFormat("<td>{0}</td>", School);
                sbTable.AppendFormat("<td>{0}</td>", SchoolType);
                sbTable.AppendFormat("<td>{0}</td>", Discipline);
                sbTable.AppendFormat("<td>{0}</td>", Degree);
                sbTable.AppendFormat("<td>{0}</td>", XueWei);
                sbTable.AppendFormat("</tr>");
            }
        }
        sbTable.Append("</table>");
        return sbTable.ToString();
    }
    #endregion

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace ChengJian.Com.UI
{
    public class ExportExcel
    {
        public static void tryToExcel(System.Web.UI.Control ctl, string fileName, string eportExcelStr)
        {
            if (fileName.Equals(string.Empty) || fileName == null) fileName = "报表";
            
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.Write("<meta http-equiv=Content-Type   content=text/html;charset=UTF-8>");
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName + ".xls"));
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

            ctl.Page.EnableViewState = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-CN", true);
            System.IO.StringWriter tw = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

            ctl.RenderControl(hw);

            Console.WriteLine(tw.ToString());

            HttpContext.Current.Response.Write(eportExcelStr);
            HttpContext.Current.Response.End();
        }
    }
}

 

数据表的导出

标签:

原文地址:http://www.cnblogs.com/Zpyboke/p/5280318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!