I try to export the table in DataGridView control on my form to MS excel with the following code.
Expand|Select|Wrap|Line Numbers
- 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
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.