说明:该方法需要一个参数用来获取Excel文件的路径,找到后放到一个DataTable中。 对于第二条,为了提高性能,我是这样实现的,每次遍历时只跟它后面的数据比较,因为它前面的数据已经跟它比较过了,没必要它再跟前面的数据比较
/// <summary> 02 . 03 . /// 查看Excel文件中重复的数据 04 . 05 . /// 该方法只是比较编号和客户名称,其他字段没有比较 06 . 07 . /// </summary> 08 . 09 . /// <param name="excelPath"> string:Excel文件路径 </param> 10 . 11 . /// <returns> ArrayList:重复行组成的动态数组 </returns> 12 . 13 . public static ArrayList arrCFData( string excelPath) 14 . 15 . { 16 . 17 . ArrayList arrUserData = new ArrayList(); 18 . 19 . excelPath = excelPath.Trim(); 20 . 21 . if ( string .IsNullOrEmpty(excelPath)) 22 . 23 . { 24 . 25 . arrUserData = null ; 26 . 27 . } 28 . 29 . else 30 . 31 . { 32 . 33 . string ret = null ; 34 . 35 . DataTable dtCompInfo = null ; 36 . 37 . try 38 . 39 . { 40 . 41 . dtCompInfo = DF.DBFactory.GetCompanyDAO().GetUserDataDTList(excelPath); 42 . 43 . if (dtCompInfo != null && dtCompInfo.Rows.Count > 0 ) 44 . 45 . { 46 . 47 . int count = dtCompInfo.Rows.Count; 48 . 49 . int x, y; 50 . 51 . for ( int i = 0 ; i < count;i ++ ) 52 . 53 . { 54 . 55 . for ( int j = i + 1 ; j < count; j ++ ) 56 . 57 . { 58 . 59 . if (dtCompInfo.Rows[i][ 0 ].ToString().Equals(dtCompInfo.Rows[j][ 0 ].ToString()) && dtCompInfo.Rows[i][ 3 ].ToString().Equals(dtCompInfo.Rows[j][ 3 ].ToString())) 60 . 61 . { 62 . 63 . x = i + 2 ; 64 . 65 . y = j + 2 ; 66 . 67 . ret = string .Format( " Excel数据中第{0}行和第{1}行数据重复 " ,x, y); 68 . 69 . arrUserData.Add(ret); 70 . 71 . break ; 72 . 73 . } 74 . 75 . } 76 . 77 . // arrUserData.Add(dtCompInfo.Rows[i][0].ToString() + "出来吧" + dtCompInfo.Rows[i][3].ToString()); 78 . 79 . 80 . 81 . } 82 . 83 . } 84 . 85 . } 86 . 87 . catch (Exception ex) 88 . 89 . { 90 . 91 . ExceptionManager.SaveException(ex, " Company.cs " , ex.Message); 92 . 93 . arrUserData = null ; 94 . 95 . } 96 . 97 . finally 98 . 99 . { 100 . 101 . dtCompInfo = null ; 102 . 103 . } 104 . 105 . } 106 . 107 . return arrUserData; 108 . 109 . }转载于:https://www.cnblogs.com/Mr0909/archive/2011/05/09/2041523.html
