Obtain the sheet name (VB. NET) of the Excel file)

Source: Internet
Author: User

1. Get the name of the first sheet.

Public shared function getfirstsheetname (byval strfilepath as string) as string
Dim myexcel as Excel. Application = new excel. Application
Dim missing as object = system. reflection. Missing. Value
Myexcel. application. workbooks. Open (strfilepath)
Dim mybook as Excel. Workbook = myexcel. workbooks (1)
Dim sheet as Excel. worksheet = mybook. Sheets (1)
Dim strsheetname as string = sheet. Name
Mybook. Close ()
Myexcel. Quit ()
Return strsheetname
End Function

2. Get the name of all sheet.

Public shared function getallsheetname (byval strfilepath as string) as string ()
Dim strconn as string = string. Empty
If strfilepath. endswith ("xls") then
Strconn = "provider = Microsoft. Jet. oledb.4.0;" +
"Data Source =" + strfilepath + ";" +
"Extended properties = 'excel 8.0; IMEX = 1 '"
Elseif strfilepath. endswith ("XLSX") then
Strconn = "provider = Microsoft. Ace. oledb.12.0;" +
"Data Source =" + strfilepath + ";" +
"Extended properties =" "Excel 12.0; HDR = yes """
End if
Dim conn as oledbconnection = new oledbconnection (strconn)
Conn. open ()
Dim sheetnames (conn. getschema ("tables"). Rows. Count-1) as string
For I as integer = 0 to conn. getschema ("tables"). Rows. Count-1
Sheetnames (I) = conn. getschema ("tables"). Rows (I) ("table_name"). tostring
Next
Conn. Close ()
Return sheetnames
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.