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

  • 相关阅读:
    C. Mobile phones
    star
    敌兵不正
    Java数据类型
    Eclipse的使用及Java程序的标识符和关键字
    Python学习笔记字符串操作之小结之表格打印
    HTML学习笔记7----图像
    Python学习笔记字符串操作之小结之Wiki标记中添加无序列表
    Python学习笔记字符串操作之pyperclip模块拷贝粘贴字符串
    Python学习笔记字符串操作之strip()、rstrip()和lstrip()方法删除空白格
  • 原文地址:https://www.cnblogs.com/jordan2009/p/2002973.html
Copyright © 2020-2023  润新知