跳到主要内容

如何将工作表数据另存为带/不带双引号的csv文件?

当您将一系列数据从Excel导出或保存到csv文件时,通常,csv数据不是用双引号引起来的,但是,如果您的单元格值由逗号或换行符分隔,则导出的csv数据将是用双引号引起来的引号。 在本文中,我将讨论如何根据需要将工作表数据保存为带或不带双引号的csv文件。

将工作表数据另存为带双引号的csv文件

将工作表数据另存为csv文件而不带双引号


箭头蓝色右气泡 将工作表数据另存为带双引号的csv文件

要将常规数据另存为带双引号的csv文件,以下VBA代码可能会对您有所帮助,请执行以下操作:

1。 按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

VBA代码:用双引号将工作表数据另存为csv文件:

Sub CSVFile()
'updateby Extendoffice
    Dim xRg As Range
    Dim xRow As Range
    Dim xCell As Range
    Dim xStr As String
    Dim xSep As String
    Dim xTxt As String
    Dim xName As Variant
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
    Set xRg = Application.InputBox("Please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    xName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
    xSep = Application.International(xlListSeparator)
    Open xName For Output As #1
    For Each xRow In xRg.Rows
        xStr = ""
        For Each xCell In xRow.Cells
            xStr = xStr & """" & xCell.Value & """" & xSep
        Next
        While Right(xStr, 1) = xSep
            xStr = Left(xStr, Len(xStr) - 1)
        Wend
        Print #1, xStr
    Next
    Close #1
    If Err = 0 Then MsgBox "The file has saved to: " & xName, vbInformation, "Kutools for Excel"
End Sub

3。 然后按 F5 键以运行此代码,并弹出提示框,提醒您选择要另存为csv文件的数据范围,并用双引号引起来,请参见屏幕截图:

doc保存带引号的csv

4。 然后点击 OK 按钮和a 另存为 出现窗口,请指定文件名和位置,然后单击 保存 按钮,请参见屏幕截图:

doc保存带引号的csv

5。 和a Kutools for Excel的提示框会弹出,提醒您新的csv文件已保存到您的指定位置,请参见屏幕截图:

doc保存带引号的csv

6。 点击 OK 关闭对话框,现在,当您打开新的csv文件时,所有数据都用双引号引起来,如以下屏幕截图所示:

doc保存带引号的csv


箭头蓝色右气泡 将工作表数据另存为csv文件而不带双引号

如果您的数据用逗号分隔,则在单元格中换行,将数据另存为csv文件时,数据将被双引号括起来,如下所示:

doc保存带引号的csv

要将数据另存为csv文件而不带双引号,请应用以下VBA代码。

1。 按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

VBA代码:将工作表数据另存为csv文件,不带双引号:

Sub Export()
'updateby Extendoffice
    Dim xRg As Range
    Dim xRow As Range
    Dim xCell As Range
    Dim xStr As String
    Dim xTxt As String
    Dim xName As Variant
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
    Set xRg = Application.InputBox("Please select data range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    xName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
    Open xName For Output As #1
    For Each xRow In xRg.Rows
        xStr = ""
        For Each xCell In xRow.Cells
            xStr = xStr & xCell.Value & Chr(9)
        Next
        While Right(xStr, 1) = Chr(9)
            xStr = Left(xStr, Len(xStr) - 1)
        Wend
        Print #1, xStr
    Next
    Close #1
    If Err = 0 Then MsgBox "The file has saved to: " & xName, vbInformation, "Kutools for Excel"
End Sub

3。 然后按 F5 键运行此代码,并弹出一个提示框,提醒您选择要另存为csv且不带双引号的数据范围,请参见屏幕截图:

doc保存带引号的csv

4。 点击 OK 按钮和a 另存为 显示窗口,请为您的新csv文件指定文件名和位置,请参见屏幕截图:

doc保存带引号的csv

5。 然后点击 保存 按钮,一个 Kutools for Excel的提示框会弹出,告诉您新的csv文件已保存到您的指定位置,请参见屏幕截图:

doc保存带引号的csv

6. 点击 OK 关闭对话框,然后在打开新的csv文件时,数据周围的双引号将被删除,如以下屏幕截图所示:

doc保存带引号的csv

最佳办公生产力工具

热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

kte选项卡201905


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (7)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Super simple and straight forward! Saved the day for me. Much appreciated.
Rated 5 out of 5
This comment was minimized by the moderator on the site
Caso deseje apenas salva todo o Excel em csv *sem aspas* conforme eu, segue sugestão:

Sub Exportar_CSV()

Application.ScreenUpdating = False

'Seleciona a guia Extract do Excel depois a célula A1
Sheets("Extract").Select
Range("A1").Select

'Iniciar exportação txt via Print com texto exato mostrado em tela sem erro das aspas

Dim Path As String
Dim FileNumber As Integer
Dim LR As Integer
Dim LC As Integer

Dim k As Integer
Dim i As Integer

LR = Worksheets("Extract").Cells(Rows.Count, 1).End(xlUp).Row
LC = Worksheets("Extract").Cells(1, Columns.Count).End(xlToLeft).Column

Path = ThisWorkbook.Path & "\Extract " & Format(Now(), "ddmmyyyy-hhmmss") & ".csv"
FileNumber = FreeFile

Open Path For Output As FileNumber

For k = 1 To LR

For i = 1 To LC

If i <> LC Then
Print #FileNumber, Cells(k, i),
Else
Print #FileNumber, Cells(k, i)
End If

Next i

Next k

Close FileNumber


' Caso deseja abrir o notepad imediatamente para conferir o txt gerado só retirar a linha comentada abaixo:


'Shell "notepad.exe " & Path, vbNormalFocus



Application.ScreenUpdating = True

MsgBox "Extract*.txt salvo na pasta onde abriu este Excel!"

End Sub
This comment was minimized by the moderator on the site
Sub Exportar_TXT()



Application.ScreenUpdating = False



'Seleciona a guia Extract do Excel depois a célula A1
Sheets("Extract").Select
Range("A1").Select

'Iniciar exportação txt via Print com texto exato mostrado em tela sem erro das aspas

Dim Path As String
Dim FileNumber As Integer
Dim LR As Integer
Dim LC As Integer

Dim k As Integer
Dim i As Integer

LR = Worksheets("Extract").Cells(Rows.Count, 1).End(xlUp).Row
LC = Worksheets("Extract").Cells(1, Columns.Count).End(xlToLeft).Column

Path = ThisWorkbook.Path & "\Extract " & Format(Now(), "ddmmyyyy-hhmmss") & ".csv"
FileNumber = FreeFile

Open Path For Output As FileNumber

For k = 1 To LR

For i = 1 To LC

If i <> LC Then
Print #FileNumber, Cells(k, i),
Else
Print #FileNumber, Cells(k, i)
End If

Next i

Next k

Close FileNumber


' Caso deseja abrir o notepad imediatamente para conferir o txt gerado só retirar a linha comentada abaixo:


'Shell "notepad.exe " & Path, vbNormalFocus



Application.ScreenUpdating = True

MsgBox "Extract*.txt salvo na pasta onde abriu este Excel!"

End Sub
This comment was minimized by the moderator on the site
Thank you so much for this. I've been trying to figure out how to put double quotes around my file for 3 days!
This comment was minimized by the moderator on the site
not unicode
This comment was minimized by the moderator on the site
Sério eu te amo rsrsrsr
Me ajudou demais, muito obrigada!
This comment was minimized by the moderator on the site
Olá!
Muito obrigado! O conteúdo me ajudou muito. Precisava gerar um arquivo txt eliminando as aspas duplas no início e no final de cada linha e não estava conseguindo resolver. Agora consegui.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations