如何使用vb.net或c#从excel 2007(* .xlsx)获取工作表名称
作者:互联网
我尝试使用vb.net从文件excel中获取工作表名称并将其显示到文本框中.我试过这个代码:
Imports Microsoft.Office.Interop
Private Sub GetSheetsName
Dim efa As New Excel.Application
Dim ewb As Excel.Workbook
Dim ews As Excel.Worksheet
Dim fileName as string
fileName="D:\test.xls"
ewb = efa.Workbooks.Open(fileName)
For Each ews In ewb.Worksheets
ExcelSheetName += ews.Name & vbNewLine
Next ews
TextBox1.text=ExcelSheetName
end sub
该代码适用于文件excel * .xls,在文本框中显示来自文件test.xls的Sheets Name
Sheet1
Sheet2
Sheet3
但是当我尝试使用excel 2007(* .xlsx)时,会显示如下错误消息.
我该怎么办?你能帮我吗.
解决方法:
试试这段代码:
Private Sub GetExcelSheetNames(ByVal fileName As String)
Dim strconn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &
fileName & ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
Dim conn As New OleDbConnection(strconn)
conn.Open()
Dim dtSheets As DataTable =
conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim listSheet As New List(Of String)
Dim drSheet As DataRow
For Each drSheet In dtSheets.Rows
listSheet.Add(drSheet("TABLE_NAME").ToString())
Next
//show sheetname in textbox where multiline is true
For Each sheet As String In listSheet
TextBox1.Text = TextBox1.Text & sheet & vbNewLine
Next
conn.Close()
End Sub
标签:c,vb-net,excel-2007 来源: https://codeday.me/bug/20190718/1494281.html