 |
| General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category.
** PLEASE BE SPECIFIC WITH YOUR QUESTION **
When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 21st, 2005, 04:39 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How to export data into Excel
Hi
I need a code to export data into excel using C#.
Any one can send the code?
|
|

May 21st, 2005, 06:24 AM
|
|
Authorized User
|
|
Join Date: Nov 2004
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi
See this
oControl.Page.Response.Clear();
oControl.Page.Response.Buffer=true;
oControl.Page.Response.ContentType = "application/vnd.ms-excel";
oControl.Page.Response.Charset = "";
oControl.Page.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
oControl.RenderControl(oHtmlTextWriter);
oControl.Page.Response.Write(oStringWriter.ToStrin g());
oControl.Page.Response.End();
oControl is any server control (datagrid, table etc)
Avanish
|
|

May 21st, 2005, 06:48 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have already tried with this code but this displays pagewise and when i click for exporting only current page is exporting to Excel,I need to do whole record from database.
|
|

May 21st, 2005, 07:28 AM
|
|
Authorized User
|
|
Join Date: Nov 2004
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi
Import office web component and use this
Dim oXL As New Excel.Application
Dim oBooks As Excel.Workbooks = oXL.Workbooks
Dim oWB As Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet = oXL.ActiveSheet
oSheet.Cells(1, 1).value = "A"
oSheet.Cells(2, 1).value = "B"
oSheet.Cells(3, 1).value = "C"
oSheet.Cells(1, 2).value = "D"
oSheet.Cells(2, 2).value = "E"
oSheet.Cells(3, 2).value = "F"
oXL.ActiveWorkbook.SaveAs(SaveAsPath)
NAR(oSheet)
oWB.Close(False)
NAR(oWB)
oBooks.Close()
NAR(oBooks)
oXL.Application.Quit()
NAR(oXL)
oXL = Nothing
Avanish
|
|

May 24th, 2005, 01:55 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
what are all these:
Dim oXL As New Excel.Application
Dim oBooks As Excel.Workbooks = oXL.Workbooks
Dim oWB As Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet = oXL.ActiveSheet
I imported office web component,but getting error. I need the code in C#.
|
|

May 30th, 2005, 02:02 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Very strange that no one has replied correctly for this query.
I need it very urgently.
|
|

May 31st, 2005, 02:12 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2004
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
What u mean by "What are all these"-----
Do u have set the rerences
Go to Add Refrences--> Com
Add Microsoft Excel object lib 11 or 10 .....
|
|

August 9th, 2005, 06:48 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 345
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Exporting datagrid to excel might sounds complex but its pretty simple. Let's see how this can be done.
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.No Cache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
myDataGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
Gokulan Ethiraj
|
|

January 11th, 2006, 10:02 PM
|
|
Registered User
|
|
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
the coding is juat can send the first page of the data(if data grid in paing). It can not export all the data to excel.
Any solution please. Thanx very much
|
|
 |