NET导出Excel(二)

继续:需求说明书上要求导出的数据表的样式如下

程序已完成,但由于某些原因不能公开源代码,所以自己写了一个小示例,因为时间短没有做过多的抽象

1 Microsoft.Office.Interop.Excel.Application excel = null;
2 _Workbook xBk = null;
3 _Worksheet xSt = null;
4 public Window1()
5 {
6 InitializeComponent();
7 }
8
9 void ExportExcel()
10 {
11 try
12 {
13 const int columnCount = 15;
14
15 var saveExcelAlert = new SaveFileDialog
16 {
17 Filter = "Microsoft Office Excel 工作簿(*.xls)|*.xls"
18 };
19
20 if (saveExcelAlert.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
21
22 GC.Collect();
23 excel = new ApplicationClass();
24 xBk = excel.Workbooks.Add(true); //添加一张工作表
25   xSt = (_Worksheet)xBk.ActiveSheet; //激活当前的工作表
26
27 //标头开始
28   xSt.Name = "地市级输出结果" + DateTime.Now.ToString("yyyy-MM-dd");
29 excel.Cells[1, 1] = "地市级输出结果" + DateTime.Now.ToString("yyyy-MM-dd");
30 xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Bold = true;
31 xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Size = 16;
32 xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).RowHeight = 50; //设置行高
33 xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.ColorIndex = 5; //设置字体颜色
34 xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Interior.ColorIndex = 7;//设置背景颜色
35 xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Select(); //先选中再操作
36 xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection; //设置整个报表的标题为跨列居中
37
38 SetBorderColor(excel, xSt, (Range)excel.Cells[1, 1], (Range)excel.Cells[1, columnCount]);
39
40 var rangeHeader = xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, columnCount]);//取得合并的区域 注:xcel.Cells[1, columnCount]里面的columnCount也就是列的数量是需要通过程序算出来的,我这里只是写了个静态的做的演示,你也别想用excel.Cells.Count来得到当前数据的列数,这个会把整个工作簿的所有列全都给你统计出来
41 rangeHeader.MergeCells = true; //进行合并操作
42
43 //第二行复标头开始
44 for (var i = 1; i <= columnCount; i++)
45 {
46 if (i % 2 != 0)
47 {
48 if (i != 1)
49 {
50 excel.Cells[3, i] = "当前值"; //得到偶数的单元格
51 SetBorderColor(excel, xSt, (Range)excel.Cells[3, i], (Range)excel.Cells[3, i]);
52 continue;
53 }
54 }
55 if (i != 1)
56 {
57 excel.Cells[2, i] = i.ToString();
58 xSt.get_Range(excel.Cells[2, i], excel.Cells[2, i]).Font.ColorIndex = 5; //设置字体颜色
59 xSt.get_Range(excel.Cells[2, i], excel.Cells[2, i]).RowHeight = 20; //行高
60 xSt.get_Range(excel.Cells[2, i], excel.Cells[2, i]).Interior.ColorIndex = 8; //设置背景颜色
61 var rangeComplexHeader = xSt.get_Range(excel.Cells[2, i], excel.Cells[2, (i + 1)]); //得到需要合并的单元格
62 rangeComplexHeader.MergeCells = true; //合并单元格
63
64 xSt.get_Range(excel.Cells[2, i], excel.Cells[2, i]).HorizontalAlignment =
65 XlHAlign.xlHAlignCenterAcrossSelection; //设置整个报表的标题为跨列居中
66
67 SetBorderColor(excel, xSt, (Range)excel.Cells[2, i], (Range)excel.Cells[2, (i + 1)]);
68
69 //设置第三行的背景色
70 excel.Cells[3, i] = "上周平均";
71 var rangeCellBackground = xSt.get_Range(excel.Cells[3, i], excel.Cells[3, i]); //得到要着色的单元格
72 rangeCellBackground.Cells.Interior.ColorIndex = 35;
73 SetBorderColor(excel, xSt, (Range)excel.Cells[3, i], (Range)excel.Cells[3, i]);
74 }
75 else if (i == 1) //跨二行
76 {
77 excel.Cells[2, i] = "城市名称";
78 var rangeRowSpread = xSt.get_Range(excel.Cells[2, i], excel.Cells[3, i]);
79 rangeRowSpread.Cells.Interior.ColorIndex = 38;
80 rangeRowSpread.MergeCells = true;
81 SetBorderColor(excel, xSt, (Range)excel.Cells[2, i], (Range)excel.Cells[3, i]);
82 }
83 }
84
85
86 //excel.Cells[2, 2] = "信令信道拥塞率";
87 //xSt.get_Range(excel.Cells[2, 2], excel.Cells[2, 2]).Font.ColorIndex = 5;//设置字体颜色
88 //xSt.get_Range(excel.Cells[2, 2], excel.Cells[2, 2]).Interior.ColorIndex = 8;//设置背景颜色
89 //var rangeComplexHeader = xSt.get_Range(excel.Cells[2, 2], excel.Cells[2, 3]);
90 //xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection; //设置整个报表的标题为跨列居中
91 //rangeComplexHeader.MergeCells = true;
92
93 //for (int i = 2; i <= 6; i++) //第一行需要合并,从第二行开始
94 //{
95 // for (int j = 1; j <= 5; j++)
96 // {
97 // excel.Cells[i, j] = (i + j).ToString(); //注意:Excel.Cells 的下标不能为0. 否则就会出现异常:HRESULT:0x800A03EC
98 // xSt.get_Range(excel.Cells[i, j], excel.Cells[i, j]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
99 // }
100 //}
101
102
103
104 excel.Visible = true; //让Excel文件可见
105 xBk.SaveCopyAs(saveExcelAlert.FileName);
106 xBk.Close(false, Type.Missing, Type.Missing);
107 excel.Quit();
108
109
110 }
111 catch (Exception ex)
112 {
113 System.Windows.MessageBox.Show(ex.Message);
114 }
115 finally
116 {
117 System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk); //释放工作表
118 System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
119 System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
120 GC.Collect(0); //强制结束进程
121
122 //释放COM组件,其实就是将其引用计数减1
123 foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
124 {
125 //先关闭图形窗口。如果关闭失败...有的时候在状态里看不到图形窗口的excel了,
126 //但是在进程里仍然有EXCEL.EXE的进程存在,那么就需要杀掉它
127 if (!theProc.CloseMainWindow())
128 {
129 theProc.Kill();
130 }
131 }
132 }
133 }
134
135 private void btnExportExcel_Click(object sender, RoutedEventArgs e)
136 {
137 ExportExcel();
138 }
139
140 /// <summary>
141 /// 设置边框的颜色
142 /// </summary>
143 /// <param name="excel"></param>
144 /// <param name="xSt"></param>
145 /// <param name="sRange"></param>
146 /// <param name="tRange"></param>
147 void SetBorderColor(Microsoft.Office.Interop.Excel.Application excel, _Worksheet xSt, Range sRange, Range tRange)
148 {
149 xSt.get_Range(sRange, tRange).EntireColumn.AutoFit();//自动调整列宽
150 xSt.get_Range(sRange, tRange).Borders.LineStyle = 0.5; //设置边框的宽度
151 xSt.get_Range(sRange, tRange).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThin;
152 xSt.get_Range(sRange, tRange).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThin;
153 xSt.get_Range(sRange, tRange).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThin;
154 xSt.get_Range(sRange, tRange).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThin;
155 }
156

最终导出的效果如下图所示,并把进程中的Excel.exe安全的排除了

一个完整的人生应该是宽恕、容忍、等待和爱!
原文地址:https://www.cnblogs.com/homezzm/p/1615870.html