繁体   English   中英

将Excel数据导出到固定宽度的文本文件-字段位置

[英]Export Excel data to fixed-width text file - field locations

首先,我要说我是处理分隔文件的新手。 我试图模拟一个软件如何使用Excel布置文本文件。

这是我用来从工作表创建文本文件的代码:

Sub Export_Selection_As_Fixed_Length_File()
     ' Dimension all  variables.
    Dim DestinationFile, CellValue, Filler_Char_To_Replace_Blanks As String
    Dim FileNum, ColumnCount, RowCount, FieldWidth As Integer
    Dim sht As Worksheet

    'Below are options incase you want to change the folder where VBA stores the .txt file
    'We use ActiveWorkbook.Path in this example
    'ActiveWorkbook.Path 'the activeworkbook
    'ThisWorkbook.Path  'the workbook with the code
    'CurDir  'the current directory (when you hit File|open)


    'If a cell is blank, what character should be used instead
    Filler_Char_To_Replace_Blanks = " "

        'Check if the user has made any selection at all
        If Selection.Cells.Count < 2 Then
            MsgBox "Nothing selected to export"
            Selection.Activate
            End
        End If

    'This is the destination file name.
    DestinationFile = ActiveWorkbook.Path & "/textfile.txt"
    'Obtain next free file handle number.
    FileNum = FreeFile()

     ' Turn  error checking off.
    On Error Resume Next

     ' Attempt to open destination file for output.
    Open DestinationFile For Output As #FileNum

     ' If an error occurs report it and end.
    If Err <> 0 Then
         MsgBox "Cannot open filename " & DestinationFile
         Selection.Activate
        End
    End If

     ' Turn error checking on.
    On Error GoTo 0

     '  Loop for each row in selection.
    For RowCount = 1 To Selection.Rows.Count
                For ColumnCount = 1 To Selection.Columns.Count
                    CellValue = Selection.Cells(RowCount, ColumnCount).Text
                    If (IsNull(CellValue) Or CellValue = "") Then CellValue = Filler_Char_To_Replace_Blanks
                    FieldWidth = Cells(1, ColumnCount).Value
                    If (ColumnCount = Selection.Columns.Count) Then
                            Print #FileNum, Format$(CellValue, "!" & String(FieldWidth, "@")) & vbCrLf;
                    Else: Print #FileNum, Format$(CellValue, "!" & String(FieldWidth, "@"));
                    End If
                Next ColumnCount
         ' Start next iteration of RowCount loop.
    Next RowCount
     ' Close destination file.
    Close #FileNum
    Selection.Activate
    Workbooks.OpenText Filename:=DestinationFile
End Sub

我要模拟的软件具有“数据位置”和“字段大小”。 例如,一个字段的数据位置为77,这意味着它将以文本文件中该行的第77个字符开头。 (我不知道这是多么普遍,因此,如果非常普遍,请原谅无用的信息。)字段大小为12。

如果这没有意义,请使用以下文本文件的屏幕截图。 第一行显示了我的VBA创建的内容,第二行显示了我希望它的外观。 如何根据工作表所在的列强制工作表上的值从行中的某个位置开始?

在此处输入图片说明

看起来您选择的第一行包含字段的宽度FieldWidth = Cells(1, ColumnCount).Value 在问题描述中,您提到了数据位置和字段大小。 您需要在某些地方获得此信息。 您可以将其放在文件的另一张纸上,这将允许您调整文本文件的输出,或者可以将这些值作为常量放入VBA代码中,或者可以创建类。 使用类似的方法将使您可以根据需要重新定义字段。 下面的示例在模块中使用一个简单的类和一些私有函数

在下面的示例中,您将需要添加一个名为“ FieldControl”的工作表并将适当的值放在各列中。请参阅GetFieldControl函数。 为了测试代码,我使用了以下代码:

在此处输入图片说明

您需要在宏工作簿中添加以下参考 在“工具”菜单下的VBA编辑器中,选择“引用”,然后在出现对话框时,选择“ Microsoft脚本运行时”。 (工具->参考)

与所有与代码相关的事物一样,可以对此进行改进。

祝您好运

(Insert-> Class)将默认名称更改为clField(您可以随意调用它,但请确保更新dim语句GetFieldControl函数以匹配您给它的名称。)

Option Explicit

Public Enum eFieldType
    Number
    Text
End Enum

Public Name As String
Public Size As Long
Public StartPos As Long
Public Value As String
Public FieldType As eFieldType

具有一些更新的模块

Option Explicit
Option Base 1    'This makes any defined array start a 1 rather than 0

