Monday, January 16, 2012

EXPORT EXCEL USING ABSTRACT CLASS

#region Export

public abstract class DataGridExporterBase
{
///
/// Holds a reference to the datagrid being exported
///
protected DataGrid MyDataGrid;

///
/// Holds a reference to the page where the datagrid locates
///
protected Page CurrentPage;

///
/// Overloaded. Initializes a new instance of the DataGridExporterBase class.
///
/// The datagrid to be exported
/// The page to which the datagrid is to be exported
public DataGridExporterBase(DataGrid dg, Page pg)
{
MyDataGrid = dg;
CurrentPage = pg;
}

///
/// Overloaded. Initializes a new instance of the DataGridExporterBase class.
///
/// The datagrid to be exported
public DataGridExporterBase(DataGrid dg)
: this(dg, dg.Page)
{
}

///
/// Exports the current datagrid
///
public abstract void Export();
}
public void ExportLinkButton_Click(object sender, System.EventArgs e)
{
string strTitle = "MOISIS";
new DataGridExcelExporter(gvSupplierList, this.Page).Export("MOISIS.xls");
}

public class DataGridExcelExporter : DataGridExporterBase
{

/// file:///\\Websrv\public\currentprojects\MOISIS\En\FinanceUser\DownloadPostedSupplier.aspx.cs
/// CSS file for decoration, se it if any or dont use it
///
//private const string MY_CSS_FILE = "./css/MDF.css";
//private const string MY_CSS_FILE = "";

///
/// Overloaded. Initializes a new instance of the DataGridExcelExporter class.
///
/// The datagrid to be exported
/// The page to which the datagrid is to be exported
public DataGridExcelExporter(DataGrid dg, Page pg)
: base(dg, pg)
{
}

///
/// Overloaded. Initializes a new instance of the DataGridExcelExporter class.
///
/// The datagrid to be exported
public DataGridExcelExporter(DataGrid dg)
: base(dg)
{
}

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

///
/// Renders the html text before the datagrid.
///
/// A HtmlTextWriter to write html to output stream
protected virtual void FrontDecorator(HtmlTextWriter writer)
{
writer.WriteFullBeginTag("HTML");
writer.WriteFullBeginTag("Head");
writer.RenderBeginTag(HtmlTextWriterTag.Style);
writer.Write("");
writer.RenderEndTag();
writer.WriteEndTag("Head");
writer.WriteFullBeginTag("Body");
}

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

///
/// Exports the datagrid to an Excel file with the name of the datasheet provided by the passed in parameter
///
/// Name of the datasheet.
///
public virtual void Export(string reportName)
{
ClearChildControls(MyDataGrid);
MyDataGrid.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=" + 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);
MyDataGrid.RenderControl(htmlWriter);
RearDecorator(htmlWriter);

//Write the content to the web browser
CurrentPage.Response.Write(stringWriter.ToString());
CurrentPage.Response.End();
}

///
/// Iterates a control and its children controls, ensuring they are all LiteralControls
///
/// Only LiteralControl can call RenderControl(System.Web.UI.HTMLTextWriter htmlWriter) method. Otherwise
/// a runtime error will occur. This is the reason why this method exists.
///
///
/// The control to be cleared and verified
private void RecursiveClear(Control control)
{
//Clears children controls
for (int i = control.Controls.Count - 1; i >= 0; i--)
{
RecursiveClear(control.Controls[i]);
}

//
//If it is a LinkButton, convert it to a LiteralControl
//
if (control is LinkButton)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = ((LinkButton)control).Text;
control.Parent.Controls.Remove(control);
}
//We don't need a button in the excel sheet, so simply delete it
else if (control is Button)
{
control.Parent.Controls.Remove(control);
}

//If it is a ListControl, copy the text to a new LiteralControl
else if (control is ListControl)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = ((ListControl)control).SelectedItem.Text;
}
catch
{
}
control.Parent.Controls.Remove(control);

}
//You may add more conditions when necessary

return;
}

///
/// Clears the child controls of a Datagrid to make sure all controls are LiteralControls
///
/// Datagrid to be cleared and verified
protected void ClearChildControls(DataGrid dg)
{

for (int i = dg.Columns.Count - 1; i >= 0; i--)
{
DataGridColumn column = dg.Columns[i];
if (column is ButtonColumn)
{
dg.Columns.Remove(column);
}
}

this.RecursiveClear(dg);

}

}
public void ColumnWidth(Object sender, DataGridItemEventArgs e)
{



//Response.End();
/*if ((e.Item.ID == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.AlternatingItem))
{

}
*/
}

/****** End Export********/
#endregion

No comments: