First of all, add Reference:
Select COM tab and select Microsoft.Office.Interop.Excel
Then write the below code:
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
ExcelApp.Workbooks.Add(true);
int columnIndex = 0;
foreach (DataColumn col in Ds.Tables[0].Columns)
{
columnIndex++;
ExcelApp.Cells[1, columnIndex] = col.ColumnName;
}
int rowIndex = 0;
foreach (DataRow row in Ds.Tables[0].Rows)
{
rowIndex++;
columnIndex = 0;
foreach (DataColumn col in Ds.Tables[0].Columns)
{
columnIndex++;
ExcelApp.Cells[rowIndex + 1, columnIndex] = row[col.ColumnName].ToString();
}
}
ExcelApp.Visible = true;
//To show excel as minimized window
ExcelApp.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;
Thanks—-
<Write any comments>