Use the following code to save the contents of the DataGridView in an Excel table:
Using Microsoft.Office.Core;
Using excel= Microsoft.Office.Interop.Excel;
Excel.Application excelapp = new Excel.Application ();
EXCELAPP.APPLICATION.WORKBOOKS.ADD (TRUE);
Object misvalue = System.Reflection.Missing.Value;
if (file.exists ("D:\\list.xls"))
{
File.delete ("D:\\list.xls");
ExcelApp.ActiveWorkbook.SaveAs (@ "D:\List.xls", Misvalue, Misvalue, Misvalue, Misvalue, Misvalue, Excel.XlSaveAsAccessMode.xlExclusive, Misvalue, Misvalue, Misvalue, Misvalue, Misvalue);
}
Else
{
ExcelApp.ActiveWorkbook.SaveAs (@ "D:\List.xls", Misvalue, Misvalue, Misvalue, Misvalue, Misvalue, Excel.XlSaveAsAccessMode.xlExclusive, Misvalue, Misvalue, Misvalue, Misvalue, Misvalue);
}
String MyPath = "D:\\list.xls";
ExcelApp.Workbooks.Open (MyPath);
for (int i = 0; i < Datagridview1.columncount; i++)
{
for (int j = 0; J < Datagridview1.rowcount-1; J + +)
{
Excelapp.cells[j + 2, i + 1] = Datagridview1[i, j]. Value.tostring ();
}
Excelapp.cells[1, i + 1] = Datagridview1.columns[i]. HeaderText;
}
Excelapp.visible = true;
Use C # to store DataGridView content in an Excel table