• Excel 导出的方法 ,类



    /// <summary>
    /// 导出Excel 李成虎
    /// </summary>
    /// <param name="dt">DataTable数据集</param>
    /// <param name="filePath">模板路径</param>
    /// <param name="fileName">文件名</param>
    public void DGToExcel(DataTable dt, string filePath, string fileName)
    {
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    DataGrid excel = new DataGrid();
    System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle();
    System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();
    System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle();
    AlternatingStyle.BackColor = System.Drawing.Color.LightGray;
    headerStyle.BackColor = System.Drawing.Color.LightGray;//背景色
    headerStyle.Font.Bold = true; //字体
    headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
    itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; ;
    excel.AlternatingItemStyle.MergeWith(AlternatingStyle);
    excel.HeaderStyle.MergeWith(headerStyle);
    excel.ItemStyle.MergeWith(itemStyle);
    excel.GridLines = GridLines.Both;
    excel.HeaderStyle.Font.Bold = true;
    excel.DataSource = dt.DefaultView;
    excel.DataBind();
    excel.RenderControl(htmlWriter);
    string filestr = Server.MapPath(filePath);
    int pos = filestr.LastIndexOf("\\");
    string file = filestr.Substring(0, pos);
    if (!Directory.Exists(file))
    {
    Directory.CreateDirectory(file);
    }
    System.IO.StreamWriter sw = new StreamWriter(filestr);
    sw.Write(stringWriter.ToString());
    sw.Close();

    Response.ContentEncoding = System.Text.Encoding.UTF8;
    Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName));
    Response.ContentType = "application/ms-excel";
    Response.WriteFile(filestr);
    Response.End();
    }

    /// <summary>
    /// 导出到Excel lichenghu
    /// </summary>
    /// <param name="dt"></param>
    public static void ToExcel(DataTable dt)
    {
    string sb = "";

    foreach (DataRow dr in dt.Rows)
    {
    for (int i = 0; i < dt.Columns.Count; i++)
    {
    sb = sb + dr[i].ToString() + "\t";
    }
    sb = sb + "\n";
    }

    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=MyExcel.xls");
    HttpContext.Current.Response.Charset = "UTF-8";
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
    HttpContext.Current.Response.ContentType = "application/ms-excel";

    System.IO.StringWriter tw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
    hw.WriteLine(sb.ToString());
    HttpContext.Current.Response.Write(tw.ToString());

    HttpContext.Current.Response.End();

    hw.Flush();
    hw.Close();
    tw.Flush();
    tw.Close();
    }

  • 相关阅读:
    云原生体系下 Serverless 弹性探索与实践
    PaddlePaddle:在 Serverless 架构上十几行代码实现 OCR 能力
    manjaro kde系统格式化U盘
    从B站看到的资源网站(确实是很不错,推荐!)
    ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysql 或 manjaro 安装 mariadb
    manjaro kde 我安装后使用到的命令
    思考了许久,博客在未来将会进行的改变(2021-10-03)
    linux 下配置 python源
    ModuleNotFoundError: No module named 'distutils.util'
    mysqlclient 安装失败
  • 原文地址:https://www.cnblogs.com/chenghu/p/2606495.html
Copyright © 2020-2023  润新知