0

目前我在将csv文件导入devexpress gridview时遇到问题,当我执行代码时,显示以下错误'C:\New folder\QtimeAutomotiveByLot_new.csv' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.并且文件路径在我这边工作得很好。我的代码如下,有人可以指导我吗?

<dx:ASPxGridView ID="DetailGridx" runat="server"  OnDataBinding="DetailGridx_DataBinding">

VB.net

  Protected Sub DetailGridx_DataBinding(sender As Object, e As EventArgs)

        Dim dt1 As New DataTable()
        Dim csvFileFolder As String = "C:\New folder\QtimeAutomotiveByLot_New.csv"
        'Dim csvFile As String = "QtimeAutomotiveByLot_New.csv"
        Dim strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + csvFileFolder + ";Extended Properties='Text;HDR=YES;IMEX=1;';"
        Dim connx As New OleDbConnection(strCon)
        Dim adapter1 As New OleDbDataAdapter
        connx.Open()

        Dim sql As New OleDbCommand("Select * FROM [" + csvFileFolder + "]", connx)

        adapter1.SelectCommand = sql

        adapter1.Fill(dt1)

        connx.Close()

        Dim detailGrid As ASPxGridView = CType(sender, ASPxGridView)
        Dim RowLotID As String = TryCast(sender, ASPxGridView).GetMasterRowFieldValues("LOTID")
        Dim ddata As DataView = New DataView(dt1)
        ddata.RowFilter = "LOTID = '" + RowLotID + "'"
        detailGrid.DataSource = ddata
        detailGrid.DataBind()

    End Sub
4

1 回答 1

1

听起来您当前读取 CSV 文件的连接字符串是错误的。与读取 Excel 文件不同,读取 CSV 文件时,我们不指定实际文件名,而是指定它所属的目录路径(请参阅此问题)。

以下示例显示如何使用 Jet 4.0 提供程序读取 CSV 文件。请注意,不是设置IMEX=1,而是使用FMT=Delimited属性,因为IMEX主要用于 XLS 和 XLSX 文件格式:

Protected Sub DetailGridx_DataBinding(sender As Object, e As EventArgs)

    Dim dt1 As New DataTable()
    Dim csvFileFolder As String = "C:\New folder\"
    Dim csvFile As String = "QtimeAutomotiveByLot_New.csv"

    ' specify directory path containing CSV file as data source
    Dim strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + csvFileFolder + ";Extended Properties='Text;HDR=YES;FMT=Delimited';"

    Dim connx As New OleDbConnection(strCon)
    Dim adapter1 As New OleDbDataAdapter
    connx.Open()

    ' specify actual file name here
    Dim sql As New OleDbCommand("Select * FROM [" + csvFile + "]", connx)

    adapter1.SelectCommand = sql

    adapter1.Fill(dt1)

    connx.Close()

    Dim detailGrid As ASPxGridView = CType(sender, ASPxGridView)
    Dim RowLotID As String = TryCast(sender, ASPxGridView).GetMasterRowFieldValues("LOTID")
    Dim ddata As DataView = New DataView(dt1)
    ddata.RowFilter = "LOTID = '" + RowLotID + "'"
    detailGrid.DataSource = ddata
    detailGrid.DataBind()

End Sub
于 2018-08-21T09:14:27.893 回答