跳到主要內容

如何將工作表數據另存為帶/不帶雙引號的csv文件?

當您將一系列數據從Excel導出或保存到csv文件時,通常,csv數據不是用雙引號引起來的,但是,如果您的單元格值由逗號或換行符分隔,則導出的csv數據將是用雙引號引起來的引號。 在本文中,我將討論如何根據需要將工作表數據保存為帶或不帶雙引號的csv文件。

將工作表數據另存為帶雙引號的csv文件

將工作表數據另存為csv文件而不帶雙引號


箭頭藍色右氣泡 將工作表數據另存為帶雙引號的csv文件

要將普通數據另存為帶雙引號的csv文件,以下VBA代碼可能會為您提供幫助,請執行以下操作:

1。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

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 按鈕和一個 另存為 出現窗口,請指定文件名和位置,然後單擊 節省 按鈕,請參見屏幕截圖:

doc保存帶引號的csv

5。 和a Excel的Kutools的提示框會彈出,提醒您新的csv文件已保存到您的指定位置,請參見屏幕截圖:

doc保存帶引號的csv

6。 點擊 OK 關閉對話框,現在,當您打開新的csv文件時,所有數據都用雙引號括起來,如以下屏幕截圖所示:

doc保存帶引號的csv


箭頭藍色右氣泡 將工作表數據另存為csv文件而不帶雙引號

如果數據用逗號分隔,則在單元格中換行,將數據另存為csv文件時,數據將被雙引號括起來,如下所示:

doc保存帶引號的csv

要將數據另存為csv文件而不帶雙引號,請應用以下VBA代碼。

1。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

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 按鈕和一個 另存為 顯示窗口,請為新的csv文件指定文件名和位置,請參見屏幕截圖:

doc保存帶引號的csv

5。 然後點擊 節省 按鈕,一個 Excel的Kutools的提示框會彈出,告訴您新的csv文件已保存到您的指定位置,請參見屏幕截圖:

doc保存帶引號的csv

6. 點擊 OK 關閉對話框,當您打開新的csv文件時,數據周圍的雙引號將被刪除,如以下屏幕截圖所示:

doc保存帶引號的csv

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為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