HighTechTalks DotNet Forums  

exporting to excel

ASP.net Data Grid Control microsoft.public.dotnet.framework.aspnet.datagridcontrol


Discuss exporting to excel in the ASP.net Data Grid Control forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Vince13
 
Posts: n/a

Default exporting to excel - 07-27-2007 , 05:34 PM






I am attempting to export a DataGrid to Excel.

I have tried code from this link:
http://www.codeproject.com/office/ex...lect=1703 842


and it seemed like nothing happened.

I also tried the code here:
http://www.c-sharpcorner.com/UploadF...idToExcel.aspx


this time excel actually popped up, but the only thing in the grid was:
Results Per Page: 15 Records Found:Label1

Any ideas? Thanks.

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...agrid/200707/1


Reply With Quote
  #2  
Old   
Alvin Bruney [MVP]
 
Posts: n/a

Default Re: exporting to excel - 08-02-2007 , 08:59 AM






couple things. make sure that the grid is the only thing on the page.
nothing else. then make sure you actually have data in your grid by using a
check on datagrid.tables.rows.count > 0

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
https://www.microsoft.com/MSPress/books/10933.aspx
OWC Black Book www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley


"Vince13" <u35350@uwe> wrote

Quote:
I am attempting to export a DataGrid to Excel.

I have tried code from this link:
http://www.codeproject.com/office/ex...lect=1703 842


and it seemed like nothing happened.

I also tried the code here:
http://www.c-sharpcorner.com/UploadF...idToExcel.aspx


this time excel actually popped up, but the only thing in the grid was:
Results Per Page: 15 Records Found:Label1

Any ideas? Thanks.

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...agrid/200707/1




Reply With Quote
  #3  
Old   
Vince13 via DotNetMonster.com
 
Posts: n/a

Default Re: exporting to excel - 08-06-2007 , 04:20 PM



Sorry for the late reply. . .

Okay, I used the code from the second link on a new page on which I only had
the datagrid. I get the same result as before (Results Per Page: 15 Records
Found:Label1) which seems to be coming off of the previous page. One time,
for which I am sure that I changed nothing, I did get it to work, but just as
magically as it was there, it was gone again. Any more ideas?

Thanks!

Alvin Bruney [MVP] wrote:
Quote:
couple things. make sure that the grid is the only thing on the page.
nothing else. then make sure you actually have data in your grid by using a
check on datagrid.tables.rows.count > 0

I am attempting to export a DataGrid to Excel.

[quoted text clipped - 10 lines]

Any ideas? Thanks.
--
Message posted via http://www.dotnetmonster.com



Reply With Quote
  #4  
Old   
Alvin Bruney [MVP]
 
Posts: n/a

Default Re: exporting to excel - 08-07-2007 , 09:08 PM



Send the code in the test page, make sure it's C#. I'm tired of looking at
VB for a day.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
https://www.microsoft.com/MSPress/books/10933.aspx
OWC Black Book www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley


"Vince13 via DotNetMonster.com" <u35350@uwe> wrote

Quote:
Sorry for the late reply. . .

Okay, I used the code from the second link on a new page on which I only
had
the datagrid. I get the same result as before (Results Per Page: 15
Records
Found:Label1) which seems to be coming off of the previous page. One
time,
for which I am sure that I changed nothing, I did get it to work, but just
as
magically as it was there, it was gone again. Any more ideas?

Thanks!

Alvin Bruney [MVP] wrote:
couple things. make sure that the grid is the only thing on the page.
nothing else. then make sure you actually have data in your grid by using
a
check on datagrid.tables.rows.count > 0

I am attempting to export a DataGrid to Excel.

[quoted text clipped - 10 lines]

Any ideas? Thanks.

--
Message posted via http://www.dotnetmonster.com




Reply With Quote
  #5  
Old   
Vince13 via DotNetMonster.com
 
Posts: n/a

Default Re: exporting to excel - 08-08-2007 , 09:05 AM



Okay here's the code for the page

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
DataTable myTable = new DataTable();
string command = (string)Session["command"]; //this is my SQL select command
SqlConnection myConnection = new SqlConnection("data
source=SQLSERVER;initial catalog=Catalog;persist security info=False;user
id=sa;workstation id=ELCORE28;packet size=4096");
SqlDataAdapter myAdapter = new SqlDataAdapter(command, myConnection);
myAdapter.Fill(myTable);
myConnection.Close();

DataGrid1.DataSource = myTable;
DataGrid1.DataBind();

Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.
HtmlTextWriter (oStringWriter);
this.ClearControls(DataGrid1);
DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
//Button1_Click(sender, e);
}

private void ClearControls(Control control)
{
for (int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedIte m").
GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetV alue
(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}


Thanks again for taking a look.


Alvin Bruney [MVP] wrote:
Quote:
Send the code in the test page, make sure it's C#. I'm tired of looking at
VB for a day.

Sorry for the late reply. . .

[quoted text clipped - 20 lines]

Any ideas? Thanks.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...agrid/200708/1



Reply With Quote
  #6  
Old   
Alvin Bruney [MVP]
 
Posts: n/a

Default Re: exporting to excel - 08-10-2007 , 09:32 PM



comment out the set of lines after
Quote:
Response.ContentType = "application/vnd.ms-excel";
replace with this set of code.
StringWriter tw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(tw);

DataGrid1.RenderControl(hw);

Response.Write(tw.ToString());

Response.End();


--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc

"Vince13 via DotNetMonster.com" <u35350@uwe> wrote

Quote:
Okay here's the code for the page

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
DataTable myTable = new DataTable();
string command = (string)Session["command"]; //this is my SQL select
command
SqlConnection myConnection = new SqlConnection("data
source=SQLSERVER;initial catalog=Catalog;persist security info=False;user
id=sa;workstation id=ELCORE28;packet size=4096");
SqlDataAdapter myAdapter = new SqlDataAdapter(command, myConnection);
myAdapter.Fill(myTable);
myConnection.Close();

DataGrid1.DataSource = myTable;
DataGrid1.DataBind();

Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.
HtmlTextWriter (oStringWriter);
this.ClearControls(DataGrid1);
DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
//Button1_Click(sender, e);
}

private void ClearControls(Control control)
{
for (int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedIte m").
GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetV alue
(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}


Thanks again for taking a look.


Alvin Bruney [MVP] wrote:
Send the code in the test page, make sure it's C#. I'm tired of looking at
VB for a day.

Sorry for the late reply. . .

[quoted text clipped - 20 lines]

Any ideas? Thanks.

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...agrid/200708/1




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.