Excel 一次写入多行而不是使用WriteLine来提高性能?

Excel 一次写入多行而不是使用WriteLine来提高性能?,excel,vba,csv,Excel,Vba,Csv,我正在创建一个函数来创建带有自定义分隔符(例如:“;”或“-”而不是“,”)的CSV文件 我有一个FOR循环,它将我范围内的每一行写入CSV,但是由于CSV是在远程服务器中创建的,所以逐行执行非常缓慢。我的目的是创建一个字符串,其中包含循环中的所有文件,并打印到循环外部的CSV 有可能这样做吗 这就是我现在正在做的: Set fso = CreateObject("Scripting.FileSystemObject") Set oFile = fso.CreateTextFile(FilePa

我正在创建一个函数来创建带有自定义分隔符(例如:“;”或“-”而不是“,”)的CSV文件

我有一个FOR循环,它将我范围内的每一行写入CSV,但是由于CSV是在远程服务器中创建的,所以逐行执行非常缓慢。我的目的是创建一个字符串,其中包含循环中的所有文件,并打印到循环外部的CSV

有可能这样做吗

这就是我现在正在做的:

Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(FilePath)
ColsCount = wb.Sheets(1).UsedRange.Columns.Count
RowsCount = wb.Sheets(1).UsedRange.Rows.Count

With wb.Sheets(1)
    For r = 1 To RowsCount
        Line = Join(Application.Transpose(Application.Transpose(.range(.Cells(r, 1), .Cells(r, ColsCount)).Value)), ";")
        Call oFile.WriteLine(Line) ' Can I Remove this?...
    Next r
End With

' ...and do the writing to the file here?

Call oFile.Close
Set fso = Nothing
Set oFile = Nothing

您可以使用
vbNewLine
分隔符将所有行连接起来,形成一个大字符串,然后一次将其全部推出。我不确定在这里使用FileSystemObject有什么好处。这里有一个例子

Sub CustomCSV()

    Dim rRow As Range
    Dim lCnt As Long
    Dim aLines() As String
    Dim wf As WorksheetFunction
    Dim sFile As String, lFile As Long

    Set wf = Application.WorksheetFunction
    ReDim aLines(1 To Sheet1.UsedRange.Rows.Count)

    For Each rRow In Sheet1.UsedRange.Rows
        lCnt = lCnt + 1
        aLines(lCnt) = Join(wf.Transpose(wf.Transpose(rRow.Value)), ";")
    Next rRow

    lFile = FreeFile
    sFile = Environ$("TEMP") & Application.PathSeparator & "test.csv"
    Open sFile For Output As lFile
    Print #lFile, Join(aLines, vbNewLine)
    Close lFile

End Sub

假设文件小于2GB/RAM,请尝试以下操作:

Dim CSVString As String
ColsCount = wb.Sheets(1).UsedRange.Columns.Count
RowsCount = wb.Sheets(1).UsedRange.Rows.Count

With wb.Sheets(1)
    For r = 1 To RowsCount
        Line = Join(Application.Transpose(Application.Transpose(.range(.Cells(r, 1), .Cells(r, ColsCount)).Value)), ";")
        CSVString = CSVString & Line & VbCrLf           'Append each Line together with an end of line to form the full contents of the CSV as a String
    Next r
End With

' ...and do the writing to the file here? Yup!
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(FilePath)
oFile.Write CSVString

Call oFile.Close
Set fso = Nothing
Set oFile = Nothing

一次写一段文字是可能的。按如下方式调整代码:

Dim completeFile as String
With wb.Sheets(1)
    For r = 1 To RowsCount
        Line = Join(Application.Transpose(Application.Transpose(.range(.Cells(r, 1), .Cells(r, ColsCount)).Value)), ";")
        completeFile = completeFile + Line + vbNewLine
    Next r
End With
Call oFile.write(completeFile)
这样,您可以将行存储在
completeFile
中,并一次性写入(
.write
而不是
.writeLine
)。请注意,这会给您留下一个
vbNewLine
太多(在末尾),您可以使用以下方法删除:
completeFile=left(completeFile,len(completeFile)-1)

如果您的
completeFile
-变量此时不是空的。

您可以创建一个包含换行符的很长字符串,然后将该字符串写入文本文件吗?CSV文件有多大?如果它小于2GB(并且您有足够的RAM),那么请务必将其写入驱动器一次。如果它会更大,将其分块写入本地临时文件,然后将文件复制到目标驱动器可能会更快。向其中添加vbcrlf如何?等等,让我试着给你回复…为什么不在本地编写然后远程复制呢?另外,如果你不介意这个无耻的插件,你可以使用这个VBA类来提高构建CSV字符串的性能。