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

  • 相关阅读:
    CentOS7 彻底关闭 IPV6
    查看 nodejs 安装包的相关指令
    npm 查看全局安装过的包
    更换 nodejs npm 镜像为 淘宝 镜像
    更改 Centos 6 的 yum 源
    Nodejs 实现 WebSocket 太容易了吧!!
    解决国内 NPM 安装依赖速度慢问题
    详解 HTML5 中的 WebSocket 及实例代码-做弹幕
    JSmpeg-用JavaScript编写的视频播放器
    适用于Centos6.x系统的15项优化脚本
  • 原文地址:https://www.cnblogs.com/jordan2009/p/2002973.html
Copyright © 2011-2022 走看看