本文实例讲述了C#基于OLEDB获取Excel文件表结构信息的方法。分享给大家供大家参考,具体如下:
这个问题来自论坛提问,同理可以获得access等数据库的表结构信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
using System;
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 );
}
}
}
|
希望本文所述对大家C#程序设计有所帮助。