导出excel小结(C#,.NET,Wpf)

range.NumberFormatLocal = "@";     //设置单元格格式为文本     
 1  range.NumberFormatLocal = "@";     //设置单元格格式为文本     
 2     
 3 range = (Range)worksheet.get_Range("A1", "E1");     //获取Excel多个单元格区域:本例做为Excel表头     
 4     
 5 range.Merge(0);     //单元格合并动作     
 6     
 7 worksheet.Cells[1, 1] = "Excel单元格赋值";     //Excel单元格赋值     
 8     
 9 range.Font.Size = 15;     //设置字体大小     
10     
11 range.Font.Underline=true;     //设置字体是否有下划线     
12     
13 range.Font.Name="黑体";       设置字体的种类     
14     
15 range.HorizontalAlignment=XlHAlign.xlHAlignCenter;     //设置字体在单元格内的对其方式     
16     
17 range.ColumnWidth=15;     //设置单元格的宽度     
18     
19 range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb();     //设置单元格的背景色     
20     
21 range.Borders.LineStyle=1;     //设置单元格边框的粗细     
22     
23 range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb());     //给单元格加边框     
24     
25 range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //设置单元格上边框为无边框     
26     
27    range.EntireColumn.AutoFit();     //自动调整列宽     
28     
29 Range.HorizontalAlignment= xlCenter;     // 文本水平居中方式     
30     
31 Range.VerticalAlignment= xlCenter     //文本垂直居中方式     
32     
33 Range.WrapText=true;     //文本自动换行     
34     
35 Range.Interior.ColorIndex=39;     //填充颜色为淡紫色     
36     
37 Range.Font.Color=clBlue;     //字体颜色     
38     
39 xlsApp.DisplayAlerts=false;     //保存Excel的时候,不弹出是否保存的窗口直接进行保存  
View Code

Excel 行高,列宽,合并单元格,单元格边框线,冻结

 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);  
View Code

在Excel中设置背景字体及其它颜色的时候用的都是索引

如: xSt.get_Range(excel.Cells[2, i], excel.Cells[2, i]).Font.ColorIndex = 5

特此提供一个对应的Excel颜色索引图例

 

 

