473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Export to Excel

145 100+
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 3781
dorandoran
145 100+
Anyone? cause I am working on a deadline and working on this for 48 hours.
Apr 16 '09 #2
dorandoran
145 100+
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 Expert Mod 8TB
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
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
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....
5
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
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 =...
6
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...
13
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"...
5
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
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...
1
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...
3
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...
2
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.