/// <summary>
2 /// 读取Excel文件,将内容存储在DataSet中
3 /// </summary>
4 /// <param name="opnFileName">带路径的Excel文件名</param>
5 /// <returns>DataSet</returns>
6 private DataSet ExcelToDataSet(string opnFileName)
7 {
8 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+opnFileName+";Extended Properties=/"Excel 8.0;HDR=YES;IMEX=1/"";
9 OleDbConnection conn = new OleDbConnection(strConn);
10 string strExcel = "";
11 OleDbDataAdapter myCommand = null;
12 DataSet ds = new DataSet();
13 strExcel = "select * from [sheet1$]";
14 try
15 {
16 conn.Open();
17 myCommand = new OleDbDataAdapter(strExcel, strConn);
18 myCommand.Fill(ds,"dtSource");
19 return ds;
20 }
21 catch (Exception ex)
22 {
23 MessageBox.Show("导入出错:" + ex, "错误信息");
24 return ds;
25 }
26 finally
27 {
28 conn.Close();
29 conn.Dispose();
30 }
31 }