[C#]读取不同版本的excel文件的方法

--------------------------------2007及以上的版本--------------------------------

测试如下:

 1         //DataInterface.Method
 2         /// <summary>
 3         /// 读取2007版本excel
 4         /// </summary>
 5         /// <param name="filePath">文件路径</param>
 6         /// <returns>返回一个表</returns>
 7         public System.Data.DataTable GetExcel2007(string filePath)
 8         {
 9             try
10             {
11                 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties="Excel 12.0;HDR=NO;IMEX =1"";
12 
13                 OleDbConnection conn = new OleDbConnection(strConn);
14                 conn.Open();
15                 System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
16                 //获取Excel文件中第一个(按字母排序的)sheet页的页名。
17                 string tableName = schemaTable.Rows[0][2].ToString().Trim();
18 
19                 string strExcel = "SELECT  * FROM [" + tableName + "]";
20                 OleDbDataAdapter myCommand = null;
21                 myCommand = new OleDbDataAdapter(strExcel, strConn);
22                 DataSet ds = new DataSet();
23                 myCommand.Fill(ds, "dtSource");
24                 System.Data.DataTable table = ds.Tables["dtSource"];
25                 conn.Close();
26                 return table;
27             }
28             catch
29             {
30                 return null;
31             }
32         }

--------------------------------2007以下的版本--------------------------------

测试如下:

 1 /// <summary>
 2         /// 读取2003版本excel
 3         /// </summary>
 4         /// <param name="filePath"></param>
 5         /// <returns></returns>
 6         public System.Data.DataTable GetExcel2003(string filePath)
 7         {
 8             //读取OMR结果文件(xls)数据
 9             try
10             {
11         //由于windows系统的更新,无法正常使用此strConn
12         //如果继续想用请对系统更新的补丁卸载即可
13                 //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + xlsPath + 
14 
15 ";Extended Properties="Excel 8.0;HDR=YES;IMEX=1"";
16         //也可以使用以下strConn
17                 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + 
18 
19 ";Extended Properties="Excel 8.0;HDR=NO;IMEX=1"";
20                 OleDbConnection conn = new OleDbConnection(strConn);
21                 conn.Open();
22                 System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable
23 
24 (System.Data.OleDb.OleDbSchemaGuid.Tables, null);
25                 //获取Excel文件中第一个(按字母排序的)sheet页的页名。
26                 string tableName = schemaTable.Rows[0][2].ToString().Trim();
27 
28                 string strExcel = "SELECT  * FROM [" + tableName + "]";
29                 OleDbDataAdapter myCommand = null;
30                 myCommand = new OleDbDataAdapter(strExcel, strConn);
31                 DataSet ds = new DataSet();
32                 myCommand.Fill(ds, "dtSource");
33                 System.Data.DataTable table = ds.Tables["dtSource"];
34                 conn.Close();
35                 return table;
36             }
37             catch
38             {
39                 return null;
40             }
41         }
原文地址:https://www.cnblogs.com/ttkl/p/7729767.html