您的位置:首页 > 其它

Winform下datagrid数据倒入excel

2005-10-20 16:52 387 查看
Excel.Application excel = new Excel.Application ( ) ;
excel.Application.Workbooks.Add ( true ) ;
for(int n =0;n<dt.Columns.Count;n++)
{
excel.Cells[1,n+1] = dt.Columns
.ColumnName;
}
for(int i=0;i<dt.Rows.Count;i++)
{
for(int j=0;j<dt.Columns.Count;j++)
{
excel.Cells[i+2,j+1] = dt.Rows[i][j].ToString();
}
}
Excel.Range r1 = excel.get_Range(excel.Cells[1,1],excel.Cells[dt.Rows.Count+1,dt.Columns.Count]);
r1.EntireColumn.AutoFit();
r1.EntireRow.AutoFit();
r1.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
r1.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
excel.Visible = true ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: