常用的代码堆积!一,导出excel

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Text;
  11. using System.IO;
  12. //using System.Reflection;
  13. //using System.Runtime.InteropServices;
  14. //using Microsoft.Office.Tools.Excel;
  15. //using Microsoft.Office;
  16. /// <summary>
  17. /// OutPutExcel 的摘要说明
  18. /// </summary>
  19. public class OutPutExcel
  20. {
  21.     public OutPutExcel()
  22.     {
  23.         //
  24.         // TODO: 在此处添加构造函数逻辑
  25.         //
  26.     }
  27.     /// <summary>
  28.     /// 导出为Excel表格
  29.     /// </summary>
  30.     /// <param name="filename"></param>
  31.     /// <param name="dg"></param>
  32.     public static void OutExcel(string filename,DataGrid dg)
  33.     {
  34.         string strFileName =filename;
  35.         System.IO.StringWriter SW = new System.IO.StringWriter();
  36.         System.Web.UI.HtmlTextWriter HTW = new System.Web.UI.HtmlTextWriter(SW);
  37.         //this.dg.AllowPaging = false;
  38.        // GVDataBind();
  39.         dg.RenderControl(HTW);
  40.         //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
  41.         System.Web.HttpContext.Current.Response.Buffer = true;
  42.         System.Web.HttpContext.Current.Response.Clear();
  43.         System.Web.HttpContext.Current.Response.ClearContent();
  44.         System.Web.HttpContext.Current.Response.ClearHeaders();
  45.         System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
  46.         //Response.ContentType是输出流的 HTTP MIME 类型
  47.         //Response.ContentType     --- word文件
  48.         //application/vnd.ms-excel --- excel文件
  49.         //...
  50.         System.Web.HttpContext.Current.Response.Charset = "gb2312";
  51.         System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
  52.         System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition""attachment;filename=" + strFileName + ".xls");
  53.         //inline --- 在线打开
  54.         //filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
  55.         //进行进行编码,以解决文件名乱码的问题
  56.         System.Web.HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content=/"text/html; charset=gb2312/">");
  57.         System.Web.HttpContext.Current.Response.Write(SW.ToString());
  58.         System.Web.HttpContext.Current.Response.Write("</body></html>");
  59.         System.Web.HttpContext.Current.Response.Flush();
  60.         System.Web.HttpContext.Current.Response.Close();
  61.     }
  62. }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值