c 讀取Excel的第乙個Sheet表

2021-09-05 22:55:09 字數 1736 閱讀 7526

有朋友問道:

public

arraylist excelsheetname(

string

filepath)

);conn.close();

foreach

(datarow dr 

insheetnames.rows)

return

al;}

這個方法獲取的excel表名是按字母順序排列的,如果我要獲得excel的第乙個工作表名,怎麼做?

其實利用framework提供的excel 物件模型很容易解決這個問題,下面是邀月的測試過程及**:

新建一控制台專案,新增兩個dll

microsoft.office.interop.excel

microsoft.visualbasic

如圖:

新增**:

using

system;

using

system.collections.generic;

using

system.text;

//add namespace

using

microsoft.office.interop.excel;

namespace

readfirstsheetnamedemo

public

static

string

getfirstsheetnamefromexcelfilename(

string

filepath,

intnumbersheetid)

if(numbersheetid 

<=1) 

try=

default

=default

(microsoft.office.interop.excel.workbook);

string

strfirstsheetname 

=null

;obj ="

", string

.empty);

objwb 

=obj.workbooks.open(filepath, type.missing, type.missing,

type.missing, type.missing, type.missing, type.missing, 

type.missing,type.missing, type.missing, type.missing, 

type.missing, type.missing,type.missing, type.missing);

strfirstsheetname 

=((microsoft.office.interop.excel.worksheet)objwb.worksheets[

1]).name;

objwb.close(type.missing, type.missing, type.missing);

objwb 

=null

;obj.quit();

obj 

=null

;return

strfirstsheetname;

}catch

(exception err)}}

}

結果:

參考資料:

c 讀取Excel的第乙個Sheet表

有朋友問道 public arraylist excelsheetname string filepath conn.close foreach datarow dr in sheetnames.rows return al 這個方法獲取的excel表名是按字母順序排列的,如果我要獲得excel的第...

c 讀取Excel的第乙個Sheet表

有朋友問道 public arraylist excelsheetname string filepath conn.close foreach datarow dr in sheetnames.rows return al 這個方法獲取的excel表名是按字母順序排列的,如果我要獲得excel的第...

c 讀取Excel的第乙個Sheet表

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!有朋友問道 c sharp view plain copy print?public arraylist excelsheetname string filepath conn.close foreach datarow dr in sheetna...