无法找到Isam可安装的CSV文件

时间:2015-03-20 13:52:24

标签: vb.net excel csv

嘿我在使用excel电子表格并将其插入Visual Studio 2013中的数据网格时遇到问题我想知道是否有人可以帮助我。

这是我目前的代码。

获取错误只有当我点击了csv时,才能找到可安装的ISAM,而其他文件是否适用于其他文件?

    OpenFileDialog1.ShowDialog()


    Dim filePath As String = OpenFileDialog1.FileName
    Dim extension As String = Path.GetExtension(filePath)
    Dim header As String = If(rbHeaderYes.Checked, "YES", "NO")
    Dim conStr As String, sheetName As String

    conStr = String.Empty
    Select Case extension

        Case ".xls"
            'Excel 97-03
            conStr = String.Format(Excel03ConString, filePath, header)
            Exit Select

        Case ".xlsx"
            'Excel 07
            conStr = String.Format(Excel07ConString, filePath, header)
            Exit Select

        Case ".csv"
            'CSV
            conStr = String.Format(ExcelCsv, filePath, header)
            Exit Select
    End Select

    'Get the name of the First Sheet.
    Using con As New OleDbConnection(conStr)
        Using cmd As New OleDbCommand()
            cmd.Connection = con
            con.Open()
            Dim dtExcelSchema As DataTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            sheetName = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
            con.Close()
        End Using
    End Using

    'Read Data from the First Sheet.
    Using con As New OleDbConnection(conStr)
        Using cmd As New OleDbCommand()
            Using oda As New OleDbDataAdapter()
                Dim dt As New DataTable()
                cmd.CommandText = (Convert.ToString("SELECT * From [") & sheetName) + "]"
                cmd.Connection = con
                con.Open()
                oda.SelectCommand = cmd
                oda.Fill(dt)
                con.Close()

                'Populate DataGridView.
                dataGridView1.DataSource = dt
            End Using
        End Using
    End Using
End Sub

0 个答案:

没有答案