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

  • 相关阅读:
    【matlab】学习笔记 2脚本编写
    【matlab】学习笔记 1 入门简单操作
    【matlab】学习笔记 3 函数编写
    MySQL学习笔记
    数据库连接-----MySQL -> JDBC
    leetcode——Mysql数据库查询题目
    不同单词个数统计
    初始化二维数组
    JS基本变量类型和对象杂谈
    LeetCode Clone Graph
  • 原文地址:https://www.cnblogs.com/jordan2009/p/2002973.html
Copyright © 2011-2022 走看看