有時候需要批量將excel中的資料匯入到資料庫中,這時就需要獲取excel檔案中的工作表名稱,由於很多檔案都只有第乙個工作表有資料,而第乙個工作表的命名方式也不一樣,比如有的是英文命名,有的是中文命名,有的網友提供了下面的方法來獲取所有工作表的名稱
//////讀取excel中表的名稱
/// ///
///public string getexcelsheetnames(string
excelfile)
string excelsheets = new
string[dt.rows.count];
int i = 0
;
foreach (datarow row in
dt.rows)
return
excelsheets;
}catch
finally
if (dt != null
)
}}
這種方法可以獲取excel中所有工作表的名稱,但是得到的結果是經過排序的,不是按照excel中的工作表順序返回的,因此無法得到第乙個工作表的名稱。下面這種方法經過測試,可以獲取第乙個工作表名稱:
microsoft.office.interop.excel.workbook objwb = default
(microsoft.office.interop.excel.workbook);
string firstsheetname = null
;", 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);
firstsheetname = ((microsoft.office.interop.excel.worksheet)objwb.worksheets[1
]).name;
objwb.close(type.missing, type.missing, type.missing);
objwb = null
;obj.quit();
obj = null;
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...