0

我想从 excel 生成一个制表符分隔文件(使用公式来设置字符串)我不想使用另存为函数的原因是我将从一个电子表格生成许多不同的 csv/txt 文件以及何时你保存,主文件的扩展名被改变了。

这就是我在 Excel 中的功能,该函数必须将所有列与中间的选项卡连接起来,Desc 字段必须用双引号封装。

在此处输入图像描述

当我将单元格 D2 的内容复制到文本编辑器中时,返回以下字符串

"Toyota Corrola ""在这里描述"""

如您所见,Excel 决定将双引号放入整个字符串并转义原始引号...

有没有什么办法解决这一问题?

4

2 回答 2

0

为什么不将要放置的数据复制到 .csv 文件中,打开一个新的 Excel 实例,将数据粘贴进去,然后另存为 .csv 文件?

于 2012-08-17T15:56:39.930 回答
0

尽管 Excel 制表符分隔导出工作完美,但我需要尽快在所选内容上生成制表符分隔文件。

找到/编辑了下面的代码并为其分配了一个 Excel 宏快捷方式:

    Sub QuoteCommaExport()
    Dim DestFile As String
    Dim FileNum As Integer
    Dim ColumnCount As Integer
    Dim RowCount As Integer

    ' Prompt user for destination file name.
    DestFile = InputBox("Enter the destination filename" & _
      Chr(10) & "(with complete path and extension):", _
      "Quote-Comma Exporter", "C:\myTabFile.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 DestFile For Output As #FileNum
    ' If an error occurs report it and end.
    If Err <> 0 Then
      MsgBox "Cannot open filename " & DestFile
      End
    End If

    ' Turn error checking on.
    On Error GoTo 0

    Dim topString
    For ColumnCount = 1 To Selection.Columns.Count

        If ColumnCount < Selection.Columns.Count Then
            topString = topString & """" & ActiveSheet.Cells(ColumnCount, 1).Text & """" & vbTab
            Else
            topString = topString & """" & ActiveSheet.Cells(ColumnCount, 1).Text & """"
        End If
    Next ColumnCount
    Print #FileNum, topString

    ' Loop for each row in selection.
    For RowCount = 1 To Selection.Rows.Count
      ' Loop for each column in selection.
      For ColumnCount = 1 To Selection.Columns.Count

         ' Write current cell's text to file with quotation marks.
         Print #FileNum, """" & Selection.Cells(RowCount, _
            ColumnCount).Text & """";
         ' Check if cell is in last column.
         If ColumnCount = Selection.Columns.Count Then
            ' If so, then write a blank line.
            Print #FileNum,
         Else
            ' Otherwise, write a comma.
            ' Print #FileNum, ",";
              Print #FileNum, vbTab;
         End If
      ' Start next iteration of ColumnCount loop.
      Next ColumnCount
    ' Start next iteration of RowCount loop.
    Next RowCount

    ' Close destination file.
    Close #FileNum
End Sub
于 2012-08-17T16:26:39.827 回答