1 //引用Microsoft.Office.Interop.Excel.dll文件 2 //添加using 3 using Microsoft.Office.Interop.Excel; 4 using Excel=Microsoft.Office.Interop.Excel; 5 6 //设置程序运行语言 7 System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture; 8 System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); 9 //创建Application 10 Excel.Application xlApp = new Excel.Application(); 11 //设置是否显示警告窗体 12 excelApp.DisplayAlerts = false; 13 //设置是否显示Excel 14 excelApp.Visible = false; 15 //禁止刷新屏幕 16 excelApp.ScreenUpdating = false; 17 //根据路径path打开 18 Excel.Workbook xlsWorkBook = excelApp.Workbooks.Open(path, System.Type.Missing, System.Type.Missing, System.Type.Missing, 19 System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, 20 System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); 21 //获取Worksheet对象 22 Excel.Worksheet xlsWorkSheet = (Worksheet)xlsWorkBook.Worksheets["sales plan"]; 23 24 ***获取最后一行、一列的两种方法*** 25 //获取已用的范围数据 26 int rowsCount = xlsWorkSheet.UsedRange.Rows.Count; 27 int colsCount = xlsWorkSheet.UsedRange.Columns.Count; 28 int rowsCount = xlsWorkSheet.get_Range("A65536", "A65536").get_End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row; 29 int colsCount = xlsWorkSheet.get_Range("ZZ1", "ZZ1").get_End(Microsoft.Office.Interop.Excel.XlDirection.xlToLeft).Column; 30 31 ***将Excel数据存入二维数组*** 32 //rowsCount:最大行 colsCount:最大列 33 Microsoft.Office.Interop.Excel.Range c1 = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.Cells[1, 1]; 34 Microsoft.Office.Interop.Excel.Range c2 = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.Cells[rowsCount, colsCount]; 35 Range rng = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.get_Range(c1, c2); 36 object[,] exceldata = (object[,])rng.get_Value(Microsoft.Office.Interop.Excel.XlRangeValueDataType.xlRangeValueDefault); 37 38 //在第一列的左边插入一列 39 Excel.Range xlsColumns = (Excel.Range)xlsWorkSheet.Columns[1, System.Type.Missing]; 40 xlsColumns.Insert(XlInsertShiftDirection.xlShiftToRight, Type.Missing); 41 //xlsSheetTemplateMajor_Meisai.Cells.get_Range(xlsSheetTemplateMajor_Meisai.Cells[1, 1], xlsSheetTemplateMajor_Meisai.Cells[65535, 1]).Insert(Type.Missing, Type.Missing); 42 Excel.Range rng; 43 rng = worksheet.get_Range("A:A", "A:A"); 44 rng.Insert(Excel.XlDirection.xlToRight, Excel.XlInsertFormatOrigin.xlFormatFromLeftOrAbove); 45 Excel.Range rng = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.Columns[12, Type.Missing]; 46 rng.Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromLeftOrAbove); 47 48 49 //删除行 50 Range deleteRng = (Range)xlsWorkSheetSapExcel.Rows[2, System.Type.Missing]; 51 deleteRng.Delete(Excel.XlDeleteShiftDirection.xlShiftUp); 52 53 //删除一列数据 54 ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 11]).Select(); 55 ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 11]).EntireColumn.Delete(0); 56 ((Excel.Range)xlsSheetShareMajor_Meisai.Cells[1, 3]).EntireColumn.Delete (0); 57 58 //设置背景色为红色 59 xlsWorkSheet.get_Range("A1", "A1").Interior.ColorIndex = 3; 60 61 //设置Format属性 属性值可以通过在vba中录宏得到 62 Microsoft.Office.Interop.Excel.Range range1 = xlsWorkSheetAdd.get_Range("J1", "J65535"); 63 range1.NumberFormat = "@";//文本格式 64 range1 = xlsWorkSheetAdd.get_Range("L1", "L65535"); 65 range1.NumberFormat = "0.00";//保留两位小数 66 Excel.Range rng = xlsSheetShareMajor_Meisai.Columns["I", System.Type.Missing] as Excel.Range; 67 rng.NumberFormatLocal =@"yyyy/m/d";//设置日期格式 68 69 70 //替换 71 Range Drng = xlsWorkSheetTemplate.get_Range("D1", "D65535"); 72 Drng.Replace(" ", "", XlLookAt.xlPart, XlSearchOrder.xlByColumns, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); 73 Drng.TextToColumns(Drng, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierSingleQuote, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); 74 75 //分列处理 设置为文本 76 Range TextToColumnRng = xlsWorkSheet.get_Range("E1", "E65535"); 77 xlsWorkSheet.get_Range("E1", "E65535").TextToColumns(TextToColumnRng, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierSingleQuote, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); 78 79 //设置公式 80 rng = xlMergeFileWorkSheet.get_Range("D2", "D" + rowcount);//设置列范围 81 rng.Formula = @"=IF(RC[-3]=""H"",""Survivor"",""Donor"")";//设置公式 @的问题 82 //rng.NumberFormat = "$0.00";//设置格式 83 copyRng = xlsSheetTemplateMajor_Meisai.get_Range("N3", "N" + lastRowTemplate); 84 copyRng.Formula = "=VLOOKUP(RC[-12],AR残!C[-13]:C[-11],2,0)"; 85 86 //通过行、列的索引获取值 87 string f = Convert.ToString(xlsSheetShareMajor_Meisai.get_Range(xlsSheetShareMajor_Meisai.Cells[2, 1], xlsSheetShareMajor_Meisai.Cells[2,1]).Value2); 88 89 //筛选 90 //确定筛选范围 91 D1_rng = D1_TemSheet.Cells.get_Range(D1_TemSheet.Cells[1, 1], D1_TemSheet.Cells[1, 50]); 92 //执行筛选动作 93 rng.AutoFilter(5, "S", Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing, true); 94 rng.AutoFilter(6, "H", Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlOr, "F", true); 95 D1_rng.AutoFilter(D1_Column + 2, "#N/A", Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlFilterValues,"#N/A",false); 96 97 //复制粘贴 98 D2_rng.Copy(Type.Missing); 99 D2_TemSheet.Cells.get_Range(D2_TemSheet.Cells[2, (D2_Column + 1)], D2_TemSheet.Cells[2, (D2_Column + 1)]).PasteSpecial(Excel.XlPasteType.xlPasteValues, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false); 100 101 102 //Find查找 103 rng = mySheet.get_Range("A1", "IV10").Find(arrLabel[j], Type.Missing, 104 Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole, 105 Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, 106 Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing); 107 108 //文字占满单元格 109 range.EntireColumn.AutoFit(); 110 111 //另存 112 xlsWorkBook.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 113 114 ***关闭对象*** 115 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWorkSheet); 116 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWorkBook); 117 excelApp.Quit(); 118 Kill(excelApp);//调用方法关闭进程 119 GC.Collect(); 120 121 /// <summary> 122 /// 关闭Excel进程 123 /// </summary> 124 public class KeyMyExcelProcess 125 { 126 [DllImport("User32.dll", CharSet = CharSet.Auto)] 127 public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); 128 public static void Kill(Microsoft.Office.Interop.Excel.Application excel) 129 { 130 try 131 { 132 IntPtr t = new IntPtr(excel.Hwnd); //得到这个句柄,具体作用是得到这块内存入口 133 int k = 0; 134 GetWindowThreadProcessId(t, out k); //得到本进程唯一标志k 135 System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); //得到对进程k的引用 136 p.Kill(); //关闭进程k 137 } 138 catch (System.Exception ex) 139 { 140 throw ex; 141 } 142 } 143 } 144 145 146 //关闭打开的Excel方法 147 public void CloseExcel(Microsoft.Office.Interop.Excel.Application ExcelApplication, Microsoft.Office.Interop.Excel.Workbook ExcelWorkbook) 148 { 149 ExcelWorkbook.Close(false, Type.Missing, Type.Missing); 150 ExcelWorkbook = null; 151 ExcelApplication.Quit(); 152 GC.Collect(); 153 KeyMyExcelProcess.Kill(ExcelApplication); 154 }
原文链接:http://www.cnblogs.com/qqnnhhbb/p/3601844.html
时间: 2024-10-11 12:32:32