将Excel文件保存为制表符分隔的文本文件,不带引号

发布于 2024-12-29 23:06:25 字数 280 浏览 3 评论 0原文

我有一个 Excel 2010 工作簿。我需要将每个工作表的使用范围保存为制表符分隔的文本文件,不带引号,文件名与工作簿相同,扩展名由工作表名称指定。

请注意,只要 Excel 看到逗号,就会愚蠢地用引号将值括起来,即使分隔符是制表符;除此之外,正常的“另存为”/“文本(制表符分隔)”就可以了。

我更愿意使用 Excel 中的 VBA 代码来完成此操作。

如果有Python解决方案,我也会感兴趣。但目前 pywin32 对 Python 3 的支持还只是实验性的,所以我不确定我是否可以使用它。

I have an Excel 2010 workbook. I need to save the used range of each of its worksheets as a tab-delimited text file with no quotes, with the same filename as the workbook and with an extension given by the worksheet name.

Note that Excel stupidly surrounds a value by quotes whenever it sees a comma, even though the delimiter is a tab; other than that, the normal "Save As" / "Text (Tab delimited)" would be fine.

I would prefer to do that using VBA code from within Excel.

If there is a Python solution, I'd be interested too. But at this point pywin32 support for Python 3 is only experimental, so I am not sure I can use it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘东忘西忘不掉你 2025-01-05 23:06:25

好的,这是一个稍微复杂的例程,是我几个月前为我的一位客户写的。此代码将 Excel 工作表导出到不带引号的固定宽度文件。还附上了屏幕截图。我相信这段代码可以做得更好:)

经过尝试和测试

Option Explicit

'~~> Change this to relevant output filename and path
Const strOutputFile As String = "C:\Output.Csv"

Sub Sample()
    Dim ws As Worksheet
    Dim rng As Range
    Dim MyArray() As Long, MaxLength As Long
    Dim ff As Long, i As Long, lastRow As Long, LastCol As Long
    Dim strOutput As String

    On Error GoTo Whoa

    Application.ScreenUpdating = False

    '~~> Change this to the respective sheet
    Set ws = Sheets("Sheet1")
    LastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column

    '~~> Loop through each Column to get the max size of the field
    For i = 1 To LastCol
        MaxLength = getMaxLength(ws, i)
        ReDim Preserve MyArray(i)
        MyArray(i) = MaxLength
    Next i

    ff = FreeFile

    '~~> output file
    Open strOutputFile For Output As #ff

    '~~> Write to text file
    With ws
        lastRow = .Range("A" & Rows.Count).End(xlUp).Row

        For Each rng In .Range("A1:A" & lastRow)
            With rng
                For i = 1 To UBound(MyArray)
                    '~~> Insert a DELIMITER here if your text has spaces
                    strOutput = strOutput & " " & Left(.Offset(0, i-1).Text & _
                                String(MyArray(i), " "), MyArray(i))
                Next i

                Print #ff, Mid(Trim(strOutput), 1)
                strOutput = Empty
            End With
        Next rng
    End With

LetsContinue:
    On Error Resume Next
        Close #ff
    On Error GoTo 0
    Application.ScreenUpdating = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

'~~> Function to get the max size
Public Function getMaxLength(ws As Worksheet, Col As Long) As Long
    Dim lastRow As Long, j As Long

    getMaxLength = 0

    lastRow = ws.Range("A" & ws.Rows.Count).End(-4162).Row

    For j = 1 To lastRow
        If Len(Trim(ws.Cells(j, Col).Value)) > getMaxLength Then _
        getMaxLength = Len(Trim(ws.Cells(j, Col).Value))
    Next j
End Function

在此处输入图像描述

Ok here is a slightly complex routine which I wrote couple of months back for one of my clients. This code exports the Excel Worksheet to a Fixed Width File without QUOTES. Screenshots also attached. I am sure this code can be made even better :)

TRIED AND TESTED

Option Explicit

'~~> Change this to relevant output filename and path
Const strOutputFile As String = "C:\Output.Csv"

Sub Sample()
    Dim ws As Worksheet
    Dim rng As Range
    Dim MyArray() As Long, MaxLength As Long
    Dim ff As Long, i As Long, lastRow As Long, LastCol As Long
    Dim strOutput As String

    On Error GoTo Whoa

    Application.ScreenUpdating = False

    '~~> Change this to the respective sheet
    Set ws = Sheets("Sheet1")
    LastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column

    '~~> Loop through each Column to get the max size of the field
    For i = 1 To LastCol
        MaxLength = getMaxLength(ws, i)
        ReDim Preserve MyArray(i)
        MyArray(i) = MaxLength
    Next i

    ff = FreeFile

    '~~> output file
    Open strOutputFile For Output As #ff

    '~~> Write to text file
    With ws
        lastRow = .Range("A" & Rows.Count).End(xlUp).Row

        For Each rng In .Range("A1:A" & lastRow)
            With rng
                For i = 1 To UBound(MyArray)
                    '~~> Insert a DELIMITER here if your text has spaces
                    strOutput = strOutput & " " & Left(.Offset(0, i-1).Text & _
                                String(MyArray(i), " "), MyArray(i))
                Next i

                Print #ff, Mid(Trim(strOutput), 1)
                strOutput = Empty
            End With
        Next rng
    End With

LetsContinue:
    On Error Resume Next
        Close #ff
    On Error GoTo 0
    Application.ScreenUpdating = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

'~~> Function to get the max size
Public Function getMaxLength(ws As Worksheet, Col As Long) As Long
    Dim lastRow As Long, j As Long

    getMaxLength = 0

    lastRow = ws.Range("A" & ws.Rows.Count).End(-4162).Row

    For j = 1 To lastRow
        If Len(Trim(ws.Cells(j, Col).Value)) > getMaxLength Then _
        getMaxLength = Len(Trim(ws.Cells(j, Col).Value))
    Next j
End Function

enter image description here

能否归途做我良人 2025-01-05 23:06:25

打开您的 Excel/csv/文本
执行您所需的操作,然后您可以使用文件格式另存为 xlTextPrinter

ActiveWorkbook.SaveAs Filename:="Your File Name.txt", FileFormat:=xlTextPrinter, CreateBackup:=False

不需要额外的代码来替换额外的引号

Open your excel/csv/text
Perform your your required action and then You can saveas using file format as xlTextPrinter

ActiveWorkbook.SaveAs Filename:="Your File Name.txt", FileFormat:=xlTextPrinter, CreateBackup:=False

No need for extra code to replace extra quotes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文