天天看點

c#通過oledb擷取excel檔案表結構資訊

 這個問題來自論壇提問,同理可以獲得access等資料庫的表結構資訊。

c#通過oledb擷取excel檔案表結構資訊

using

 System;

c#通過oledb擷取excel檔案表結構資訊

namespace

 ConsoleApplication11

c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊

...

{

c#通過oledb擷取excel檔案表結構資訊

class

 Program

c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊

...

{

c#通過oledb擷取excel檔案表結構資訊

public

static

void

 Main()

c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊

...

{

c#通過oledb擷取excel檔案表結構資訊

            getExcelFileInfo(

@"

c:a.xls

"

);

c#通過oledb擷取excel檔案表結構資訊

        }

c#通過oledb擷取excel檔案表結構資訊

private

static

void

 getExcelFileInfo(

string

 Path)

c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊

...

{

c#通過oledb擷取excel檔案表結構資訊

string

 strConn 

=

"

Provider=Microsoft.Jet.OLEDB.4.0;

"

+

"

Data Source=

"

+

 Path 

+

"

;

"

+

"

Extended Properties=Excel 8.0;

"

;

c#通過oledb擷取excel檔案表結構資訊

            System.Data.OleDb.OleDbConnection conn 

=

new

 System.Data.OleDb.OleDbConnection(strConn);

c#通過oledb擷取excel檔案表結構資訊

            conn.Open();

c#通過oledb擷取excel檔案表結構資訊

            System.Data.DataTable table 

=

 conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, 

null

);

c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊

foreach

 (System.Data.DataRow drow 

in

 table.Rows)

c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊

...

{

c#通過oledb擷取excel檔案表結構資訊

string

 TableName 

=

 drow[

"

Table_Name

"

].ToString();

c#通過oledb擷取excel檔案表結構資訊

                Console.WriteLine(TableName

+

"

:

"

);

c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊

                System.Data.DataTable tableColumns 

=

 conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, 

new

object

[] 

...

null

null

, TableName ,

null

}

);

c#通過oledb擷取excel檔案表結構資訊

foreach

 (System.Data.DataRow drowColumns 

in

 tableColumns.Rows)

c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊

...

{

c#通過oledb擷取excel檔案表結構資訊

string

 ColumnName 

=

 drowColumns[

"

Column_Name

"

].ToString();

c#通過oledb擷取excel檔案表結構資訊

                    Console.WriteLine(

"

"

+

ColumnName);

c#通過oledb擷取excel檔案表結構資訊

                }

c#通過oledb擷取excel檔案表結構資訊

            }

c#通過oledb擷取excel檔案表結構資訊

            Console.ReadKey(

true

);

c#通過oledb擷取excel檔案表結構資訊

        }

c#通過oledb擷取excel檔案表結構資訊

    }

c#通過oledb擷取excel檔案表結構資訊

}

c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊
c#通過oledb擷取excel檔案表結構資訊