472,145 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

export datagridview to excel

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"];
int cIndex = 0;
foreach (DataColumn col in inventoryTable.Columns)
excel.Cells[1, cIndex] = col.ColumnName;
}//end foreach

int rIndex = 0;
foreach (DataRow row in inventoryTable.Rows)
cIndex = 0;
foreach (DataColumn col in inventoryTable.Columns)
excel.Cells[rIndex + 1, cIndex] = row[col.ColumnName].ToString();
}//end foreach
}//end foreach
}//end try
{ }

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

Mar 6 '07 #1
0 2216

Post your reply

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

Similar topics

1 post views Thread by Matt | last post: by
13 posts views Thread by Hemant Sipahimalani | last post: by
5 posts views Thread by Mike Wilson | last post: by
19 posts views Thread by cj2 | last post: by

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.