dongxiaoling1029

导航

ASP.NET 操作 Excel 【转】

1 using System.Reflection;
2
3 using Excel = Microsoft.Office.Interop.Excel;
4
5
6
7 3、简单例子
8 public void ToExcel(string strTitle)
9
10 {
11
12 int rowCount = nMax - nMin + 1;//总行数
13 const int columnCount = 4;//总列数
14
15
16 //创建Excel对象
17 Excel.Application excelApp = new Excel.ApplicationClass();
18
19
20
21 //新建工作簿
22 Excel.Workbook workBook = excelApp.Workbooks.Add(true);
23
24
25
26 //新建工作表
27 Excel.Worksheet worksheet = workBook.ActiveSheet as Excel.Worksheet;
28
29
30
31 //设置标题
32 Excel.Range titleRange = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, columnCount]);//选取单元格
33 titleRange.Merge(true);//合并单元格
34 titleRange.Value2 = strTitle; //设置单元格内文本
35 titleRange.Font.Name = "宋体";//设置字体
36 titleRange.Font.Size = 18;//字体大小
37 titleRange.Font.Bold = true;//加粗显示
38 titleRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平居中
39 titleRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//垂直居中
40 titleRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//设置边框
41 titleRange.Borders.Weight = Excel.XlBorderWeight.xlMedium;//边框常规粗细
42
43
44 //设置表头
45 string[] strHead = new string[columnCount] { "序号", "分组", "频数", "相对频数" };
46
47 int[] columnWidth = new int[4] { 8, 16, 8, 10 };
48
49 for (int i = 0; i < columnCount; i++)
50
51 {
52
53 Excel.Range headRange = worksheet.Cells[2, i + 1] as Excel.Range;//获取表头单元格
54 headRange.Value2 = strHead[i];//设置单元格文本
55 headRange.Font.Name = "宋体";//设置字体
56 headRange.Font.Size = 12;//字体大小
57 headRange.Font.Bold = true;//加粗显示
58 headRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平居中
59 headRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//垂直居中
60 headRange.ColumnWidth = columnWidth[i];//设置列宽
61 headRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//设置边框
62 headRange.Borders.Weight = Excel.XlBorderWeight.xlMedium;//边框常规粗细
63 }
64
65
66
67 //设置每列格式
68 for (int i = 0; i < columnCount; i++)
69
70 {
71
72 Excel.Range contentRange = worksheet.get_Range(worksheet.Cells[3, i+1], worksheet.Cells[rowCount-1 + 3, i+1]);
73
74 contentRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平居中
75 contentRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//垂直居中
76 contentRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//设置边框
77 contentRange.Borders.Weight = Excel.XlBorderWeight.xlMedium;//边框常规粗细
78 contentRange.WrapText = true;//自动换行
79 contentRange.NumberFormatLocal = "@";//文本格式
80 }
81
82
83
84 //填充数据
85 for (int i = nMin; i <= nMax; i++)
86
87 {
88
89 int k = i - nMin;
90
91 excelApp.Cells[k + 3, 1] = string.Format("{0}", k + 1);
92
93 excelApp.Cells[k + 3, 2] = string.Format("{0}-{1}", i - 0.5, i + 0.5);
94
95 excelApp.Cells[k + 3, 3] = string.Format("{0}", subordinativeValues[k].count);
96
97 excelApp.Cells[k + 3, 4] = string.Format("{0:0.0000}", subordinativeValues[k].frequency);
98
99 }
100
101
102
103 //设置Excel可见
104 excelApp.Visible = true;
105
106 }

posted on 2011-04-19 22:46  dongxiaoling1029  阅读(270)  评论(0编辑  收藏  举报