Note: The other languages of the website are Google-translated. Back to English
登陆  \/ 
x
or
x
注册  \/ 
x

or

如何在Excel中将多个Excel文件批量转换为CSV文件?

在Excel中,我们可以使用“另存为”功能将工作簿转换为CSV文件,但是您知道如何将多个Excel文件批量转换为单独的CSV文件吗? 在本文中,我介绍了一个VBA代码,用于将文件夹中的所有Excel文件批量转换为Excel中的CSV文件。

使用VBA将文件夹的Excel文件批量转换为CSV文件

使用Kutools for Excel将工作簿表转换为单独的CSV文件好主意3


使用VBA将文件夹的Excel文件批量转换为CSV文件

在Excel中,除了VBA,没有内置函数可以快速解决此问题。

1.启用Excel,然后按 Alt + F11键 钥匙打开 Microsoft Visual Basic应用程序 窗口。

2。 点击 插页 > 模块 创建一个新的模块。

3.复制以下代码并将其粘贴到新的“模块”窗口中。

VBA:将Excel文件批量转换为CSV

Sub WorkbooksSaveAsCsvToFolder()
'UpdatebyExtendoffice20181031
Dim xObjWB As Workbook
Dim xObjWS As Worksheet
Dim xStrEFPath As String
Dim xStrEFFile As String
Dim xObjFD As FileDialog
Dim xObjSFD As FileDialog
Dim xStrSPath As String
Dim xStrCSVFName As String

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    On Error Resume Next
Set xObjFD = Application.FileDialog(msoFileDialogFolderPicker)
    xObjFD.AllowMultiSelect = False
    xObjFD.Title = "Kutools for Excel - Select a folder which contains Excel files"
    If xObjFD.Show <> -1 Then Exit Sub
    xStrEFPath = xObjFD.SelectedItems(1) & "\"

    Set xObjSFD = Application.FileDialog(msoFileDialogFolderPicker)

    xObjSFD.AllowMultiSelect = False
    xObjSFD.Title = "Kutools for Excel - Select a folder to locate CSV files"
    If xObjSFD.Show <> -1 Then Exit Sub
    xStrSPath = xObjSFD.SelectedItems(1) & "\"

    xStrEFFile = Dir(xStrEFPath & "*.xls*")

    Do While xStrEFFile <> ""
        Set xObjWB = Workbooks.Open(Filename:=xStrEFPath & xStrEFFile)
        xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".") - 1) & ".csv"
        xObjWB.SaveAs Filename:=xStrCSVFName, FileFormat:=xlCSV
        xObjWB.Close savechanges:=False
        xStrEFFile = Dir
  Loop
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

4。 按 F5 键,在第一个弹出对话框中选择包含要转换为CSV文件的Excel文件的文件夹。
doc批处理到csv 1

5。 点击 OK,然后在第二个弹出对话框中,选择放置CSV文件的文件夹。
doc批处理到csv 2

6。 点击 OK,现在文件夹中的Excel文件已转换为CSV文件并保存在另一个文件夹中。
doc批处理到csv 3


使用Kutools for Excel将工作簿表转换为单独的CSV文件

众所周知,我们只能使用Excel的另存为功能将整个工作簿转换为一个CSV文件。 但是有时候,您希望将单个工作表转换为CSV文件,在这种情况下, 拆分工作簿 实用程序 Kutools for Excel 可以帮你。

Kutools for Excel, 与超过 300 方便的功能,使您的工作更加轻松。 

安装后 Kutools for Excel,请执行以下操作:(立即免费下载Kutools for Excel!)

1.启用要将其工作表转换为单独的CSV文件的工作簿,单击 Kutools Plus > 工作簿 > 拆分工作簿.
doc批处理到csv 4

2。 在里面 拆分工作簿 对话框中,检查要拆分的工作表名称(默认情况下选中所有工作表),然后选中 除上文所 类型,选择 CSV(Macintosh)(* .CSV) 从下拉列表中选择。
doc批处理到csv 5

3。 点击 分裂 弹出 浏览文件夹 对话框中,选择或创建一个文件夹以放置CSV文件。
doc批处理到csv 6

4。 点击 OK,现在工作簿已拆分为单独的CSV文件。
doc批处理到csv 7


最佳办公效率工具

