DataGridView导出为EXCEL的问题
private void button6_Click(object sender, EventArgs e)//导出为EXCEL
{
Excel.ApplicationClass MyExcel = new Excel.ApplicationClass();
MyExcel.Visible = true;
if (MyExcel == null)
{
MessageBox.Show( "EXCEL无法启动! ", "错误 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int rowcount = 0;
int columncount = 0;
MyExcel.Application.Workbooks.Add(true);
columncount = this.dataGridView1.ColumnCount;
rowcount = this.dataGridView1.RowCount;
for (int m = 1; m < columncount; m++)
{
MyExcel.Cells[1, m] = this.dataGridView1.Columns[m].HeaderText;//去除dataGridView1的编号列
}
for (int i = 0; i < rowcount; i++)
{
for (int j = 1; j < columncount; j++)
{
MyExcel.Cells[i + 2, j] = this.dataGridView1[j, i].Value.ToString();
}
}
}
每次我点击按钮,总会开启excel的新实例,怎么如何让它自