zoukankan      html  css  js  c++  java
  • asp.net 导出数据到Excle

    public class PrinExcel
    {
    public PrinExcel()
    {
    //
    //TODO: 在此处添加构造函数逻辑
    //
    }
    public static void ToExcel(System.Web.UI.Control ctl, string strFileName)
    {
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");
    HttpContext.Current.Response.Charset = "utf-8";

    string style = @"<style> .text { } </script> "; //Excel中的文本格式

    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
    //System.Text.Encoding.Default;

    HttpContext.Current.Response.ContentType = "application/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();
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

    ctl.RenderControl(hw);
    HttpContext.Current.Response.Write(style);
    HttpContext.Current.Response.Write(tw.ToString());
    HttpContext.Current.Response.End();
    hw.Flush();
    hw.Close();
    tw.Flush();
    tw.Close();

    }
    //导出到Excel
    public static void ToExcel2(System.Web.UI.Control ctl, string strFileName)
    {
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");
    HttpContext.Current.Response.Charset = "GB2312"; //"utf-8";

    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
    //System.Text.Encoding.Default;

    HttpContext.Current.Response.ContentType = "application/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();
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

    ctl.RenderControl(hw);
    HttpContext.Current.Response.Write(tw.ToString());
    HttpContext.Current.Response.End();
    hw.Flush();
    hw.Close();
    tw.Flush();
    tw.Close();

    }
    //导出到Excel
    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();
    }

    //导出到Excel
    public static void ToExcel(string sb)
    {
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "inline;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();
    }

  • 相关阅读:
    算法笔记_187:历届试题 网络寻路(Java)
    算法笔记_186:历届试题 高僧斗法(Java)
    算法笔记_185:历届试题 格子刷油漆(Java)
    Tomcat,JBoss与JBoss Web
    JBoss Web和Tomcat的区别
    tomcat与jboss等容器的区别
    dubbo
    ZooKeeper学习第一期---Zookeeper简单介绍
    Java GC系列
    Tomcat中JVM内存溢出及合理配置
  • 原文地址:https://www.cnblogs.com/jordan2009/p/2002973.html
Copyright © 2011-2022 走看看