将文件上传到datagridview

时间:2017-08-15 08:12:22

标签: vb.net isam

我有这个程序,我试图上传一个excel文件,并将在我的datagridview中显示,但我有这个错误 Additional information: Could not find installable ISAM. 我是VB新手请帮助我

这是我的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim Con As System.Data.OleDb.OleDbConnection
    Dim ds As System.Data.DataSet
    Dim cmd As System.Data.OleDb.OleDbDataAdapter


    Con = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source='c:\manpower.xlsx';Extended Properties=cc;")
    cmd = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet2$]", Con)

    ds = New System.Data.DataSet
    cmd.Fill(ds)
    DataGridView1.DataSource = ds.Tables(0)

    Con.Close()
End Sub

2 个答案:

答案 0 :(得分:1)

为什么你首先使用OLEDB连接?为什么不用sqlConnection?

如果可以,请使用以下内容:

Private Sub LoadFormData()
    Dim cmdText as String
    Dim ds as DataSet
    cmdText = "SELECT ID, FName, SName, Title FROM PeopleTable;"
    ds = GetQueryResults(cmdText)
    Me.DataGridView1.DataSource = ds.Tables(0)
End Sub

Private Function GetQueryResults(cmdText As String)
    Dim oConn As New SqlConnection
    oConn.ConnectionString = "........."   ' your connection string
    Dim cmd As New SqlCommand(cmdText, oConn)
    Dim ds As New DataSet()
    Dim da As New SqlDataAdapter(cmd)

    Try
        oConn.Open()
        da.Fill(ds)
        oConn.Close()
    Catch ex As Exception    ' handle error any way you like
        Dim errform As New SysErrScreen
        errform.ErrText.Text = ex.Message & vbCrLf & vbCrLf & cmdText 
        errform.ShowDialog()
        oConn.Close()
    End Try

    Return ds
End Function

答案 1 :(得分:1)

您可以轻松地将Excel文件导入DataGridView,并从DataGridView导出到Excel。

Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Data.OleDb

'~~> Define your Excel Objects


Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim xlApp As New Excel.Application
        'Dim xlWorkBook As Excel.Workbook
        'Dim xlWorkSheet As Excel.Worksheet

        Dim strConn As String
        Dim da As OleDbDataAdapter
        Dim ds As New DataSet
        Dim dao_dbE As dao.DBEngine
        Dim dao_DB As DAO.Database
        Dim strFirstSheetName As String

        dao_dbE = New dao.DBEngine
        dao_DB = dao_dbE.OpenDatabase("C:\your_path_here\Book1.xls", False, True, "Excel 8.0;")
        strFirstSheetName = dao_DB.TableDefs(0).Name
        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=C:\your_path_here\Book1.xls;Extended Properties=""Excel 8.0;"""

        da = New OleDbDataAdapter("SELECT * FROM [" & _
        strFirstSheetName & "]", strConn)
        da.TableMappings.Add("Table", "Excel")
        da.Fill(ds)

        DataGridView1.DataSource = ds.Tables(0).DefaultView

        da.Dispose()
        dao_DB.Close()

    End Sub


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        Dim rowsTotal, colsTotal As Short
        Dim I, j, iC As Short
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
        Dim xlApp As New Excel.Application

        Try
            Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
            Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
            xlApp.Visible = True
            rowsTotal = DataGridView1.RowCount - 1
            colsTotal = DataGridView1.Columns.Count - 1

            With excelWorksheet
                .Cells.Select()
                .Cells.Delete()
                For iC = 0 To colsTotal
                    .Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
                Next

                For I = 0 To rowsTotal - 1
                    For j = 0 To colsTotal - 1
                        .Cells(I + 2, j + 1).value = DataGridView1.Rows(I).Cells(j).Value
                    Next j
                Next I

                .Rows("1:1").Font.FontStyle = "Bold"
                .Rows("1:1").Font.Size = 10
                .Cells.Columns.AutoFit()
                .Cells.Select()
                .Cells.EntireColumn.AutoFit()
                .Cells(1, 1).Select()
            End With

        Catch ex As Exception
            MsgBox("Export Excel Error " & ex.Message)
        Finally
            'RELEASE ALLOACTED RESOURCES
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            xlApp = Nothing

        End Try


    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        For Each col As DataGridViewColumn In Me.DataGridView1.Columns
            xlWorkSheet.Cells(1, col.Index + 1) = col.HeaderText.ToString
        Next
        Try
            For CurrentRowIndex = 0 To DataGridView1.RowCount - 1 'current row index
                'For j = 0 To Me.DataGridView1.ColumnCount

                For CurrentColumnIndex = 0 To DataGridView1.ColumnCount - 1 'current column index within row index
                    xlWorkSheet.Cells(2, CurrentColumnIndex + 1) = DataGridView1.Columns(CurrentColumnIndex).HeaderText 'display header
                    xlWorkSheet.Cells(CurrentRowIndex + 3, CurrentColumnIndex + 1) = DataGridView1(CurrentColumnIndex, CurrentRowIndex).Value.ToString()
                Next

                'xlWorkSheet.Cells(2, CurrentColumnIndex + 1) = DataGridView1.Columns(CurrentColumnIndex).HeaderText 'display header
                'xlWorkSheet.Cells(i + 2, j + 1) = Me.DataGridView1(j, i).Value.ToString()
                'xlWorkSheet.Cells(2, CurrentColumnIndex + 1) = DataGridView1.Columns(CurrentColumnIndex).HeaderText 'display header
                'Next
            Next

        Catch ex As Exception
            MsgBox("Unable to extract data" & ex.Message, MsgBoxStyle.Critical)
            Exit Sub
        End Try

        xlWorkBook.Activate()

        '//get path
        Me.FolderBrowserDialog1.ShowDialog()
        Dim path As String = Me.FolderBrowserDialog1.SelectedPath

        xlWorkBook.SaveAs(path & "\Excel_With_Headers.xls")
        'xlWorkSheet.SaveAs("burn permit export.xls")

        xlWorkBook.Close()
        xlApp.Quit()

        'releaseObject(xlApp)
        'releaseObject(xlWorkBook)
        'releaseObject(xlWorkSheet)

        MsgBox("You can find your report at " & path & "\burn permit export.xls")
    End Sub


    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click

        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        Try
            For CurrentRowIndex = 0 To DataGridView1.RowCount - 1 'current row index
                'xlWorkSheet.Cells(1, 1) = "With Headers"
                For CurrentColumnIndex = 0 To DataGridView1.ColumnCount - 1 'current column index within row index
                    xlWorkSheet.Cells(2, CurrentColumnIndex + 1) = DataGridView1.Columns(CurrentColumnIndex).HeaderText 'display header
                    xlWorkSheet.Cells(CurrentRowIndex + 3, CurrentColumnIndex + 1) = DataGridView1(CurrentColumnIndex, CurrentRowIndex).Value.ToString()
                Next
            Next
        Catch ex As Exception
            MsgBox("Unable to extract data" & ex.Message, MsgBoxStyle.Critical)
            Exit Sub
        End Try
    End Sub
End Class