Esta forma tiene la contra que si el cliente tiene Excell 2007 le va a tirar un warning y va a tener que confirmar un cartel antes de abrir el archivo. Suponiendo que tenemos. Una forma de usar esta clase sería.using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.IO;
///
/// This class exports an IEnumerable to Excell
/// It generates a response
///
public class ExcelExporter
{
IEnumerable _content = null;
String _filename = String.Empty;
DataControlFieldCollection _columns = new DataControlFieldCollection();
private string GetControlAsHtml(Control ctrl)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htm = new HtmlTextWriter(sw);
ctrl.RenderControl(htm);
return sw.ToString();
}
private void ConfigureColumns(GridView grid)
{
if (this._columns.Count == 0)
{
grid.AutoGenerateColumns = true;
}
else
{
grid.AutoGenerateColumns = false;
grid.Columns.Clear();
foreach (DataControlField f in this._columns)
{
grid.Columns.Add(f);
}
}
}
public ExcelExporter(String fileName, IEnumerable content)
{
this._content = content;
this._filename = fileName;
}
public ExcelExporter(String fileName, IEnumerable content, DataControlFieldCollection columns) : this(fileName, content)
{
this.Columns = columns;
}
public ExcelExporter()
{
//
// TODO: Add constructor logic here
//
}
public String Filename
{
get { return this._filename; }
set { this._filename = value; }
}
public IEnumerable Content
{
get { return this._content; }
set { this._content = value; }
}
public DataControlFieldCollection Columns
{
get
{
return this._columns;
}
set
{
this._columns.Clear();
foreach (DataControlField f in value)
{
this._columns.Add(f);
}
}
}
public void ExportToExcell(HttpResponse response)
{
if (this._content == null) throw new Exception("ExcelExporter: Error exporting to Excel. \n No Content Specified.");
if (this._filename == String.Empty) throw new Exception("ExcelExporter: Error exporting to Excel. \n No output filename specified.");
response.Clear();
response.AddHeader("content-disposition", string.Format("attachment; filename={0}", this._filename));
response.ContentType = "application/ms-excel";
GridView grid = new GridView();
this.ConfigureColumns(grid);
grid.DataSource = this._content;
grid.DataBind();
response.Write(GetControlAsHtml(grid));
response.End();
}
}
o suponiendo que hay una gridview con datos y queremos usar sus columnas.
ExcelExporter exporter = new ExcelExporter("archivo.xls",
FinderObjetoPrueba.FindAll());
exporter.ExportToExcell(Response);
ExcelExporter exporter = new ExcelExporter("archivo.xls",
FinderObjetoPrueba.FindAll(),
grdDatos.Columns);
exporter.ExportToExcell(Response);
En ambos casos FinderObjetoPrueba es una clase que nos devuelve una lista de objetos y en el ejemplo completo funciona como object data source de grdDatos y Response es la clase de Page.Response donde escribiremos el html que por el content-type sera abierto por el excell del lado del cliente
No hay comentarios:
Publicar un comentario