Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old May 21st, 2005, 04:39 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Hyderabad, , India.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to export data into Excel

Hi
I need a code to export data into excel using C#.
Any one can send the code?




  #2 (permalink)  
Old May 21st, 2005, 06:24 AM
Authorized User
 
Join Date: Nov 2004
Location: Indore, MP, India.
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to avanishp
Default

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

  #3 (permalink)  
Old May 21st, 2005, 06:48 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Hyderabad, , India.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

  #4 (permalink)  
Old May 21st, 2005, 07:28 AM
Authorized User
 
Join Date: Nov 2004
Location: Indore, MP, India.
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to avanishp
Default

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

  #5 (permalink)  
Old May 24th, 2005, 01:55 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Hyderabad, , India.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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#.



  #6 (permalink)  
Old May 30th, 2005, 02:02 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Hyderabad, , India.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Very strange that no one has replied correctly for this query.
I need it very urgently.

  #7 (permalink)  
Old May 31st, 2005, 02:12 AM
Friend of Wrox
 
Join Date: Sep 2004
Location: , , India.
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 .....

  #8 (permalink)  
Old August 9th, 2005, 06:48 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , India.
Posts: 345
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to gokul_blr Send a message via Yahoo to gokul_blr
Default

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
  #9 (permalink)  
Old January 11th, 2006, 10:02 PM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Export to Excel (Data only) nikks9 ASP.NET 1.0 and 1.1 Professional 3 February 9th, 2011 05:01 AM
Export Data to Excel seananderson Access 2 January 16th, 2007 12:30 PM
Export Data to Excel msbsam ASP.NET 2.0 Basics 0 November 8th, 2006 02:09 AM
export data to excel tulincim Classic ASP Databases 0 August 22nd, 2005 03:34 PM
export data to Excel?? amy_mgfe Excel VBA 0 January 15th, 2005 10:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.