Winform下datagrid數據倒入excel

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[n].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 ;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章