2016-11-17 44 views
0

我想要做的是轉換器。由於它被指定爲[Sheet1 $],因此我在打開Excel文件時遇到了問題。如果工作表已重命名,我的代碼不會讀取它。有沒有辦法讓我得到任何名單的平板電腦?如何閱讀excel文件的表格,而不管它是否被重命名?

Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click 
    If cmbPortal.Text.Length = 0 Then 
     MessageBox.Show("Select portal to process first", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Warning) 
    Else 
     Select Case cmbPortal.Text 
      Case "Robinsons" 
       FileFilter = "Portable Document File (*.pdf)|*.pdf" 
       txtFilePath.Text = parse.showdialog(FileFilter) 
       If txtFilePath.Text = Nothing Then 
        MessageBox.Show("No file was selected", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information) 
       End If 
      Case "Waltermart" 
       FileFilter = "Portable Document File (*.pdf)|*.pdf" 
       txtFilePath.Text = parse.showdialog(FileFilter) 
       If txtFilePath.Text = Nothing Then 
        MessageBox.Show("No file was selected", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information) 
       End If 
      Case "7-11" 
       Dim OpenFileDialog1 As New OpenFileDialog 
       With OpenFileDialog1 
        .FileName = "Excel File" 
        .Title = "Open File" 
        .InitialDirectory = "C:\User\Desktop" 
        .Filter = "EXCEL|*.xls;*.xlsx;*.xlsm;*.xlsb" 
        .FilterIndex = 1 
       End With 
       If OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then 
        Me.Cursor = Cursors.WaitCursor 
        Try 
         Dim FileName As String = OpenFileDialog1.FileName 
         Dim strConnString As String 
         strConnString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & FileName & ";" 
         Dim strSQL As String 
         strSQL = "SELECT * FROM [Sheet1$]" 
         Dim y As New Odbc.OdbcDataAdapter(strSQL, strConnString) 
         y.Fill(RetVal) 
         txtFilePath.Text = FileName 
        Catch 
         MessageBox.Show("An Error Occurred, Please Contact MIS for Assistance", "SYSTEM", MessageBoxButtons.OK, MessageBoxIcon.Error) 
        End Try 
        Me.Cursor = Cursors.Default 
       ElseIf txtFilePath.Text = Nothing Then 
        MessageBox.Show("No file was selected", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information) 
       End If 
       Me.Cursor = Cursors.Default 
     End Select 
     If txtFilePath.Text.Length = 0 Then 
      btnSelect.Enabled = True 
      btnConvert.Enabled = False 
      cmbPortal.Enabled = True 
     Else 
      btnSelect.Enabled = False 
      btnConvert.Enabled = True 
      cmbPortal.Enabled = False 
     End If 
    End If 
End Sub 
+1

您正在檢索的工作表的索引是否已修復?例如。它總是第一張工作表嗎? –

+0

根據你的問題的內容,你的標題也是誤導性的。 –

+1

它始終是第一張工作表,並且基於代碼,只有在第一張工作表被命名爲Sheet1時纔會指定它。我想要做的是仍然閱讀第一張紙,不管它是否被重命名。我對標題感到非常抱歉,感謝您的注意。 – Biboy

回答

1

試試這個:

Dim xlApp As New Microsoft.Office.Interop.Excel.Application 
Dim xlWorkBook As Microsoft.Office.Interop.Excel.WorkBook= xlApp.Workbooks.Open(FileName) 

Dim SheetName As String = xlWorkBook.Worksheets(1).Name.ToString 

現在,你有工作表名稱,你可以把它串聯到您的strSQL像這樣的:

strSQL = "SELECT * FROM [" & SheetName & "$]"

這會給你的工作簿中的第一張工作表。

+1

哇謝謝!它非常棒!只是說Microsoft.Office.Interop.Excel.Workook。缺少工作簿的「b」。非常感謝! – Biboy

+0

感謝您指出。這是來自我的「藏匿」代碼,甚至在我發佈之前就沒有對它進行測試。我很高興它有幫助。快樂的編碼! –

相關問題