private void button1_Click(object sender, EventArgs e) { var arr1 = GetObjArr("C:\\Users\\Administrator\\Desktop\\Arr1.xlsx"); Dictionary dic1 = new Dictionary(); for (int i = 1; i <= arr1.GetLength(0); i++) { dic1.Add(arr1[i, 1], arr1[i, 2]); } var arr2= GetObjArr("C:\\Users\\Administrator\\Desktop\\Arr2.xlsx"); List list = new List(); for (int i = 1; i <= arr2.GetLength(0); i++) { if (arr2[i,1]!=null&& arr2[i, 2] == null) { list.Add(arr2[i, 1]); } if (arr2[i, 2] != null && arr2[i, 1] == null) { list.Add(arr2[i, 2]); } } string outputpath = "C:\\Users\\Administrator\\Desktop\\Arr3.xlsx"; Excel.Application excel = new Excel.Application(); Excel.Workbook workbook = excel.Workbooks.Add(Type.Missing); Excel.Worksheet sheet = (Excel.Worksheet)workbook.ActiveSheet; for (int i = 0; i < list.Count; i++) { if (list[i].ToString().ToUpper().StartsWith("MR")) { sheet.Cells[i + 1, 2].Value = dic1[list[i]]; } else { sheet.Range[sheet.Cells[i + 1, 1], sheet.Cells[i + 1, 2]].Merge(); } sheet.Cells[i+1, 1].Value = list[i]; } workbook.SaveAs(outputpath); workbook.Close(); excel.Quit(); } public object[,] GetObjArr(string filename) { Excel.Application excel = null; excel = new Excel.Application(); Excel.Workbook wkb = null; wkb = excel.Workbooks.Open(filename); Excel._Worksheet sheet = wkb.Sheets[1]; Excel.Range range1 = sheet.UsedRange; // Read all data from data range in the worksheet var valueArray = (object[,])range1.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault); wkb.Close(true); excel.Quit(); return valueArray; }