获取特定的 excel(.xls) 单元格



有没有办法使用此代码在Excel中获取特定单元格?

 Try
        OpenFileDialog.InitialDirectory = "Desktop"
        OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel Files (*.xlsx)|*.xlsx|Xls Files (*.xls)|*.xls"
        If OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
            Dim fi As New IO.FileInfo(OpenFileDialog.FileName)
            Dim Filename As String = OpenFileDialog.FileName
            excel = fi.FullName
            conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0;")
            dta = New OleDbDataAdapter("select * from [Sheet1$]", conn)
            dts = New DataSet
            dta.Fill(dts, "Sheet1$")
            DGV1.DataSource = dts
            DGV1.DataMember = "Sheet1$"
            conn.Close()
        End If
        lblRowCount.Text = DGV1.Rows.Count - 1
    Catch ex As Exception
        MsgBox(ex.Message)
        conn.Close()
        Exit Sub
    End Try

或者有没有其他方法可以使用循环逐个单元格插入单元格将 Excel 文件导入 DataGridView?

您可以使用

此代码打印C2单元格值

'--------  Option Strict off ---------------
    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
                Dim mypath As String
                Dim xlApp As New Excel.Application
                Dim wb As Excel.Workbook
                Dim ws As Excel.Worksheet
                mypath = "F:Documentsmyfile.xlsx"
                wb = xlApp.Workbooks.Open(mypath)
                ws = xlApp.Worksheets(1)
                MessageBox.Show(ws.cells(2, 3).value + " in cell C2")
                wb.Close()
                xlApp.Quit()
        End Sub
    ' --------- option strict on ------------------
    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
        Dim percorso As String
        Dim xlApp As New Excel.Application
        Dim wb As Excel.Workbook
        Dim ws As Excel.Worksheet
        mypath = "F:Documentsmyfile.xlsx"
        wb = xlApp.Workbooks.Open(mypath)
        ws = CType(xlApp.Worksheets(1), Excel.Worksheet)
        Dim xRng As Excel.Range = CType(ws.Cells(2, 3), Excel.Range)
        Dim val As Object = xRng.Value()
        MessageBox.Show(val.ToString + " in cell C2")
        wb.Close()
        xlApp.Quit()
    End Sub

最新更新