0

我使用了一些代码使用 ADO 将文本文件导入 Excel,如下所示:

    Sub ImportTextFile()
    'Imports text file into Excel workbook using ADO.
    'If the number of records exceeds 65536 then it splits it over more than one sheet.

        Dim strFilePath As String, strFilename As String, strFullPath As String
        Dim lngCounter As Long
        Dim oConn As Object, oRS As Object, oFSObj As Object

        'Get a text file name
        strFullPath = Application.GetOpenFilename("Text Files (*.txt),*.txt", , "Please select text file...")

        If strFullPath = "False" Then Exit Sub  'User pressed Cancel on the open file dialog

        'This gives us a full path name e.g. C:\temp\folder\file.txt
        'We need to split this into path and file name
        Set oFSObj = CreateObject("SCRIPTING.FILESYSTEMOBJECT")

        strFilePath = oFSObj.GetFile(strFullPath).ParentFolder.Path
        strFilename = oFSObj.GetFile(strFullPath).Name

        'Open an ADO connection to the folder specified
        Set oConn = CreateObject("ADODB.CONNECTION")
        oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                   "Data Source=" & strFilePath & ";" & _
                   "Extended Properties=""text;HDR=Yes;FMT=Delimited"""

        Set oRS = CreateObject("ADODB.RECORDSET")

        'Now actually open the text file and import into Excel
        oRS.Open "SELECT * FROM [" & strFilename & "]", oConn, 3, 1, 1
        While Not oRS.EOF
            Sheets("Sheet1").Select
             ActiveSheet.Range("A1").CopyFromRecordset oRS, 65536
            Range("A1").Select
            Range(Selection, Selection.End(xlToRight)).Select
            Range(Selection, Selection.End(xlDown)).Select
            Selection.Copy
            Range("C1").Select
            ActiveSheet.Paste
            Range("E1").Select
            ActiveSheet.Paste
            .
            .
            .
            Range("CW1").Select
            ActiveSheet.Paste
            Range("CY1").Select
            ActiveSheet.Paste

            Range("A1").Select
            Range(Selection, Selection.End(xlToRight)).Select
            Range(Selection, Selection.End(xlDown)).Select
            Selection.Copy
            Sheets("Recorder Log").Select
            Range("A9").Select
            ActiveSheet.Paste

            Range("C8").Select
            Range(Selection, Selection.End(xlToRight)).Select
            Application.CutCopyMode = False
            Selection.Copy
            Range("C9").Select
            Range(Selection, Selection.End(xlToRight)).Select
            Range(Selection, Selection.End(xlDown)).Select
            ActiveSheet.Paste

            Sheets("Sheet1").Select
            Cells.Select
            Selection.Delete Shift:=xlUp
            Sheets("Recorder Log").Select
            Range("C9").Select
        Wend

        oRS.Close
        oConn.Close

    End Sub

我认为代码运行良好,但后来注意到我正在导入的文件中的第一行文本没有被复制到 Excel 中。

是否有任何原因会发生这种情况,是否有解决方案可以避免在开始时使用空白行预先格式化文本文件?

提前致谢。

简历

4

1 回答 1

2

您已通过连接属性设置HDR=Yes,这意味着第一行是包含列名的标题。由于您没有标题列,因此您应该设置HDR=No

oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
               "Data Source=" & strFilePath & ";" & _
               "Extended Properties=""text;HDR=No;FMT=Delimited"""
于 2013-06-03T13:48:45.440 回答