111,125
社区成员
发帖
与我相关
我的任务
分享
public void exceldaochu1(DataGridView datagridview1)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//对此实例进行验证,如果为null则表示运行此代码的机器可能未安装Excel
if (excel==null)
{
MessageBox.Show("机器未安装Microsoft Office Excel");
}
else
{
excel.Visible = true;
excel.Application.Workbooks.Add(true);
for (int i = 0; i < datagridview1.ColumnCount; i++)
{
excel.Cells[1, i + 1] = datagridview1.Columns[i].HeaderText;
}
for (int j = 0; j < datagridview1.RowCount; j++)
{
for (int n = 0; n < datagridview1.ColumnCount; n++)
{
excel.Cells[j + 2, n + 1] = datagridview1.Rows[j].Cells[n].Value.ToString();
}
}
}
}