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