Getting the first sheet from an Excel document regardless of sheet name with OleDb

前端 未结 9 673
青春惊慌失措
青春惊慌失措 2020-12-29 02:23

I have users that name their sheets all sorts of crazy things, but I want to be able to get the first sheet of the Excel document regardless of what it is named.

I

9条回答
  •  悲&欢浪女
    2020-12-29 02:33

    You can get sheet1 name like this and use in this manner.If you want to get other sheet names you can increase value from 0,1,2..

     Dim myExcelConn As OleDbConnection = _
                    New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
                        Server.MapPath(".") & "\" & FileUpload1.FileName() & _
                        ";Extended Properties=Excel 12.0;")
        Dim Sheets As DataTable = myExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
                    Sheet1 = Sheets.Rows(0)("TABLE_NAME").ToString()
      Dim objOleDB As New OleDbCommand("SELECT *FROM [" + Sheet1 + "]", myExcelConn)
    

提交回复
热议问题