// creating Excel Application
Microsoft.Office.Interop.Excel.Application ExcelFile = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel.Workbook workbook = ExcelFile.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
// store its reference to worksheet
worksheet = workbook.ActiveSheet;
// Name Excel table
worksheet.Name = "Excel Table form Xml Reader";
// storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
// see the excel sheet behind the program
ExcelFile.Columns.AutoFit();
ExcelFile.Visible = true;
workbook.Close();
ExcelFile.Quit();
Блин помню что я нулл
запихал во все переменные и Интером освобождал, прожорливая херня.