How to transfer data from a GridView to an Excel worksheet.

S

Steve Kershaw

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.InvokeMember(...) method:

Range range2 = worksheet.get_Range("A1", "L1");
Object[] args2 = new Object[1];
args2[0] = header; // an array of 12 members
range2.GetType().InvokeMember("Value", BindingFlags.SetProperty,
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
 
N

nahid

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.InvokeMember(...) method:

Range range2 = worksheet.get_Range("A1", "L1");
Object[] args2 = new Object[1];
args2[0] = header; // an array of 12 members
range2.GetType().InvokeMember("Value", BindingFlags.SetProperty,
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.AddHeader("content-disposition",
"attachment;filename=FileName.xls");
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new
System.IO.StringWriter();
System.Web.UI.HtmlTextWriter 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.Count; i++)
{
if (temp.Columns.ColumnName.Contains("Id") == true)
{

exportTable.Columns.Remove(temp.Columns.ColumnName);
}
}

dg.DataSource = exportTable;
dg.DataBind();
dg.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}


hope help

nahid
http://nahidulkibria.blogspot.com/
http://www.kaz.com.bd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top