现在的位置: 首页 > 综合 > 正文

用ado.net读取一个execl

2012年08月12日 ⁄ 综合 ⁄ 共 1174字 ⁄ 字号 评论关闭

  public DataTable getDataTableFromExcel(string url){
   // Create connection string variable. Modify the "Data Source"
   // parameter as appropriate for your environment.
   String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    "Data Source=" + url + ";" + "Extended Properties=Excel 8.0;";

   // Create connection object by using the preceding connection string.
   OleDbConnection objConn = new OleDbConnection(sConnectionString);

   // Open connection with the database.
   objConn.Open();

   // The code to follow uses a SQL SELECT command to display the data from the worksheet.

   // Create new OleDbCommand to return data from worksheet.
   OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);

   // Create new OleDbDataAdapter that is used to build a DataSet
   // based on the preceding SQL SELECT statement.
   OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

   // Pass the Select command to the adapter.
   objAdapter1.SelectCommand = objCmdSelect;

   // Create new DataSet to hold information from the worksheet.
   DataSet objDataset1 = new DataSet();

   // Fill the DataSet with the information from the worksheet.
   objAdapter1.Fill(objDataset1, "XLData");
   
   DataTable dt = objDataset1.Tables["XLData"];

   // Clean up objects.
   objConn.Close();
   return dt;

  }

抱歉!评论已关闭.