C 匯入Excel 讀取Excel方法總結

2021-08-09 00:28:03 字數 3166 閱讀 4031

c#匯入excel|讀取excel方法總結 這種方法一般不用,因為他讀出來的是流,一般是轉成字串

**如下:

?

01

02

streamreader sr =newstreamreader("檔名以及完整路徑",system.text.encoding.default);

stringdata = sr.readtoend();

sr.close();

2.oledbconnection讀取

最常 用的看下**吧

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

/**/

///

/// 返回excel資料來源

///

/// 檔案路徑

/// tsql

/// dataset

publicstaticdataset exceltodataset(stringfilename,stringtsql)

使用方法如下,只要寫sql語句就行了

?

01

02

03

04

//設定t_sql

stringtsql ="select  * from [sheel1$]";

//讀取資料

datatable table = exceltodataset(fullpath, tsql).tables[0];

在這種情況下大家一般都會碰到這樣的問題

如果是動態匯入很多excel**的時候,他們的表名可能不一樣,總不能每次都輸入一次表名吧,其它不需要的

只要用下面的方法就可以得到表名

請看**

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

///

/// 動態取excel表名

///

/// 檔案路徑

///

publicstaticstringgetexcelfirsttablename(stringfullpath)

}

returntablename;

}

修改上面的**如下所示

?

01

02

03

04

05

stringtablename = getexcelfirsttablename("檔案 路徑");

//設定t_sql

stringtsql ="select  * from ["+ tablename +"]";

//讀取資料

datatable table = exceltodataset(fullpath, tsql).tables[0];

C 匯入Excel 讀取Excel方法總結

原文 1.streamreader 方法 這種方法一般不用,因為他讀出來的是流,一般是轉成字串 如下 streamreader sr new streamreader 檔名以及完整路徑 system.text.encoding.default string data sr.readtoend sr....

C 讀取Excel,匯出Excel

c 讀取excel 方法一 oledb方式 1 定義連線字串 string strconn provider microsoft.jet.oledb.4.0 data source exce檔案路徑及名字 extended properties excel 8.0 hdr false imex 1 ...

C 讀取Excel,匯出Excel

c 讀取excel 方法一 oledb方式 1 定義連線字串 string strconn provider microsoft.jet.oledb.4.0 data source exce檔案路徑及名字 extended properties excel 8.0 hdr false imex 1 ...