代码之家  ›  专栏  ›  技术社区  ›  Apis

excel到.tsv文件,但仍有逗号和引号

  •  0
  • Apis  · 技术社区  · 6 年前

    我试图通过运行下面的代码来创建一个新的.tsv文件但即使我将其设置为.tsv,输出仍在.csv文件中有什么办法解决这个问题吗?

    Sub toTxt()
        Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
        myFile = Application.DefaultFilePath & "\PO" & Format(Now(), "yyyymmddhhmmss") & ".tsv"
        iLast = ThisWorkbook.Sheets("PO_Master").Range("C" & Rows.Count).End(xlUp).Row
    
        Set rng = ThisWorkbook.Sheets("PO_Master").Range("A7:BA" & iLast)
        Open myFile For Output As #2
        For i = 1 To rng.Rows.Count
            For j = 1 To rng.Columns.Count
                cellValue = rng.Cells(i, j).Value
    
                If j = rng.Columns.Count Then
                    Write #2, cellValue
                Else
                    Write #2, cellValue,
                End If
            Next j
        Next i
    
        Close #2
    

    样本输出

    ASA,"AA","BB","CC","DD","EE","FF"
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   CLR    6 年前

    试着建立一个字符串 outputline (你需要声明)并且只写每个 .

    For i = 1 To rng.Rows.Count
        outputline = ""
        For j = 1 To rng.Columns.Count
            cellValue = rng.Cells(i, j).Value
    
            If outputline = "" Then
                outputline = outputline & cellValue
            Else
                outputline = outputline & Chr(9) & cellValue
            End If
    
        Next j
        Print #2, outputline
    
    Next i
    
    推荐文章