简体   繁体   English

将数据从Excel导出到文本文件

[英]Export Data from Excel to Text File

i need help plz 我需要帮助

I have a excel file and I want to export all data from it to a text file. 我有一个excel文件,我想将所有数据从其中导出到文本文件。

This the source code: 这是源代码:


Sub ExceltoText()

    Dim FileName, sLine, Deliminator, x, y, z As String
    Dim LastCol, LastRow, FileNumber As Integer


    FileName = "C:\Users\Administrateur\Desktop\App vba\ExcToTxt.txt"

    Deliminator = "|"
    x = "$|0|Les Données de SALARIES"
    y = "=|0|Les Données de SALARIES"
    z = "=|1|SALARIES|"

    LastCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
    LastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
    FileNumber = FreeFile

    Open FileName For Output As FileNumber

    For i = 1 To LastRow
        For j = 1 To LastCol
            If j = LastCol Then
                sLine = z & sLine & Cells(i, j).Value
            Else
                sLine = sLine & Cells(i, j).Value & Deliminator
            End If
        Next j
        If i = 1 Then i = 2
        Print #FileNumber, sLine
        sLine = ""
    Next i
    Close #FileNumber
    MsgBox "File generated"
End Sub

In the result I got this text file in that format: 结果,我得到了这种格式的文本文件:


=|1|SALARIES|PSA_etablissement|PSA_SALARIE|

=|1|SALARIES|001|10635|

=|1|SALARIES|001|10637|

But I want my text file to be like that: 但是我希望我的文本文件像这样:


$=|1|SALARIES|PSA_etablissement|PSA_SALARIE|

=|1|SALARIES|001|10635|

=|1|SALARIES|001|10637|

and I want to add those lines in the beginning of the text file: 我想在文本文件的开头添加这些行:


$|0|Les Données de SALARIES

=|0|Les Données de SALARIES

To add the lines at the beginning of the file, just before the FOR loop add: 要在文件的开头添加行,只需在FOR循环之前添加:

Print #FileNumber, x
Print #FileNumber, y

To put that dollar sign before only the first line while iterating change: 在迭代更改时将美元符号仅放在第一行之前:

If i = 1 Then i = 2
Print #FileNumber, sLine
sLine = ""

To: 至:

If i = 1 Then 
   i = 2
   Print #FileNumber, "$" & sLine
Else
   Print #FileNumber, sLine
End If
sLine = ""

Try this routine: 试试这个例程:

Added the two lines outside your for loop. 在for循环外添加了两行。

Also '&' and '=' is the only difference in that line, so take it as a variable without those symbols. 同样,“&”和“ =”是该行中的唯一区别,因此请将其作为没有这些符号的变量。

Sub ExceltoText() 子ExceltoText()

Dim FileName As String, sLine As String, Deliminator As String
Dim x As String, y As String, z As String
Dim LastCol As Integer, LastRow As Integer, FileNumber As Integer

FileName = "C:\Users\Administrateur\Desktop\App vba\ExcToTxt.txt"
Deliminator = "|"
x = "$=|1|SALARIES|PSA_etablissement|PSA_SALARIE|"
y = "|0|Les Données de SALARIES"
z = "=|1|SALARIES|"

LastCol = ActiveSheet.Cells.Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = ActiveSheet.Cells.Cells(Rows.Count, 1).End(xlUp).Row

FileNumber = FreeFile

Open FileName For Output As FileNumber
Print #FileNumber, "$" & y
Print #FileNumber, "=" & y
Print #FileNumber, x

For i = 1 To LastRow
    For j = 1 To LastCol
        If j = LastCol Then
            sLine = z & sLine & Cells(i, j).Value
        Else
            sLine = sLine & Cells(i, j).Value & Deliminator
        End If
    Next j
    Print #FileNumber, sLine
    sLine = ""
Next i
Close #FileNumber
MsgBox "File generated"

End Sub 结束子

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

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