如何上传.csv文件?

时间:2017-06-21 14:57:01

标签: vb.net

这是我的代码,我收到错误消息:

  

d:\ VPN_TFS_ETP \ ETP_DEV \ EndoAdmin \上传\ Employers_C_d5pjixjyhcwojaypfyw0paoj_Master   客户报告 - Reloadable.csv'不是一个有效的道路。确保   路径名称拼写正确,并且您已连接到   文件所在的服务器。

我要上传的文件扩展名为.csv。

Dim oOLEDBConn As New OleDb.OleDbConnection
Dim oDA As New OleDb.OleDbDataAdapter
Dim oFile As HttpPostedFile, strFileName As String, strOrigName As String

Try

    strOrigName = System.IO.Path.GetFileName(File1.Value)
    oFile = File1.PostedFile
    '
    ' Keep Copy in the Server
    '
    strFileName = Request.ServerVariables("APPL_PHYSICAL_PATH") & "Uploads\Employers_" & oUser.Usr_UserID & "C_" & Session.SessionID & "_" & strOrigName
    oFile.SaveAs(strFileName)

    'strFileName = Request.ServerVariables("APPL_PHYSICAL_PATH") & "Uploads\Co_" & Session.SessionID & "_" & strOrigName
    'oFile.SaveAs(strFileName)

    Dim OConnectionString As String, myStrmReader As New System.IO.StreamReader(strFileName)
    'OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFileName + ";Extended Properties=Excel 8.0;"

    Try
        'OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + "\""
        OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFileName + ";Extended Properties='Text;HDR=Yes;FMT=Delimited;'"
        'OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFileName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"
        'OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " + strFileName + "; Extended Properties=HTML Import;"
        oOLEDBConn.ConnectionString = OConnectionString
        oOLEDBConn.Open()

    Catch ex1 As Exception
        If ex1.Message.StartsWith("External table is not in the expected format") Then
            'Try
            '    oOLEDBConn.Close()

            '    ' Load as HTML
            '    OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFileName + ";Extended Properties='HTML Import;HDR=Yes;IMEX=1;'"
            '    oOLEDBConn.ConnectionString = OConnectionString
            '    oOLEDBConn.Open()
            'Catch ex2 As Exception
            '    lblFeedBack.Text = "22:" & ex2.Message
            '    Exit Sub
            'End Try
            lblFeedBack.Text = ex1.Message '& " Please see below."
            Exit Sub
        Else
            lblFeedBack.Text = ex1.Message
            Exit Sub
        End If
    End Try

1 个答案:

答案 0 :(得分:0)

使用连接字符串访问csv文件时,不要指向要打开的文件名,而是指向要提交的文件夹。稍后在查询中指定文件名。 所以,你的代码可能是这样的:

bytes

稍后,当您想要按文件内容运行时。你应该做点什么:

Dim folderWhereIsYourCsv = Request.ServerVariables("APPL_PHYSICAL_PATH") & "Uploads\"
OConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + folderWhereIsYourCsv + ";Extended Properties='Text;HDR=Yes;FMT=Delimited;'"
oOLEDBConn.ConnectionString = OConnectionString
oOLEDBConn.Open()