zoukankan      html  css  js  c++  java
  • 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();
    }

  • 相关阅读:
    北京高考零分作文(看到最后一句笑喷了!)
    关于前几天无法访问的问题
    用 PHP 读取和编写 XML DOM[转]
    Delphi对INI文件的详细操作方法
    如何在WebService中获取客户端的IP地址
    正则表达式30分钟入门教程
    [原创]shell对xml操作的脚本
    预防SQL注入攻击之我见(好文章)
    表驱动方法(非常好的数据结构)
    请教shell读写XML问题
  • 原文地址:https://www.cnblogs.com/chenghu/p/2606495.html
Copyright © 2011-2022 走看看