http://www.cnblogs.com/springyangwc/archive/2011/02/14/1954377.html
首先引用Microsoft.Office.Interop.Excel;
view source print ? 01 private void ExportExcel(DataTable dt) 02 { 03 if (dt == null) return; 04 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 05 06 if (xlApp == null) 07 { 08 return; 09 } 10 System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture; 11 System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); 12 Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; 13 Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); 14 Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; 15 Microsoft.Office.Interop.Excel.Range range; 16 long totalCount = dt.Rows.Count; 17 long rowRead = 0; 18 float percent = 0; 19 for (int i = 0; i < dt.Columns.Count; i++) 20 { 21 worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; 22 range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]; 23 range.Interior.ColorIndex = 15; 24 range.Font.Bold = true; 25 } 26 for (int r = 0; r < dt.Rows.Count; r++) 27 { 28 for (int i = 0; i < dt.Columns.Count; i++) 29 { 30 worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i]; 31 } 32 rowRead++; 33 percent = ((float)(100 * rowRead)) / totalCount; 34 } 35 xlApp.Visible = true; 36 } 37<BR><BR><BR>//这样就行了,很实用的代码.