![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
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. |
#4
| |||
| |||
|
|
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 |
#5
| |||
| |||
|
|
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. |
#6
| |||
| |||
|
|
Response.ContentType = "application/vnd.ms-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("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 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |