创建三个csv文件的VBA宏

时间:2019-06-12 07:38:03

标签: excel vba csv validation converters

我有以下VBA代码,它们基于我创建的excel表创建了四个文件.csv csv.sha512 csv.sha256和.csv.gz。

Private Sub Export_Click()

Version = "1.0"
MyFileName = ActiveWorkbook.Name
MyFilePath = ActiveWorkbook.Path
MyFileName = Replace(MyFileName, ".xls", ".idl")
IdFiles = 1
fileName = MyFilePath & "\" & Cells(1, 3).Value & "_" & Format(Date, "yyyymmdd") & "_" & IdFiles & ".csv"

SelectionOnly = False
AppendData = False
delimeter = "|"
Application.ScreenUpdating = False
On Error GoTo EndMacro:


If SelectionOnly = True Then
    With Selection
        StartRow = 7
        StartCol = 2
        EndRow = .Cells(.Cells.Count).Row
        EndCol = .Cells(.Cells.Count).Column
    End With
Else
    With ActiveSheet.UsedRange
        StartRow = 7
        StartCol = 2
        EndRow = .Cells(.Cells.Count).Row
        EndCol = .Cells(.Cells.Count).Column
    End With
End If

CountRow = EndRow - StartRow + 1


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSOBlokada = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(fileName) Then
    IsExistCSV = True


    Do While IsExistCSV = True
       fileName = MyFilePath & "\" & Cells(1, 3).Value & "_" & Format(Date, "yyyymmdd") & "_" & IdFiles & ".csv"

       If Not objFSO.FileExists(fileName) Then
          IsExistCSV = False
       End If
       IdFiles = IdFiles + 1 
    Loop
End If

Set objFile = objFSO.OpenTextFile(fileName, 2, True, -1)
objFile.Writeline Version & delimeter & CountRow

    For RowNdx = StartRow To EndRow
        WholeLine = ""
        For ColNdx = StartCol To EndCol

            If Cells(RowNdx, ColNdx).Value = "" Then
                CellValue = ""
            Else
               CellValue = Cells(RowNdx, ColNdx).Value
            End If


            If ColNdx = EndCol Then
                WholeLine = WholeLine & CellValue
            Else
                WholeLine = WholeLine & CellValue & delimeter
            End If
        Next ColNdx
        WholeLine = Left(WholeLine, Len(WholeLine) - Len(sep))
        objFile.Writeline WholeLine
    Next RowNdx

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True

objFile.Close



Set objFileBlokada = objFSOBlokada.OpenTextFile(fileName & ".blokada", 2, True, -1)
objFileBlokada.Close


Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 0

shellcmd1 = "cmd.exe /C certutil -hashfile """ & fileName & """ SHA512 | find /i /v ""sha512"" | find /i /v ""certutil"" > """ & fileName & ".sha512"""
wsh.Run shellcmd1, windowStyle, waitOnReturn


shacmd256 = "cmd.exe /C certutil -hashfile """ & fileName & """ SHA256 | find /i /v ""sha256"" | find /i /v ""certutil"" > """ & fileName & ".sha256"""


shellcmd2 = "cmd /C """"c:\Program Files\7-Zip\7z.exe"" a -w""" & MyFilePath & """ -tgzip """ & fileName & ".gz"" " & """" & fileName & """"""


End Sub

但是,创建的文件是使用BOM表和UCS2编码的,我需要使用utf-8编码的文件,其行尾为LF,而现在我的行​​尾为crlf。如何解决?

还有,我需要没有BOM的utf-8,这很难实现。

任何想法都该如何处理?

0 个答案:

没有答案