473,403 Members | 2,071 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,403 software developers and data experts.

Exporting from sql server 2008 r2 to excel with header

I'am trying to export data from my database to excel . The problem is my code is running but once i check my excel file there's no header included but just only the data in every particular header.
here's my code .. please suggest or help me to solve my problem . thank you.


Expand|Select|Wrap|Line Numbers
  1. //EXPORT
  2.            SqlConnection cnn;
  3.             string connectionstring = null;
  4.             string sql = null;
  5.             string data = null;
  6.             int i = 0;
  7.             int j = 0;
  8.             string title=store.Text;
  9.  
  10.             Excel.Application xlApp;
  11.             Excel.Workbook xlWorkBook;
  12.             Excel.Worksheet xlWorkSheet;
  13.             object misValue = System.Reflection.Missing.Value;
  14.  
  15.  
  16.             xlApp = new Microsoft.Office.Interop.Excel.Application();
  17.             xlWorkBook = xlApp.Workbooks.Add(misValue);
  18.             xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
  19.  
  20.             connectionstring = "Data Source=user-pc\\sqlexpress;Initial Catalog=StoreBarcode;Integrated Security=True";
  21.             cnn = new SqlConnection(connectionstring);
  22.  
  23.             try
  24.             {
  25.                 cnn.Open();
  26.                 sql = "SELECT * FROM Barcode." + title + "";
  27.                 SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
  28.                 DataSet ds = new DataSet();
  29.                 dscmd.Fill(ds);
  30.  
  31.                 for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
  32.                 {
  33.                     for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
  34.                     {
  35.                         data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
  36.                         xlWorkSheet.Cells[i + 1, j + 1] = data;
  37.                     }
  38.                 }
  39.             }
  40.             catch
  41.             {
  42.                 MessageBox.Show("Choose from Store to Export!");
  43.             }
  44.  
  45.             try
  46.             {
  47.                 xlWorkBook.SaveAs(""+title+".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
  48.                 xlWorkBook.Close(true, misValue, misValue);
  49.  
  50.                 xlApp.Quit();
  51.  
  52.  
  53.                 releaseObject(xlWorkSheet);
  54.                 releaseObject(xlWorkBook);
  55.                 releaseObject(xlApp);
  56.  
  57.                 MessageBox.Show("Excel file created , you can find the file @ C:\\Users\\User\\Documents");
  58.             }
  59.             catch {
  60.                 MessageBox.Show("Exporting has been Cancelled!");
  61.             }
Dec 17 '13 #1
0 1544

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

Similar topics

0
by: Hideyuki | last post by:
Hi, I am using asp.net, and I am having a problem exporting datagrid to excel. One column contains numbers such as 123456875468. When this goes to excel, it is displayed as 1.23456E + 11 which I...
4
by: John hoffman | last post by:
Hello, I am trying to set the MS Excel header and footer font using automation from C#. I can set the text using: oPageSetup.LeftHeader = "Header Text"; but how do you set the Font...
2
by: Mike P | last post by:
How do you go about exporting data in excel to a C# dataset? Any help would be really appreciated. Cheers, Mike
1
by: =?Utf-8?B?S2luZXRpYyBKdW1wIEFwcGxpZmUgZm9yIC5ORVQg | last post by:
Do want to attend the Microsoft SQL Server 2008 and Visual Studio 2008 sessions at your home PC?. Yes now it is possible. Microsoft has scheduled a Webcasts for you on SQL Server 2008 and Visual...
1
by: Chris O'C | last post by:
See: http://blogs.zdnet.com/microsoft/?p=1519 "Microsoft officials announced at TechEd South Africa on August 6 that SQL Server 2008 has been released to manufacturing. "Microsoft officials...
1
by: =?Utf-8?B?QW50amU=?= | last post by:
I need product keys for Terminal Server Windows Server 2008 access (CALs) under MSDN - TSLM Service said they cannot provide product keys for the access.
6
by: AAaron123 | last post by:
I need to move a database from sql server 2008 to sql express 2008. Will backing up the sql server 2008 database to a file and then using that file to restore to sql express 2008 work? I don't...
8
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a web site that automates excel. This site runs in server 2003. iis 6.0 office 2003 installed I moved this app to server 2008 iis 7.0 office 2003 installed Now when I try to automate...
1
by: BobLewiston | last post by:
I installed SQL Server 2008 Express, basic edition (SQLEXPR32_x86_ENU_Bootstrapper.exe, version 9.0.30729.1) without any problem. Then I attempted to install AdventureWorks Sample Databases for...
3
AR Ratheesh
by: AR Ratheesh | last post by:
Hi , How can i create SQL Server Express 2008 R2(free version) from SQL Server 2008 R2 Management Studio. I have SQL Server 2008 R2 management Studio. for my MS Access database i need to deploy...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.