Kutools for Excel解决了您的大多数问题,并使您的生产率提高了80%

  • 重用: 快速插入 复杂的公式,图表 以及您以前使用过的任何东西; 加密单元 带密码 创建邮件列表 并发送电子邮件...
  • 超级公式栏 (轻松编辑多行文本和公式); 阅读版式 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不会丢失数据; 拆分单元格内容; 合并重复的行/列...防止细胞重复; 比较范围...
  • 选择重复或唯一 行; 选择空白行 (所有单元格都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择...
  • 确切的副本 多个单元格,无需更改公式参考; 自动创建参考 到多张纸; 插入项目符号,复选框等...
  • 提取文字,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级滤镜 (将过滤方案保存并应用于其他工作表); 高级排序 按月/周/日,频率及更多; 特殊过滤器 用粗体,斜体...
  • 结合工作簿和工作表; 根据关键列合并表; 将数据分割成多个工作表; 批量转换xls,xlsx和PDF...
  • 超过300种强大功能。 支持Office / Excel 2007-2019和365。支持所有语言。 在您的企业或组织中轻松部署。 完整功能30天免费试用。 60天退款保证。
kte选项卡201905

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

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
officetab底部
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    TBenson85 · 2 months ago
    Hello, is there a quick change to the code that would allow me to change from a CSV UTF-8 (Comma delimited) to just CSV (Comma delimited)? I tried the first method and was hopeful but it seems like it won't change them since they are already in some form of CSV.  Maybe there is an easier process but I can't find anything.  I have to convert maybe 150 files that were saved in this format and I don't want to open every file and Save As if I can avoid it.  Any help is appreciated!
    • To post as a guest, your comment is unpublished.
      TBenson85 · 2 months ago
      So, after reviewing the code a little closer, I saw where the initial file type had to be .xls.  So replacing with .csv has solved the issue.  
  • To post as a guest, your comment is unpublished.
    Jared · 3 months ago
    This is amazing. Thank-you!
  • To post as a guest, your comment is unpublished.
    wilfried · 5 months ago
    top thanks :) 
  • To post as a guest, your comment is unpublished.
    Sen · 1 years ago
    I think it is worth adding better error handling for files with special characters, currently they are simply ignored.
  • To post as a guest, your comment is unpublished.
    Justin · 1 years ago
    Thank you for sharing. I'm trying to save out multiple xls files which contain a unique value, producing a prompt asking yes or no before saving. The prompt reads..

    "Some features in your workbook might be lost if you save it as a CSV (Comma delimited). Do you want to keep using that format?"

    Would someone know where to add the code to answer yes to this prompt?
  • To post as a guest, your comment is unpublished.
    Sam · 2 years ago
    Another small remark:

    If the cells in the original Excel files are all formatted as "General", some accuracy is lost when the file is saved as a CSV
    For example, if a cell value in Excel is 0.123456789123456, then the value in the CSV will be 0.123456789 (missing the remaining decimals), as long as the cell was formatted as 'General'. This can be solved by formatting all cells in the Excel file to anything else than 'General' (for example, 'Text'). In that case, the CSV *will* still have the full detail/accuracy. I.e. the values in the Excel files will be fully intact after saving as a CSV.

    How could this macro be changes, so it sets the formatting of all cells in the Excel file to 'Text', before saving as a CSV?
    I imagine that it must somehow make use of the following, but I can't figure out how to correctly include in within the macro:

    Cells.Select
    Selection.NumberFormat = "@"
  • To post as a guest, your comment is unpublished.
    Sam · 2 years ago
    Works great, thanks for the code!
    My only remark would be that this code cuts of file names when there is a "." in the filename itself (e.g. file.123.csv turns into file.csv).
    • To post as a guest, your comment is unpublished.
      Carol · 1 years ago
      Have you found a way around this issue?
      • To post as a guest, your comment is unpublished.
        Clara · 1 years ago
        Carol,

        On line 33 I've replaced this code:

        xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".") - 1) & ".csv"

        With this code:

        xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".xlsx") - 1) & ".csv"

        Note that if you're using some other excel extension (.xls, .xlsm, etc.) you should change it as such :)
        • To post as a guest, your comment is unpublished.
          Sonya · 1 years ago
          Thank you so much! This has saved me so much time!!
  • To post as a guest, your comment is unpublished.
    Aman · 2 years ago
    Ty it really works dear !!