473,403 Members | 2,183 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.

How to save HeadColumn in Datagridview to Excel?

make datagridview export to excel, but Headcolumn don't show in excel files.

Sample

result (in Excel)
1 | mani | 213/435 | 0-892-342234-09
2 | ware | 1/67 | 053-36-49

But i need resuult (in Excel)
ID | Name | Address | Tel. >>> Headcolumn
1 | mani | 213/435 | 0-892-342234-09
2 | ware | 1/67 | 053-36-49

Code C# Windows Form

Expand|Select|Wrap|Line Numbers
  1. private void btExport_Click(object sender, EventArgs e)
  2.         {
  3.             string sql = @"SELECT id,fullname,address,tele FROM tablePeople";
  4.             SqlCommand cmd = new SqlCommand(sql, Conn);
  5.             SqlDataAdapter da = new SqlDataAdapter(cmd);
  6.             DataSet ds = new DataSet();
  7.             da.Fill(ds, "data");
  8.             DataTable dt = ds.Tables["data"];
  9.  
  10.             Excel.Application xlApp;
  11.             Excel.Workbook xlWorkBook;
  12.             Excel.Worksheet xlWorkSheet;
  13.             object misValue = System.Reflection.Missing.Value;
  14.  
  15.             xlApp = new Excel.Application();
  16.             xlWorkBook = xlApp.Workbooks.Add(misValue);
  17.             xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
  18.             int r = 0;
  19.             int c = 0;
  20.  
  21.             // DGV To EXCLE But HeadColumn don't show in excel
  22.             for (r = 0; r <= dataGridView1.RowCount - 1; r++)
  23.             {
  24.                 for (c = 0; c <= dataGridView1.ColumnCount - 1; c++)
  25.                 {
  26.                     DataGridViewCell cell = dataGridView1[c, r];
  27.                     xlWorkSheet.Cells[r + 1, c + 1] = cell.Value;
  28.                 }
  29.             }
  30.  
  31.             xlWorkBook.SaveAs(@"c:\details.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
  32.             xlWorkBook.Close(true, misValue, misValue);
  33.             xlApp.Quit();
  34.  
  35.             releaseObject(xlWorkSheet);
  36.             releaseObject(xlWorkBook);
  37.             releaseObject(xlApp);
  38.  
  39.             MessageBox.Show("Excel file created");
  40.         }
Thanks you very much for your time :)
Sep 23 '12 #1
0 1446

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

Similar topics

5
by: Carl | last post by:
Please can anyone tell me how I can create a macro to save the results of a query as an excel file? The query is called Student List and I would like to save it to "My Documents". We have had...
3
by: yaya via DotNetMonster.com | last post by:
Hi, I'm using OleDb to read an excel files and display the data in a datagrid (using dataset as data source), after modifying the data on datagird, how can I save the changes back into Excel files. ...
0
by: adam_scheich | last post by:
When creating a file in excel and saving it to a directory that is being watched by filewatcher using Save As, the filewatcher created event doesn't fire. It works fine for text files, but for...
6
by: Michael Groeger | last post by:
Hi, I have an aspx page which generates an excel document and transfers it to the browser as attachment. Normally, once the document is transferred the open save dialog prompts to open or save...
4
by: Juliano.net | last post by:
How can I read an Excel 2003 XML Spreadsheet and save it as an Excel 97 file?
0
by: rajeshkothuru | last post by:
Hi I am new to perl programming, i stcked at one line. I have written code such that new excel sheet will save accordingly even though if already existed Excel sheet persists. The problem is if...
25
wassup
by: wassup | last post by:
Hi all, This my 1st time to get help at forum. So my problem is I have a form with some textbox inside and I want save the data that key in inside textbox to the Excel format that I create and the...
2
by: veer | last post by:
hi i want to save the value which i write in the datagridview cells how can i do actually i want to insert the value into the ms acees table by writting the values in gridview cells and then...
3
by: qfchen | last post by:
Hi In order to save data into excel file in my application, I need to define Dim excelApp As New Excel.Application but I got error message: "Type Excel.Application" is not defined. What component...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
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...
0
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...
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.