本文转自:http://www.cnblogs.com/springyangwc/archive/2011/02/14/1954377.html
首先引用Microsoft.Office.Interop.Excel;
01 |
private void ExportExcel(DataTable dt) |
03 |
if (dt == null ) return ; |
04 |
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); |
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; |
19 |
for ( int i = 0; i < dt.Columns.Count; i++) |
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 ; |
26 |
for ( int r = 0; r < dt.Rows.Count; r++) |
28 |
for ( int i = 0; i < dt.Columns.Count; i++) |
30 |
worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i]; |
33 |
percent = (( float )(100 * rowRead)) / totalCount; |