473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export to Excel

145 New Member
I should be getting 580 records but getting none (not even the header) to my excel file.

Button Code
Expand|Select|Wrap|Line Numbers
  1.     protected void btnExport_Click(object sender, EventArgs e)
  2.     {
  3.         Export2Excel e2e = new Export2Excel();
  4.         e2e.Prepare4Excel();
  5.     }
  6.  
Class Code
Expand|Select|Wrap|Line Numbers
  1. public class Export2Excel
  2. {
  3.     public Export2Excel()
  4.     {
  5.  
  6.     }    
  7.     public void Prepare4Excel()
  8.     {
  9.         string connStr = ConfigurationManager.ConnectionStrings["myCS"].ConnectionString;
  10.         string strSQL = "Select * from Employees";
  11.         SqlConnection conn = new SqlConnection(connStr);
  12.  
  13.         SqlCommand command = new SqlCommand(strSQL, conn);
  14.         SqlDataAdapter sda = new SqlDataAdapter(command);
  15.         DataSet ds = new DataSet();
  16.         sda.Fill(ds);
  17.         BindExcel(ds);
  18.         conn.Close();
  19.     }
  20.  
  21.     public void BindExcel(DataSet ds)
  22.     {
  23.         HttpContext.Current.Response.Clear();
  24.         HttpContext.Current.Response.Charset = "";
  25.         HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
  26.  
  27.         StringWriter sw = new StringWriter();
  28.         HtmlTextWriter htw = new HtmlTextWriter(sw);
  29.         DataGrid dg = new DataGrid();
  30.         dg.DataSource = ds.Tables[0];
  31.         dg.AllowPaging = true;
  32.         dg.DataBind();
  33.         dg.RenderBeginTag(htw);
  34.         HttpContext.Current.Response.Write(sw.ToString());
  35.         HttpContext.Current.Response.End();    
  36.     }
  37.  
  38. }
  39.  
  40.  
Apr 15 '09 #1
4 3846
dorandoran
145 New Member
Anyone? cause I am working on a deadline and working on this for 48 hours.
Apr 16 '09 #2
dorandoran
145 New Member
I finally got it work. here is the working version of the class that exports to Excel.
Expand|Select|Wrap|Line Numbers
  1. public class Export2Excel
  2. {
  3.     public Export2Excel()
  4.     {
  5.  
  6.     }    
  7.     public void Prepare4Excel()
  8.     {
  9.         string connStr = ConfigurationManager.ConnectionStrings["connNorthwind"].ConnectionString;
  10.         string strSQL = "Select * from Employees";
  11.         SqlConnection conn = new SqlConnection(connStr);
  12.  
  13.         SqlCommand command = new SqlCommand(strSQL, conn);
  14.         SqlDataAdapter sda = new SqlDataAdapter(command);
  15.         DataSet ds = new DataSet();
  16.         sda.Fill(ds);
  17.         BindExcel(ds);
  18.         conn.Close();
  19.     }
  20.  
  21.     public void BindExcel(DataSet dset)
  22.     {
  23.         HttpContext.Current.Response.Clear();
  24.         HttpContext.Current.Response.Buffer = true;
  25.         HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=EmployeeList.xls");
  26.         HttpContext.Current.Response.Charset = "";
  27.         HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
  28.  
  29.         StringWriter sw = new StringWriter();
  30.         HtmlTextWriter htw = new HtmlTextWriter(sw);
  31.         DataGrid dg = new DataGrid();
  32.         dg.AllowPaging = false;
  33.         dg.DataSource = dset.Tables[0];
  34.         dg.DataBind();
  35.  
  36.         dg.RenderControl(htw);
  37.         HttpContext.Current.Response.Output.Write(sw.ToString());
  38.         HttpContext.Current.Response.Flush();
  39.         HttpContext.Current.Response.End();
  40.     }
  41.  
  42.  
  43. }
  44.  
Apr 16 '09 #3
Frinavale
9,735 Recognized Expert Moderator Expert
I'm glad that you've solved your problem :)
Thanks for sharing your solution, I'm sure it'll help others facing the same problem
Apr 16 '09 #4
YonngTyler
3 New Member
Easiest way to export to excel using a c# excel component Spire.XLS, I use it long time, name Spire.XLS, may help to you.
Aug 24 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
5040
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table. Before I iterate through the recordset I instruct the browser that the content type is Excel using the following line: (Response.ContentType = "application/vnd.ms-excel") This works fine with Excel 2003 but with older versions (I tested Excel...
5
3793
by: Maria L. | last post by:
Hi, I need to export the content of a DataGrid (in Windows application in C#), into an Excel spreadsheet. Anyone knows how to do this? Any code snippets would help! thanks a lot, Maria
2
4000
by: Siu | last post by:
Hi, I use the following code to export and import a file Excel from resp. into a Web page with the following code: //EXPORT Response.Clear(); Response.Buffer = true; Response.ContentType = "application/vnd.ms-excel"; Response.Charset = ""; this.EnableViewState = false;
6
4070
by: Elena | last post by:
I'm trying to export data to an Excel worksheet. I can export the data in the cell values perfectly. I need the code to change a header and footer for the worksheet, not for the columns. Is this possible? If so, I really need the code immediately. Thank you, Elena
13
13256
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel" HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ABC.xls") HttpContext.Current.Response.Write(strHTML) HttpContext.Current.Response.End() However when the user tries to save it the Default File Type is Web Page(*.htm; *.html)
5
31927
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
1
9780
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm having I'd be most appreciative. The database is already constructed, I'm just wanting to export the data to an excel file. In short, I'm hoping to export two Tables (or queries...not sure which to use - they both seem to have the same data) in...
1
10513
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am trying to export to Excel using a command in an Access Form. RowID strFY AccountID CostElementWBS 1 2008 1 7 2 2008 1 7 I want to...
3
7167
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this file but save it as an excel file. The data in this excel file will be imported into an Access database. The
2
6425
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the method.... it creates 6 sheets # region Namespaces using System;
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.