数据表的导出

Posted 团子先生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据表的导出相关的知识,希望对你有一定的参考价值。

    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();
        }
    }
}

 

以上是关于数据表的导出的主要内容,如果未能解决你的问题,请参考以下文章

这些角度电子邮件指令代码片段如何连接

小程序各种功能代码片段整理---持续更新

如何使用 fetch 从一个模块导出从 GET API 获得的响应数据到另一个模块

如何把视频片段做成动态图片

使用While循环导出Excel

导出 iPad 应用数据的存档格式建议?压缩包?