View Code
1 private _Workbook _workBook = null;
2 private Worksheet _workSheet = null;
3 private Excel.Application _excelApplicatin = null;
4
5 _excelApplicatin = new Excel.Application();
6 _excelApplicatin.Visible = true;
7 _excelApplicatin.DisplayAlerts = true;
8
9 _workBook = _excelApplicatin.Workbooks.Add(XlSheetType.xlWorksheet);
10 _workSheet = (Worksheet)_workBook.ActiveSheet;
11 _workSheet.Name = "workSheetName";
12
13 //打开已存在的Excel
14 string strExcelPathName = AppDomain.CurrentDomain.BaseDirectory + "excelSheetName.xls";
15 Excel.Workbook workBook = application.Workbooks.Open(strExcelPathName, Type.Missing, Type.Missing,
16 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
17 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
18 //读取已打开的Excel
19 Excel.Worksheet workSheet1 = (Excel.Worksheet)workBook.Sheets["SheetName1"];
20 Excel.Worksheet workSheet2 = (Excel.Worksheet)workBook.Sheets["SheetName2"];
21
22 //添加一个workSheet
23 Worksheet workSheet = (Worksheet)workBook.Worksheets.Add(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
24
25 //RowHeight "1:1"表示第一行, "1:2"表示,第一行和第二行
26 ((Excel.Range)_workSheet.Rows["1:1", System.Type.Missing]).RowHeight = 100;
27
28 //ColumnWidth "A:B"表示第一列和第二列, "A:A"表示第一列
29 ((Excel.Range)_workSheet.Columns["A:B", System.Type.Missing]).ColumnWidth = 10;
30
31 // EXCEL操作(需要冻结的字段 按住ALT+W 再按F)
32 Excel.Range excelRange = _workSheet .get_Range(_workSheet .Cells[10, 5], _workSheet .Cells[10, 5]);
33 excelRange.Select();
34 excelApplication.ActiveWindow.FreezePanes = true;
35
36 //Borders.LineStyle 单元格边框线
37 Excel.Range excelRange = _workSheet.get_Range(_workSheet.Cells[2, 2], _workSheet.Cells[4, 6]);
38 //单元格边框线类型(线型,虚线型)
39 excelRange.Borders.LineStyle = 1;
40 excelRange.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous;
41 //指定单元格下边框线粗细,和色彩
42 excelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlMedium;
43
44 excelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).ColorIndex =3;
45
46 //设置字体大小
47 excelRange.Font.Size = 15;
48 //设置字体是否有下划线
49 excelRange.Font.Underline = true;
50
51 //设置字体在单元格内的对其方式
52 excelRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;
53 //设置单元格的宽度
54 excelRange.ColumnWidth = 15;
55 //设置单元格的背景色
56 excelRange.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb();
57 // 给单元格加边框
58 excelRange.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThick,
59 XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
60 //自动调整列宽
61 excelRange.EntireColumn.AutoFit();
62 // 文本水平居中方式
63 excelRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
64 //文本自动换行
65 excelRange.WrapText = true;
66 //填充颜色为淡紫色
67 excelRange.Interior.ColorIndex = 39;
68
69 //合并单元格
70 excelRange.Merge(excelRange.MergeCells);
71 _workSheet.get_Range("A15", "B15").Merge(_workSheet.get_Range("A15", "B15").MergeCells);
72
73 /// <summary>
74 /// 常用颜色定义,对就Excel中颜色名
75 /// </summary>
76 public enum ColorIndex
77 {
78 无色 = -4142, 自动 = -4105, 黑色 = 1, 褐色 = 53, 橄榄 = 52, 深绿 = 51, 深青 = 49,
79 深蓝 = 11, 靛蓝 = 55, 灰色80 = 56, 深红 = 9, 橙色 = 46, 深黄 = 12, 绿色 = 10,
80 青色 = 14, 蓝色 = 5, 蓝灰 = 47, 灰色50 = 16, 红色 = 3, 浅橙色 = 45, 酸橙色 = 43,
81 海绿 = 50, 水绿色 = 42, 浅蓝 = 41, 紫罗兰 = 13, 灰色40 = 48, 粉红 = 7,
82 金色 = 44, 黄色 = 6, 鲜绿 = 4, 青绿 = 8, 天蓝 = 33, 梅红 = 54, 灰色25 = 15,
83 玫瑰红 = 38, 茶色 = 40, 浅黄 = 36, 浅绿 = 35, 浅青绿 = 34, 淡蓝 = 37, 淡紫 = 39,
84 白色 = 2
85 }
86
87
88
89 range.NumberFormatLocal = "@"; //设置单元格格式为文本
90
91 range = (Range)worksheet.get_Range("A1", "E1"); //获取Excel多个单元格区域:本例做为Excel表头
92
93 range.Merge(0); //单元格合并动作
94
95 worksheet.Cells[1, 1] = "Excel单元格赋值"; //Excel单元格赋值
96
97 range.Font.Size = 15; //设置字体大小
98
99 range.Font.Underline=true; //设置字体是否有下划线
100
101 range.Font.Name="黑体"; 设置字体的种类
102
103 range.HorizontalAlignment=XlHAlign.xlHAlignCenter; //设置字体在单元格内的对其方式
104
105 range.ColumnWidth=15; //设置单元格的宽度
106
107 range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb(); //设置单元格的背景色
108
109 range.Borders.LineStyle=1; //设置单元格边框的粗细
110
111 range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb()); //给单元格加边框
112
113 range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //设置单元格上边框为无边框
114
115 range.EntireColumn.AutoFit(); //自动调整列宽
116
117 Range.HorizontalAlignment= xlCenter; // 文本水平居中方式
118
119 Range.VerticalAlignment= xlCenter //文本垂直居中方式
120
121 Range.WrapText=true; //文本自动换行
122
123 Range.Interior.ColorIndex=39; //填充颜色为淡紫色
124
125 Range.Font.Color=clBlue; //字体颜色
126
127 xlsApp.DisplayAlerts=false; //保存Excel的时候,不弹出是否保存的窗口直接进行保存