Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); Microsoft.Office.Interop.Excel._Worksheet worksheet1 = null; Microsoft.Office.Interop.Excel._Worksheet worksheet2 = null; app.Visible = true; worksheet1 = workbook.Sheets["Sheet1"]; worksheet1 = workbook.ActiveSheet; for (int i = 1; i < dataGridView1.Columns.Count + 1; i++) { worksheet1.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText; } for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { for (int j = 0; j < dataGridView1.Columns.Count; j++) { if (dataGridView1.Rows[i].Cells[j].Value != null) { worksheet1.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString(); } else { worksheet1.Cells[i + 2, j + 1] = ""; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)