第1个回答 推荐于2016-05-25
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
后面可以参考
//生成字段名称
for (int i = 0; i < DGVCustomer.ColumnCount; i++)
{
excel.Cells[1, i + 1] = DGVCustomer.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < DGVCustomer.RowCount - 1; i++)
{
for (int j = 0; j < DGVCustomer.ColumnCount; j++)
{
if (DGVCustomer[j, i].Value == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + DGVCustomer[i, j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = DGVCustomer[j, i].Value.ToString();
}
}
}
excel.Visible = true;本回答被提问者采纳