110,533
社区成员
发帖
与我相关
我的任务
分享
public void DataToExcel(DataGridView dataGridView, ProgressBar progressBar)
{
if (dataGridView.RowCount == 0)
{
MessageBox.Show("没有可导出的数据!");
}
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
for (int i = 0; i < dataGridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = dataGridView.Columns[i].HeaderText;
}
for (int i = 0; i < dataGridView.RowCount - 1; i++)
{
for (int j = 0; j < dataGridView.ColumnCount; j++)
{
excel.Cells[i + 2, j + 1] = dataGridView.Rows[i].Cells[j].Value.ToString();
}
}
excel.Visible = true;
}