如何将没有分隔符的.txt文件转换为Excel

时间:2015-12-06 16:14:04

标签: excel vba vbscript

我在.txt文件中获得了多个数据,这些数据没有被任何分隔符分隔。它只是一串字符串。到目前为止,我做到了这一点,但我现在被困住了。在此之后我应该继续在哪里?以下是我到目前为止的样本。有人可以澄清下一步该做什么吗?

Dim objFSO
Dim TextFile
Dim TextRead
Dim Line, Line1, Line2, Line3
Dim Count

Const ForReading = 1 'Constant declared so that can be used throughout the script

'Name of the text file that need to be convert
TextFile = "C:\Documents and Settings\Administrator\Desktop\2_12_2015\HRILOANDIC20170601.txt"

'Create File system object
set objFSO = CreateObject("Scripting.FileSystemObject")

'set the text file to read and open it in read-only mode
set TextRead = objFSO.OpenTextFile(TextFile,ForReading)

Do Until TextRead.AtEndOfStream
    Line = TextRead.ReadLine

    If Left(Line, 1) = "H" Then
        HeaderRecordType = Mid(Line, 1, 1)
        HeaderSequenceNo = Mid(Line, 2, 9)
        HeaderContractNo = Mid(Line, 11, 19)
        HeaderCreationBy = Mid(Line, 30, 1)
        HeaderTransactionDate = Mid(Line, 31, 8)
        HeaderTotalRecord = Mid(Line, 39, 9)
        HeaderTotalAmount = Mid(Line, 48, 17)
        HeaderSource = Mid(Line, 65, 2)
        HeaderFiller = Mid(Line, 67, 334)

    ElseIf Left(Line, 1) = "D" Then
        DetailRecordType = Mid(Line, 1, 1)
        DetailSequenceNo = Mid(Line, 2, 9)
        DetailContractNo = Mid(Line, 11, 19)
        DetailPaymentType = Mid(Line, 30, 10)
        DetailSettlementType = Mid(Line, 40, 1)
        DetailEffectiveDate = Mid(Line, 41, 8)
        DetailCreditAccountNo = Mid(Line, 49, 19)
        DetailCreditAccountType = Mid(Line, 68, 1)
        DetailCrTransactionAmount = Mid(Line, 69, 17)
        DetailLoanType = Mid(Line, 86, 10)
        DetailBankEmployeeID = Mid(Line, 96, 40)
        DetailIDNumber = Mid(Line, 136, 40)
        DetailIDTypeCode = Mid(Line, 176, 3)
        DetailBankEmployeeName = Mid(Line, 179, 200)
        DetailHRISProcessStatus = Mid(Line, 379, 1)
        DetailCIFnumber = Mid(Line, 380, 19)
        DetailAccountBranch = Mid(Line, 399, 5)

    ElseIf Left(Line, 1) = "T" Then
        TrailerRecordType = Mid(Line, 1, 1)
        TrailerSequenceNo = Mid(Line, 2, 9)
        TrailerTotalRecord = Mid(Line, 30, 9)
        TrailerTotalAmount = Mid(Line, 39, 17)
        TrailerFiller = Mid(Line, 56, 345)
    Else
        'Error Handling
    End If

Loop


ExcelFilePath = "C:\Documents and Settings\Administrator\Desktop\2_12_2015\Output.xlsx"

'Open the spreadsheet using the excel application object
Set objExcel = CreateObject("Excel.Application")
'ExcelObject.WorkBooks.Open ExcelObject

Set SheetObject = ExcelObject.ActiveWorkbook.Worksheets(1)

'Modify data in a cell (in this case we are adding data to c2)
'First value in the brackets = Column number
'2nd value = Cell number
SheetObject.Cells(3, 2).Value = "Test"

'Save and quit
ExcelObject.ActiveWorkbook.Save
ExcelObject.ActiveWorkbook.Close
ExcelObject.Application.Quit

objExcel.Visible = True
objExcel.Workbooks.Add

intRow = 2

objExcel.Cells(1, 1).Value="?" 'Name of a column

2 个答案:

