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.