如何使用vb.net或C#从excel 2007(* .xlsx)获取工作表名称 [英] how to get sheets name from excel 2007 (*.xlsx) using vb.net or c#
本文介绍了如何使用vb.net或C#从excel 2007(* .xlsx)获取工作表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用vb.net从excel文件中获取工作表名称,并将其显示在文本框中.我尝试使用此代码:
I try to get sheets name from file excel using vb.net and show them into textbox. I was try with this code:
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中的表格名称
That code was work for file excel *.xls, in textbox show Sheets Name from file test.xls
Sheet1
Sheet2
Sheet3
但是当我尝试使用excel 2007(* .xlsx)时,则显示这样的错误消息.
But when I try with excel 2007 (*.xlsx), then show an error message like this.
我该怎么办?你能帮我吗?
What should I do? Can you help me please.
推荐答案
尝试以下代码:
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
这篇关于如何使用vb.net或C#从excel 2007(* .xlsx)获取工作表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文