答案 0 :(得分:2)

  1. 打开工作表。

  2. 将数据循环写入工作表,而不是像您一样存储它。

  3. 保存并关闭

  4. 像这样。

    Dim objFSO
    Dim TextFile
    Dim TextRead
    Dim Line, Line1, Line2, Line3
    Dim Count
    
    Const ForReading = 1 'Constant declared so that can be used throughout the script
    
    'Name of the text file that need to be convert
    TextFile = "C:\Documents and Settings\Administrator\Desktop\2_12_2015\HRILOANDIC20170601.txt"
    
    'Create File system object
    set objFSO = CreateObject("Scripting.FileSystemObject")
    
    'set the text file to read and open it in read-only mode
    set TextRead = objFSO.OpenTextFile(TextFile,ForReading)
    
    'Open the spreadsheet using the excel application object
    ExcelFilePath = "C:\Documents and Settings\Administrator\Desktop\2_12_2015\Output.xlsx"
    Set objExcel = CreateObject("Excel.Application")
    Set SheetObject = ExcelObject.ActiveWorkbook.Worksheets(1)
    
    'Change from storing the data in the below variables to writing them to the worksheet.
    
    'Create yourself a counter for the worksheet row.
    Dim lRow as long
    lRow = 1
    
    Do Until TextRead.AtEndOfStream
        Line = TextRead.ReadLine
    
        If Left(Line, 1) = "H" Then
            SheetObject.Range("A" & lRow).Value = Mid(Line, 1, 1)
            SheetObject.Range("B" & lRow).Value = Mid(Line, 2, 9)
            SheetObject.Range("C" & lRow).Value = Mid(Line, 11, 19)
            SheetObject.Range("D" & lRow).Value = Mid(Line, 30, 1)
            SheetObject.Range("E" & lRow).Value = Mid(Line, 31, 8)
            SheetObject.Range("F" & lRow).Value = Mid(Line, 39, 9)
            SheetObject.Range("G" & lRow).Value = Mid(Line, 48, 17)
            SheetObject.Range("H" & lRow).Value = Mid(Line, 65, 2)
            SheetObject.Range("I" & lRow).Value = Mid(Line, 67, 334)
    
        ElseIf Left(Line, 1) = "D" Then
            SheetObject.Range("A" & lRow).Value = Mid(Line, 1, 1)
            SheetObject.Range("B" & lRow).Value = Mid(Line, 2, 9)
            SheetObject.Range("C" & lRow).Value = Mid(Line, 11, 19)
            SheetObject.Range("D" & lRow).Value = Mid(Line, 30, 10)
            SheetObject.Range("E" & lRow).Value = Mid(Line, 40, 1)
            SheetObject.Range("F" & lRow).Value = Mid(Line, 41, 8)
            SheetObject.Range("G" & lRow).Value = Mid(Line, 49, 19)
            SheetObject.Range("H" & lRow).Value = Mid(Line, 68, 1)
            SheetObject.Range("I" & lRow).Value = Mid(Line, 69, 17)
            SheetObject.Range("J" & lRow).Value = Mid(Line, 86, 10)
            SheetObject.Range("K" & lRow).Value = Mid(Line, 96, 40)
            SheetObject.Range("L" & lRow).Value = Mid(Line, 136, 40)
            SheetObject.Range("M" & lRow).Value  = Mid(Line, 176, 3)
            SheetObject.Range("N" & lRow).Value = Mid(Line, 179, 200)
            SheetObject.Range("O" & lRow).Value  = Mid(Line, 379, 1)
            SheetObject.Range("P" & lRow).Value  = Mid(Line, 380, 19)
            SheetObject.Range("Q" & lRow).Value = Mid(Line, 399, 5)
    
        ElseIf Left(Line, 1) = "T" Then
            SheetObject.Range("A" & lRow).Value = Mid(Line, 1, 1)
            SheetObject.Range("B" & lRow).Value = Mid(Line, 2, 9)
            SheetObject.Range("C" & lRow).Value  = Mid(Line, 30, 9)
            SheetObject.Range("D" & lRow).Value  = Mid(Line, 39, 17)
            SheetObject.Range("E" & lRow).Value = Mid(Line, 56, 345)
        Else
            'Error Handling
        End If
    
        '  ************************ Don't overlook that i added this! **********
        'Increment the counter
        lRow = lRow + 1
    
    Loop
    
    'Then save and quit.
    
    'Save and quit
    ExcelObject.ActiveWorkbook.Save
    ExcelObject.ActiveWorkbook.Close
    ExcelObject.Application.Quit
    

答案 1 :(得分:1)

我已修改您的代码以直接写入Excel工作簿。我没有完全完成它,但给了你足够的自己修改其余部分。我还评论了你在哪里有一些语法和一般理解错误。根据需要进行任何列和行分配调整。

Dim objFSO
Dim TextFile
Dim TextRead
Dim Line, Line1, Line2, Line3
Dim Count

'Open the spreadsheet using the excel application object
ExcelFilePath = "C:\Documents and Settings\Administrator\Desktop\2_12_2015\Output.xlsx"

Set objExcel = CreateObject("Excel.Application")
Set objWB = objExcel.Workbooks.Open(ExcelFilePath) ' have to open workbook 
Set SheetObject = objWB.Worksheets(1) 'worksheets are a member of workbooks, not the Excel Application

'open the text file
Const ForReading = 1 'Constant declared so that can be used throughout the script

'Name of the text file that need to be convert
TextFile = "C:\Documents and Settings\Administrator\Desktop\2_12_2015\HRILOANDIC20170601.txt"

'Create File system object
set objFSO = CreateObject("Scripting.FileSystemObject")

'set the text file to read and open it in read-only mode
set TextRead = objFSO.OpenTextFile(TextFile,ForReading)

i = 1 'to set row number for Excel paste

Do Until TextRead.AtEndOfStream

    Line = TextRead.ReadLine

    If Left(Line, 1) = "H" Then
        SheetObject.Cells(i, 1).Value = Mid(Line, 1, 1) 'HeaderRecordType to column A
        SheetObject.Cells(i, 2).Value = Mid(Line, 2, 9) 'ValueHeaderSequenceNo to column b
        SheetObject.Cells(i, 3).Value = Mid(Line, 11, 19) 'HeaderContractNo to column C

        '... keep going with your code

    Else
        'Error Handling
    End If

    i = i + 1 'to move down the Excel row to paste for each line in the text file
Loop

'Save and quit
objWB.Save
objWB.Close
objExcel.Quit