本文實例講述了C#基于OLEDB獲取Excel文件表結(jié)構(gòu)信息的方法,。分享給大家供大家參考,,具體如下:這個問題來自論壇提問,同理可以獲得access等數(shù)據(jù)庫的表結(jié)構(gòu)信息,。
namespace ConsoleApplication11 { class Program { public static void Main() { getExcelFileInfo( @" c:a.xls " ); } private static void getExcelFileInfo( string Path) { string strConn = " Provider=Microsoft.Jet.OLEDB.4.0; " + " Data Source= " + Path + " ; " + " Extended Properties=Excel 8.0; " ; System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn); conn.Open(); System.Data.DataTable table = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null ); foreach (System.Data.DataRow drow in table.Rows) { string TableName = drow[ " Table_Name " ].ToString(); Console.WriteLine(TableName + " : " ); System.Data.DataTable tableColumns = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, new object [] { null , null , TableName , null } ); foreach (System.Data.DataRow drowColumns in tableColumns.Rows) { string ColumnName = drowColumns[ " Column_Name " ].ToString(); Console.WriteLine( " " + ColumnName); } } Console.ReadKey( true ); } } } |
|