publicoverride DataTable GetUserDataDTList(string excelPath)
02.
03. {
04.
05. excelPath = excelPath.Trim();
06.
07. string filename =null;
08.
09. try
10.
11. {
12.
13. filename = System.Configuration.ConfigurationSettings.AppSettings["filename"].ToString();
14.
15. }
16.
17. catch(Exception ex)
18.
19. {
20.
21. filename =null;
22.
23. throw ex;
24.
25. }
26.
27. DataTable dt =new DataTable();
28.
29. //DataSet ds = new DataSet();
30.
31. if (string.IsNullOrEmpty(excelPath))
32.
33. {
34.
35. dt =null ;
36.
37. //ds = null;
38.
39. }
40.
41. else
42.
43. {
44.
45. try
46.
47. {
48.
49. string strConn =@"Provider = Microsoft.ACE.OLEDB.12.0;Data Source="+ excelPath +";Extended Properties = 'Excel 12.0;HDR=Yes;IMEX=1;'";
50.
51. using(OleDbConnection oconn =new OleDbConnection(strConn))
52.
53. {
54.
55. OleDbCommand ocmd =null;
56.
57. oconn.Open();
58.
59. try
60.
61. {
62.
63. ocmd =new OleDbCommand("SELECT * FROM ["+filename+"$]", oconn);//在web.config中配置
64.
65. OleDbDataAdapter oda =new OleDbDataAdapter(ocmd);
66.
67. oda.Fill(dt);
68.
69. }
70.
71. catch(OleDbException oex)
72.
73. {
74.
75. dt =null;
76.
77. throw oex;
78.
79. //ds = null;
80.
81. }
82.
83. finally
84.
85. {
86.
87. oconn.Close();
88.
89. ocmd.Dispose();
90.
91. }
92.
93. }
94.
95. }
96.
97. catch(Exception ex)
98.
99. {
100.
101. throw ex;
102.
103. }
104.
105. }
106.
107. //return ds.Tables[0];
108.
109. return dt;
110.
111. }
02.
03. {
04.
05. excelPath = excelPath.Trim();
06.
07. string filename =null;
08.
09. try
10.
11. {
12.
13. filename = System.Configuration.ConfigurationSettings.AppSettings["filename"].ToString();
14.
15. }
16.
17. catch(Exception ex)
18.
19. {
20.
21. filename =null;
22.
23. throw ex;
24.
25. }
26.
27. DataTable dt =new DataTable();
28.
29. //DataSet ds = new DataSet();
30.
31. if (string.IsNullOrEmpty(excelPath))
32.
33. {
34.
35. dt =null ;
36.
37. //ds = null;
38.
39. }
40.
41. else
42.
43. {
44.
45. try
46.
47. {
48.
49. string strConn =@"Provider = Microsoft.ACE.OLEDB.12.0;Data Source="+ excelPath +";Extended Properties = 'Excel 12.0;HDR=Yes;IMEX=1;'";
50.
51. using(OleDbConnection oconn =new OleDbConnection(strConn))
52.
53. {
54.
55. OleDbCommand ocmd =null;
56.
57. oconn.Open();
58.
59. try
60.
61. {
62.
63. ocmd =new OleDbCommand("SELECT * FROM ["+filename+"$]", oconn);//在web.config中配置
64.
65. OleDbDataAdapter oda =new OleDbDataAdapter(ocmd);
66.
67. oda.Fill(dt);
68.
69. }
70.
71. catch(OleDbException oex)
72.
73. {
74.
75. dt =null;
76.
77. throw oex;
78.
79. //ds = null;
80.
81. }
82.
83. finally
84.
85. {
86.
87. oconn.Close();
88.
89. ocmd.Dispose();
90.
91. }
92.
93. }
94.
95. }
96.
97. catch(Exception ex)
98.
99. {
100.
101. throw ex;
102.
103. }
104.
105. }
106.
107. //return ds.Tables[0];
108.
109. return dt;
110.
111. }
说明:该方法需要一个参数用来获取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. publicstatic 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. }
02.
03. /// 查看Excel文件中重复的数据
04.
05. /// 该方法只是比较编号和客户名称,其他字段没有比较
06.
07. ///</summary>
08.
09. ///<param name="excelPath">string:Excel文件路径</param>
10.
11. ///<returns>ArrayList:重复行组成的动态数组</returns>
12.
13. publicstatic 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. }