|
|
|
start date: Fri, 27 Jul 2007 21:34:42 GMT,
posted on: microsoft.public.dotnet.framework.aspnet.datagridcontrol
back
| Thread Index |
|
1
Vince13 u35350@uwe
|
|
2
Alvin Bruney [MVP] some guy without an email address
|
|
3
Vince13 via DotNetMonster.com u35350@uwe
|
|
4
Alvin Bruney [MVP] some guy without an email address
|
|
5
Vince13 via DotNetMonster.com u35350@uwe
|
|
6
Alvin Bruney [MVP] some guy without an email address
|
exporting to excel
I am attempting to export a DataGrid to Excel.
I have tried code from this link:
http://www.codeproject.com/office/export.asp?df=100&forumid=155463&exp=0&select=1703842
and it seemed like nothing happened.
I also tried the code here:
http://www.c-sharpcorner.com/UploadFile/DipalChoksi/ExportASPNetDataGridToExcel11222005041447AM/ExportASPNetDataGridToExcel.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/Forums.aspx/asp-net-datagrid/200707/1
Date:Fri, 27 Jul 2007 21:34:42 GMT
Author:
|
Re: exporting to excel
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 in message news:75cf1a5c06b99@uwe...
>I am attempting to export a DataGrid to Excel.
>
> I have tried code from this link:
> http://www.codeproject.com/office/export.asp?df=100&forumid=155463&exp=0&select=1703842
>
>
> and it seemed like nothing happened.
>
> I also tried the code here:
> http://www.c-sharpcorner.com/UploadFile/DipalChoksi/ExportASPNetDataGridToExcel11222005041447AM/ExportASPNetDataGridToExcel.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/Forums.aspx/asp-net-datagrid/200707/1
>
Date:Thu, 2 Aug 2007 08:59:16 -0400
Author:
|
Re: exporting to excel
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
Date:Mon, 06 Aug 2007 20:20:32 GMT
Author:
|
Re: exporting to excel
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 in message
news:764e173330948@uwe...
> 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
>
Date:Tue, 7 Aug 2007 21:08:52 -0400
Author:
|
Re: exporting to excel
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("SelectedItem").
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").GetValue
(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/Forums.aspx/asp-net-datagrid/200708/1
Date:Wed, 08 Aug 2007 13:05:09 GMT
Author:
|
Re: exporting to excel
comment out the set of lines after
> 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 in message
news:76636f689b43a@uwe...
> 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("SelectedItem").
> 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").GetValue
> (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/Forums.aspx/asp-net-datagrid/200708/1
>
Date:Fri, 10 Aug 2007 21:32:09 -0400
Author:
|
|
|