鳴少
︶ㄣ花自飄零水自流べ壹種相思べ兩處閑愁
 

 private DataSet ReadExcelToTable( tableName  )
        {
            string ConnectionExcel = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + ExcelPath + ";" + "Extended Properties=Excel 8.0;";      

           OleDbConnection connection = new OleDbConnection( ConnectionExcel );
            OleDbDataAdapter adp = new OleDbDataAdapter( "Select * from  [" + tableName + "$]", connection );
            DataSet DataSet = new DataSet();
            adp.Fill( DataSet, "Book1" );
            return DataSet;
        }

上面是读取内容的,那里的tableName就是Excel里面的工作表名

取工作表名的方法:
           string ConnectionExcel = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + ExcelPath + ";" + "Extended Properties=Excel 8.0;";

            OleDbConnection conn = new OleDbConnection( ConnectionExcel );
            conn.Open();
            System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable( System.Data.OleDb.OleDbSchemaGuid.Tables, null );

posted on 2007-11-09 09:10  鳴少  阅读(198)  评论(0)    收藏  举报