interior font HTML bgcolor= Red< Green Blue Color
Black [Color 1] #000000 #000000 0 0 0 [Black]
White [Color 2] #FFFFFF #FFFFFF 255 255 255 [White]
Red [Color 3] #FF0000 #FF0000 255 0 0 [Red]
Green [Color 4] #00FF00 #00FF00 0 255 0 [Green]
Blue [Color 5] #0000FF #0000FF 0 0 255 [Blue]
Yellow [Color 6] #FFFF00 #FFFF00 255 255 0 [Yellow]
Magenta [Color 7] #FF00FF #FF00FF 255 0 255 [Magenta]
Cyan [Color 8] #00FFFF #00FFFF 0 255 255 [Cyan]
[Color 9] [Color 9] #800000 #800000 128 0 0 [Color 9]
[Color 10] [Color 10] #008000 #008000 0 128 0 [Color 10]
[Color 11] [Color 11] #000080 #000080 0 0 128 [Color 11]
[Color 12] [Color 12] #808000 #808000 128 128 0 [Color 12]
[Color 13] [Color 13] #800080 #800080 128 0 128 [Color 13]
[Color 14] [Color 14] #008080 #008080 0 128 128 [Color 14]
[Color 15] [Color 15] #C0C0C0 #C0C0C0 192 192 192 [Color 15]
[Color 16] [Color 16] #808080 #808080 128 128 128 [Color 16]
[Color 17] [Color 17] #9999FF #9999FF 153 153 255 [Color 17]
[Color 18] [Color 18] #993366 #993366 153 51 102 [Color 18]
[Color 19] [Color 19] #FFFFCC #FFFFCC 255 255 204 [Color 19]
[Color 20] [Color 20] #CCFFFF #CCFFFF 204 255 255 [Color 20]
[Color 21] [Color 21] #660066 #660066 102 0 102 [Color 21]
[Color 22] [Color 22] #FF8080 #FF8080 255 128 128 [Color 22]
[Color 23] [Color 23] #0066CC #0066CC 0 102 204 [Color 23]
[Color 24] [Color 24] #CCCCFF #CCCCFF 204 204 255 [Color 24]
[Color 25] [Color 25] #000080 #000080 0 0 128 [Color 25]
[Color 26] [Color 26] #FF00FF #FF00FF 255 0 255 [Color 26]
[Color 27] [Color 27] #FFFF00 #FFFF00 255 255 0 [Color 27]
[Color 28] [Color 28] #00FFFF #00FFFF 0 255 255 [Color 28]
[Color 29] [Color 29] #800080 #800080 128 0 128 [Color 29]
[Color 30] [Color 30] #800000 #800000 128 0 0 [Color 30]
[Color 31] [Color 31] #008080 #008080 0 128 128 [Color 31]
[Color 32] [Color 32] #0000FF #0000FF 0 0 255 [Color 32]
[Color 33] [Color 33] #00CCFF #00CCFF 0 204 255 [Color 33]
[Color 34] [Color 34] #CCFFFF #CCFFFF 204 255 255 [Color 34]
[Color 35] [Color 35] #CCFFCC #CCFFCC 204 255 204 [Color 35]
[Color 36] [Color 36] #FFFF99 #FFFF99 255 255 153 [Color 36]
[Color 37] [Color 37] #99CCFF #99CCFF 153 204 255 [Color 37]
[Color 38] [Color 38] #FF99CC #FF99CC 255 153 204 [Color 38]
[Color 39] [Color 39] #CC99FF #CC99FF 204 153 255 [Color 39]
[Color 40] [Color 40] #FFCC99 #FFCC99 255 204 153 [Color 40]
[Color 41] [Color 41] #3366FF #3366FF 51 102 255 [Color 41]
[Color 42] [Color 42] #33CCCC #33CCCC 51 204 204 [Color 42]
[Color 43] [Color 43] #99CC00 #99CC00 153 204 0 [Color 43]
[Color 44] [Color 44] #FFCC00 #FFCC00 255 204 0 [Color 44]
[Color 45] [Color 45] #FF9900 #FF9900 255 153 0 [Color 45]
[Color 46] [Color 46] #FF6600 #FF6600 255 102 0 [Color 46]
[Color 47] [Color 47] #666699 #666699 102 102 153 [Color 47]
[Color 48] [Color 48] #969696 #969696 150 150 150 [Color 48]
[Color 49] [Color 49] #003366 #003366 0 51 102 [Color 49]
[Color 50] [Color 50] #339966 #339966 51 153 102 [Color 50]
[Color 51] [Color 51] #003300 #003300 0 51 0 [Color 51]
[Color 52] [Color 52] #333300 #333300 51 51 0 [Color 52]
[Color 53] [Color 53] #993300 #993300 153 51 0 [Color 53]
[Color 54] [Color 54] #993366 #993366 153 51 102 [Color 54]
[Color 55] [Color 55] #333399 #333399 51 51 153 [Color 55]
[Color 56] [Color 56] #333333 #333333 51 51 51 [Color 56]

在Excel中设置背景字体及其它颜色的时候用的都是索引

如: xSt.get_Range(excel.Cells[2, i], excel.Cells[2, i]).Font.ColorIndex = 5

特此提供一个对应的Excel颜色索引图例

还有就是可能是大家最关心的Excel.exe的问题了,分享我们项目中的几个释放进程的方法,希望对大家有用,都是绝对好使的

 

 1 #region 释放excel.exe  
 2   
 3         [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]  
 4         static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);  
 5   
 6         public void KillSpecialExcel()  
 7         {  
 8             try  
 9             {  
10                 if (excel != null)  
11                 {  
12                     int lpdwProcessId;  
13                     GetWindowThreadProcessId(new IntPtr(excel.Hwnd), out lpdwProcessId);  
14   
15                     System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();  
16                 }  
17             }  
18             catch (Exception ex)  
19             {  
20                 Console.WriteLine("Delete Excel Process Error:" + ex.Message);  
21             }  
22         }  
23         #endregion  
View Code
 1 #region KillAllExcel   
 2 public bool KillAllExcel()   
 3 {   
 4     try   
 5     {   
 6         if (m_objExcel != null)  
 7         {   
 8             m_objExcel.Quit();   
 9             System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);   
10             //释放COM组件,其实就是将其引用计数减1   
11             //System.Diagnostics.Process theProc;   
12             foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))   
13             {   
14                 //先关闭图形窗口。如果关闭失败...有的时候在状态里看不到图形窗口的excel了,   
15                 //但是在进程里仍然有EXCEL.EXE的进程存在,那么就需要杀掉它   
16                 if (theProc.CloseMainWindow() == false)   
17                 {   
18                     theProc.Kill();   
19                 }   
20             }   
21             m_objExcel = null;   
22             return true;   
23         }   
24     }   
25     catch   
26     {   
27         return false;   
28     }   
29     return true;   
30 }   
31 #endregion   
View Code

 

posted @ 2013-11-08 14:32  吻上明天  阅读(564)  评论(0编辑  收藏  举报