473,507 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

export 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.

private void fnExtractToExcel()
{
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true) ;
DataTable inventoryTable = this.inventoryDataSet.Tables["Inventory"];
try
{
int cIndex = 0;
foreach (DataColumn col in inventoryTable.Columns)
{
cIndex++;
excel.Cells[1, cIndex] = col.ColumnName;
}//end foreach

int rIndex = 0;
foreach (DataRow row in inventoryTable.Rows)
{
rIndex++;
cIndex = 0;
foreach (DataColumn col in inventoryTable.Columns)
{
cIndex++;
excel.Cells[rIndex + 1, cIndex] = row[col.ColumnName].ToString();
}//end foreach
}//end foreach
excel.Save("Inventory.xls");
excel.Workbooks.Close();
}//end try
catch
{ }
}//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 6 '07 #1
0 2309

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

Similar topics

1
5007
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....
13
13182
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"...
2
4968
by: Mike Wilson | last post by:
Dear all, I'm sure someone has already done this, so as not to wish to re-invent the wheel - and Google not turning up anything I can use, does anyone have a suitable function into which I can...
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...
7
14577
by: RafaulWolf | last post by:
Hi, anyone know how can i export the information in the datagridview to an Excel file? i understand that the crystal report tool has such a build in function for importing to pdf or execl, then...
0
1448
by: nitin005 | last post by:
I have to export the data present in the datagridview to Microsoft Excel. It is a window application using c#. Does anyone know how to do it. Please help. Thanks
19
11207
by: cj2 | last post by:
#1 Is there a quick way to export a datatable to an excel file? Or delimited file? #2 Where is the appropriate Microsoft monitored group to ask about writing reports in SQL Reporting services...
2
2843
by: ISATownsME | last post by:
Hello, I have a program that loads a datagridview from excel to visual studio. Can anyone tell me how to export the cell values of the datagridview to my access database?
2
2344
by: Narz1986 | last post by:
hi...i m trying to export data from the datagrid to excel...here is my code HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition",...
0
7313
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
7372
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...
1
7029
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
7481
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
5619
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
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1537
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
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.