ado.net访问Excel表如何判断指定表是否存在?

解决方案 »

  1.   

    if(System.IO.File.Exists(Server.MapPath("路徑")+"\\"+文件名))
    {
    this.lblInfo.Text="該文件名憶存在";
    return;
    }
      

  2.   

    LS的那个是找Execl文件的,貌似LZ要的是Excel里的sheet表
      

  3.   

    先把EXCEL转换为DATASET ,然后查找指定的表名.我就是这样做的.EXCEL其实就是一个数据库,使用的方法和链接数据库一样的.代码如下:
          public DataSet ExcelToDS(string path)
           {
               DataSet ds = null;
               string tableName = null;
               OleDbConnection conn = null;
               ds = new DataSet();
               try
               {
                   string strConn = GetExcelConnetionStr(path);
                    conn = new OleDbConnection(strConn);
                   conn.Open();
                   string strExcel = "";
                   OleDbDataAdapter myCommand = null;
                   DataTable tmpExcelTable= conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                   for (int i = 0; i < tmpExcelTable.Rows.Count; i++)
                   {
                       try
                       {
                           tableName = tmpExcelTable.Rows[i]["TABLE_NAME"].ToString();
                           strExcel = "select * from [" + tableName + "]";
                           myCommand = new OleDbDataAdapter(strExcel, strConn);                       myCommand.Fill(ds, tableName);
                       }
                       catch (Exception ex)
                       {
                           string error = ex.Message.ToString();
                       }
                   }
                   return ds;
               }
               finally
               {
                   if (conn != null)
                   {
                       conn.Close();
                       conn.Dispose();
                   }
               }
           }可以填充为一个DATASET ,每个EXCEL内部表为DATASET中的相应的表...不过...说句实话..EXCEL中很多临时表平时看不到,导入进去的时候就发现了.....非常恶心..
      

  4.   

    遍历excel文件中的工作表,看是否有你要的工作表名,就这么简单。。