网页excel导出类

 using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;


namespace Etmc.App.ETicket.Utility
{
 /// <summary>
 /// Exports a Control to a excel file.
 /// </summary>
 /// <requirements>Microsoft Excel 97 or above should be installed on the client machine in order to make
 /// this function work
 /// </requirements>
 public class ExcelExporter : ExporterBase
 {  

  /// <summary>
  /// Overloaded. Initializes a new instance of the DataGridExcelExporter class.
  /// </summary>
  /// <param name="control">The Control to be exported</param>
  /// <param name="pg">The page to which the datagrid is to be exported</param>
  public ExcelExporter(Control control, Page pg):base(control, pg)
  {
  }

  /// <summary>
  /// Overloaded. Initializes a new instance of the ExportExcelUtility class.
  /// </summary>
  /// <param name="dg">The datagrid to be exported</param>
  public ExcelExporter(Control control):base(control)
  {
  }

  /// <summary>
  /// Overloaded. Exports a Control to an excel file, the title of which is empty
  /// </summary>
  public override void Export()
  {
   Export(String.Empty);
  }

  /// <summary>
  /// Renders the html text before the datagrid.
  /// </summary>
  /// <param name="writer">A HtmlTextWriter to write html to output stream</param>
  protected virtual void FrontDecorator(HtmlTextWriter writer)
  {
   writer.WriteFullBeginTag("HTML");
   writer.WriteFullBeginTag("Head");
   writer.Write("<meta http-equiv=/"Content-Type/" content=/"text/html; charset=gb2312/">");
   writer.RenderBeginTag(HtmlTextWriterTag.Style);
   writer.Write("<!--");
   
   string strCss = ResUtility.GetStringFullName(
    typeof(ExcelExporter),
    "Etmc.App.ETicket.Utility.MDF.css");
   writer.Write(strCss);

   writer.Write("-->");
   writer.RenderEndTag();
   writer.WriteEndTag("Head");
   writer.WriteFullBeginTag("Body");
  }

  /// <summary>
  /// Renders the html text after the datagrid.
  /// </summary>
  /// <param name="writer">A HtmlTextWriter to write html to output stream</param>
  protected virtual void RearDecorator(HtmlTextWriter writer)
  {
   writer.WriteEndTag("Body");
   writer.WriteEndTag("HTML");
  }

  /// <summary>
  /// Exports the datagrid to an Excel file with the name of the datasheet provided by the passed in parameter
  /// </summary>
  /// <param name="reportName">Name of the datasheet.
  /// </param>
  public virtual void Export(string reportName)
  {
   this.MyControl.EnableViewState = false;
   //Gets rid of the viewstate of the control. The viewstate may make an excel file unreadable.
  
   CurrentPage.Response.Clear();
   CurrentPage.Response.Buffer = true;

   //This will make the browser interpret the output as an Excel file
   CurrentPage.Response.AddHeader( "Content-Disposition", "filename="+HttpUtility.UrlPathEncode(reportName));
   CurrentPage.Response.ContentType="application/vnd.ms-excel";

   //Prepares the html and write it into a StringWriter
   StringWriter stringWriter = new StringWriter();
   HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
   FrontDecorator(htmlWriter);
   MyControl.RenderControl(htmlWriter);
   RearDecorator(htmlWriter);

   //Write the content to the web browser
   CurrentPage.Response.Write(stringWriter.ToString());
   CurrentPage.Response.End();
  }  
 }
 /// <summary>
 /// Serves as the base class that defines the methods, properties and events common
 /// to all WebControl exporters in the Web.Generic.DataGridTools
 /// </summary>
 public abstract class ExporterBase
 {
  /// <summary>
  /// Holds a reference to the WebControl being exported
  /// </summary>
  protected Control MyControl;

  /// <summary>
  /// Holds a reference to the page where the Control locates
  /// </summary>
  protected Page CurrentPage;

  /// <summary>
  /// Overloaded. Initializes a new instance of the ExporterBase class.
  /// </summary>
  /// <param name="control">The Control to be exported</param>
  /// <param name="pg">The page to which the Control is to be exported</param>
  public ExporterBase(Control control, Page pg)
  {
   MyControl = control;
   CurrentPage = pg;
  }

  /// <summary>
  /// Overloaded. Initializes a new instance of the ExporterBase class.
  /// </summary>
  /// <param name="dg">The datagrid to be exported</param>
  public ExporterBase(Control control):this(control, control.Page)
  {
  }

  /// <summary>
  /// Exports the current Control
  /// </summary>
  public abstract void Export();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值