473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datagridview to excel

18 New Member
Hi All,

I try to export the table in DataGridView control on my form to MS excel with the following code.

Expand|Select|Wrap|Line Numbers
  1. private void fnExtractToExcel()
  2. {
  3. Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); 
  4. excel.Application.Workbooks.Add(true) ;
  5. DataTable inventoryTable = this.inventoryDataSet.Tables["Inventory"];
  6. try
  7. {
  8. int cIndex = 0;
  9. foreach (DataColumn col in inventoryTable.Columns)
  10. {
  11. cIndex++;
  12. excel.Cells[1, cIndex] = col.ColumnName;
  13. }//end foreach
  14.  
  15. int rIndex = 0;
  16. foreach (DataRow row in inventoryTable.Rows)
  17. {
  18. rIndex++;
  19. cIndex = 0;
  20. foreach (DataColumn col in inventoryTable.Columns)
  21. {
  22. cIndex++;
  23. excel.Cells[rIndex + 1, cIndex] = row[col.ColumnName].ToString();
  24. }//end foreach
  25. }//end foreach
  26. excel.Save("Inventory.xls");
  27. excel.Workbooks.Close();
  28. }//end try
  29. catch
  30. { }
  31. }//fnExtractToExcel
But each time when I try to extract via a menuItem, it creates Inventory.xls for me and prompts if I want to save the changes made to Sheet1.xls. I do not understand from where did sheet1.xls has creeped into my program.
My aim is to allow the user to save the file at different locations (or at same location with a different name) each time when he wants to, without having to prompt him about previous file.Do I need to make use of a save dialogbox?

Thank You All In advance

ArtTeam.
Mar 7 '07 #1
2 1557
kenobewan
4,871 Recognized Expert Specialist
What happens if you cancel the prompt? If you get an exception, then you have found a bug that allows the sheet to be saved in two places - hence the save as sheet1.xls. Suggest using another method.
Mar 8 '07 #2
artteam
18 New Member
What happens if you cancel the prompt? If you get an exception, then you have found a bug that allows the sheet to be saved in two places - hence the save as sheet1.xls. Suggest using another method.
I appreciate for taking time. Could you be more specific. I did not understand when you say cancelling the prompt. I myself not aware of from where the prompt is generated.

Thank you in advance....

Sudhamsh Mahankali
Mar 8 '07 #3

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

Similar topics

2
7408
by: Rich | last post by:
Hello, Some database applicatins have a tooltip feature where when you are dragging the scrollbar of the table view a tooltip appears next to the mouse cursor displaying the approximate record...
0
2309
by: artteam | last post by:
Hi All, I try to export the table in DataGridView control on my form to MS excel with the following code. private void fnExtractToExcel() { ...
0
1988
by: MathewJose | last post by:
Hi, I have a DatagridView in windows form.It has got a column which has some names that are populated from master table in database.Now i need to copy a set of data against these names. I...
5
34040
by: Mike Wilson | last post by:
Hello all, I'd like to export a DataGridView to Excel. I am using .NET 2.0 and VB.NET. But I don't know if the user has Excel on their machines, so can't use COM Excel object. Any ideas...
1
10518
by: szuccaro | last post by:
I am reading an excel file into a datagridview and trying to perform some function based on the column header that is clicked. I currently can produce an event to occur (msgbox) but, I need to get...
1
3260
by: pompair | last post by:
Hi, I've been implementing a program that populates winforms DataGridView from an excel sheet (.xlsx-file). The app works ok. Now I'm refactoring it and I've got a feeling that databinding...
0
1776
by: nudrat | last post by:
Hi All, I have an excel sheet. I am able to read each cell values of excel sheet. Now i have a datagridview control. so how do i populate these cell values into datagridview control without using...
2
3336
by: Randy | last post by:
I've found several examples of exporting to Excel (XML) from a DataGridView. I need to export to an actual binary XLS file from a DataGridView. I've found what I think are a couple of examples but...
1
3353
by: TG | last post by:
Hi! I have an application in which I have some checkboxes and depending which ones are checked those columns will show in the datagridview from sql server or no. After that I have 2 buttons:...
0
7074
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
7322
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
7451
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
5572
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
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
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 ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
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...

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.