Sub Export_Selection_As_Fixed_Length_File()
     ' Dimension all  variables.
    Dim DestinationFile, CellValue, Filler_Char_To_Replace_Blanks As String
    Dim FileNum, ColumnCount, RowCount, FieldWidth As Integer
    Dim sht As Worksheet

    Dim outputRecord() As String
    'Below are options in case you want to change the folder where VBA stores the .txt file
    'We use ActiveWorkbook.Path in this example
    'ActiveWorkbook.Path 'the activeworkbook
    'ThisWorkbook.Path  'the workbook with the code
    'CurDir  'the current directory (when you hit File|open)

    'If a cell is blank, what character should be used instead
    Filler_Char_To_Replace_Blanks = "+"

    'Check if the user has made any selection at all
    If Selection.Cells.Count < 2 Then
        MsgBox "Nothing selected to export"
        Selection.Activate
        End
    End If

    'This is the destination file name.
    DestinationFile = ActiveWorkbook.Path & "\textfile.txt"  'This was changed to the DOS version of directory separator

    On Error GoTo catchFileOpenError    'Poor man's version of Try/Catch

    'Get a FileSystemObject using the MSFT Scripting Runtime reference
    Dim fd As Scripting.FileSystemObject
    Set fd = New Scripting.FileSystemObject

    Dim outputFile As Object
    Set outputFile = fd.CreateTextFile(DestinationFile, True, False)

    ' Turn error checking on.
    On Error GoTo 0

    Dim record As Scripting.Dictionary
    'Call a private function that gets the filed control information from the
    'Sheet titled FieldControl and the associated range
    Set record = GetFieldControl(ActiveWorkbook.Sheets("FieldControl").Range("A2:D7"))

    'Declare enumerators to loop through the selection
    Dim dataRow As Range
    Dim dataFld As Range

    'Declare the output buffer, 80 characters
    Dim outputBuffer(80) As Byte
    'loop thru the selection row by row
    For Each dataRow In Selection.Rows
        'Initialize buffer to empty value defined by the second parameter
        Call InitOutputBuffer(outputBuffer, Filler_Char_To_Replace_Blanks)
        'Loop thru each field in the row
        For Each dataFld In dataRow.Columns
            'Copy the input value into the output byte array
            Call CopyStringToByteArray(outputBuffer, StrConv(Trim(CStr(dataFld.Value2)), vbFromUnicode), _
                        record(dataFld.Column).StartPos, record(dataFld.Column).FieldType, record(dataFld.Column).Size)
        Next dataFld
        'Write the record to the text file but first convert ASCII Byte to Unicode String
        'Also this method places CR/LF as part of the output to the file
        outputFile.WriteLine StrConv(outputBuffer, vbUnicode)
    Next dataRow

     ' Close destination file.
    outputFile.Close

    Selection.Activate
    Workbooks.OpenText Filename:=DestinationFile
    Exit Sub

catchFileOpenError:     'Catch the error after trying if openning the file fails
    On Error GoTo 0
    MsgBox "Cannot open filename " & DestinationFile
    Selection.Activate
End Sub
'***********************************************************************************
'*
'* PARAMETERS:
'*  outBuf is the updated buffer
'*  inBuf is the input buffer that needs to be copied to the output buffer (buffer)
'*  startCol is the starting column for the field
'*  fldTy is the field type as defined by the class enumerator eFieldType
'*  fldLen is the length of the field as defined on the control sheet
Private Sub CopyStringToByteArray(ByRef outBuf() As Byte, ByRef inBuf() As Byte, _
                ByVal startCol As Long, ByRef fldTy As eFieldType, ByVal fldLen As Long)
    Dim idx As Long
    If fldTy = Text Then       'Left Justified
        For idx = LBound(inBuf) To UBound(inBuf)
            outBuf(startCol) = inBuf(idx)
            startCol = startCol + 1
        Next idx
    Else                        'Right Justified
        Dim revIdx As Long
        revIdx = startCol + fldLen - 1
        For idx = UBound(inBuf) To LBound(inBuf) Step -1
            outBuf(revIdx) = inBuf(idx)
            revIdx = revIdx - 1
        Next idx
    End If
End Sub
'***************************************************************************
'*  InitOutputBuffer
'*      PARAMETERS:
'*          buffer is the buffer to initialize
'*          initVal is a string containing the value used to initialize the buffer
Private Sub InitOutputBuffer(ByRef buffer() As Byte, ByVal initVal As String)
    Dim byInitVal() As Byte 'Byte array to hold the values from the string conversion
    byInitVal = StrConv(initVal, vbFromUnicode) 'convert the string into an ASCII array
    Dim idx As Long
    For idx = LBound(buffer) To UBound(buffer)
        buffer(idx) = byInitVal(0)
    Next idx

    'buffer(81) = Asc(Chr(13)) 'Carriage Return Character
    'buffer(82) = Asc(Chr(10)) 'Line Feed Character

End Sub

'*******************************************************************************
'*
'*  GetFieldControl
'*      PARAMETERS:
'*          ctrlRng is the range on a worksheet where the field control info is
'*              found
'*      REMARKS:
'*          The range needs to have the following columns: Name, Size, Start Postion
'*          and Type.  Type values can be Text or Number
Private Function GetFieldControl(ByRef ctrlRng As Range) As Scripting.Dictionary
    Dim retVal As Scripting.Dictionary
    Set retVal = New Scripting.Dictionary

    'format of control range is : Name, Size, Start Position, Type
    Dim fldInfoRow As Range
    Dim fld As clField  'A class that holds the control values from the work sheet
    Dim colCnt As Long: colCnt = 1  'Becomes the key for the dictionary
    For Each fldInfoRow In ctrlRng.Rows
        Set fld = New clField
        fld.Name = fldInfoRow.Value2(1, 1)      'Name of field in data table
        fld.Size = fldInfoRow.Value2(1, 2)      'Output Size of field
        fld.StartPos = fldInfoRow.Value2(1, 3)  'Output starting position for this field
        Select Case fldInfoRow.Value2(1, 4)     'Controls how the output value is formated
            Case "Text"                         '  Text left justified, Numbers are right justified
                fld.FieldType = Text
            Case "Number"
                fld.FieldType = Number
            Case Default
                fld.FieldType = Text
        End Select
        retVal.Add Key:=colCnt, Item:=fld   'Add the key and the fld object to the dictionary
        colCnt = colCnt + 1                 'This key value is mapped to the column number in the input data table
    Next fldInfoRow

    'Return the scripting Dictionary
    Set GetFieldControl = retVal
End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM