Using Microsoft.Office.Interop.Excel;
//需要在引用里加入dll
private void toolStripMenuItem9_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = false;
for (int i = 0; i < this.dataGridView1.ColumnCount; i++)
{
excel.Cells[1, i + 1] = this.dataGridView1.Columns[i].HeaderText;
}
for (int i = 0; i < this.dataGridView1.RowCount - 1; i++)
{
for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
{
excel.Cells[i + 2, j + 1] = this.dataGridView1[j, i].Value.ToString();
}
System.Windows.Forms.Application.DoEvents();
}
excel.Visible = true;
}