2010-10-20 75 views
2
SqlDataReader reader; 
string r=""; 
if ((FileUpload1.PostedFile != null)&&(FileUpload1.PostedFile.ContentLength > 0)) 
{ 
    r = System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName); 
} 
OleDbConnection oconn = 
    new OleDbConnection 
    (@"Provider=Microsoft.Jet.OLEDB.4.0;" 
    + @"Data Source="+r+";" 
    + @"Extended Properties=""Excel 8.0;HDR=Yes;"""); 
oconn.Open(); 
OleDbCommand dbcom = new OleDbCommand("SELECT * FROM [Sheet1$]", oconn); 
OleDbDataReader dbreader = dbcom.ExecuteReader(); 
int rni = dbreader.GetOrdinal ("RollNo"); 
int mki = dbreader.GetOrdinal ("marks"); 

这里程序只有在项目文件夹中存在excel工作表。如果对Excel工作表进行了任何更改,然后程序再次运行,那么只有之前存在的行被提取,而不是稍后添加的那些。请帮我.....在此先感谢.....问题阅读excel表

回答

0

嗨请张贴的完整代码,或者你可以导入整个工作表的DataTable对象与下面的方法和检索数据表所需的列和行

static public DataTable ExecuteOleDataTable(string sql, string oledbconnectionstring) 
    { 
     using (OleDbCommand command = new OleDbCommand()) 
     { 
      command.CommandType = CommandType.Text; 

      OleDbConnection oledbconnection = new OleDbConnection(oledbconnectionstring); 
      command.Connection = new OleDbConnection(oledbconnectionstring); ; 
      command.CommandText = sql; 

      if (oledbconnection.State == System.Data.ConnectionState.Open) 
       oledbconnection.Close(); 
      oledbconnection.Open(); 
      OleDbDataAdapter sda = new OleDbDataAdapter(command); 
      DataTable datatable = new DataTable(); 
      sda.Fill(datatable); 
      oledbconnection.Close(); 
      return datatable; 
     } 
    }