On Jul 13, 10:44 pm, Steve Kershaw <steve_kers...@ yahoo.comwrote:
Hi,
I'm working on a project in which I have a Gridview that has data.
That Gridview data must be then exported to an Excel spreadsheet.
I have successfully displayed the Excel spreadsheet and fill it with
data using the Range.InvokeMem ber(...) method:
Range range2 = worksheet.get_R ange("A1", "L1");
Object[] args2 = new Object[1];
args2[0] = header; // an array of 12 members
range2.GetType( ).InvokeMember( "Value", BindingFlags.Se tProperty,
null, range2, args2);
So I know how to do that. How do I extract the data from the
GridView???
Thanks in advance for your help.
Steve
hi,
try this modify a bit as your enviormrnt
//data view use for grid bing
protected DataView dvB = null;
protected void ExportToExcel()
{
Response.Clear( );
Response.AddHea der("content-disposition",
"attachment;fil ename=FileName. xls");
Response.Conten tType = "applicatio n/vnd.xls";
System.IO.Strin gWriter stringWrite = new
System.IO.Strin gWriter();
System.Web.UI.H tmlTextWriter htmlWrite = new
HtmlTextWriter( stringWrite);
DataGrid dg = new DataGrid();
//get table fron dataview....in your case some how try to
get the table
DataTable temp = dvB.Table.Clone ();
DataTable exportTable = dvB.Table;
for (int i = 0; i < temp.Columns.Co unt; i++)
{
if (temp.Columns[i].ColumnName.Con tains("Id") == true)
{
exportTable.Col umns.Remove(tem p.Columns[i].ColumnName);
}
}
dg.DataSource = exportTable;
dg.DataBind();
dg.RenderContro l(htmlWrite);
Response.Write( stringWrite.ToS tring());
Response.End();
}
hope help
nahid
http://nahidulkibria.blogspot.com/ http://www.kaz.com.bd