提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

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

通过应用“另存为”功能,将CSV文件转换为XLS或XLSX文件非常容易。 但是,要将多个CSV文件从文件夹转换为XLS或XLSX文件非常耗时,需要手动逐个保存。 这里我介绍一个宏代码来快速批量转换所有CSV文件到文件夹中的XLS(x)文件。

使用宏代码将CSV文件批量转换为XLS(X)文件

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel - 最佳办公生产力工具将解决您的大部分Excel问题
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在公司轻松部署; 全功能60天免费试用。

使用宏代码将CSV文件批量转换为XLS(X)文件


要将多个CSV文件从一个文件夹转换为XLS(X)文件,可以按照以下步骤进行操作:

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

1。 启用新的工作簿,按 Alt + F11键 键打开 Microsoft Visual Basic for Applications 窗口,然后单击 插页 > 模块。 看截图:
doc批量转换cvs xls 1

注意:确保您要转换的所有CSV文件都已关闭。

2。 然后将下面的宏代码粘贴到 模块 脚本和新闻 F5 键来运行代码。

VBA:将CSV转换为XLS

Sub CSVtoXLS()
'UpdatebyExtendoffice20170814
    Dim xFd As FileDialog
    Dim xSPath As String
    Dim xCSVFile As String
    Dim xWsheet As String
    Application.DisplayAlerts = False
    Application.StatusBar = True
    xWsheet = ActiveWorkbook.Name
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Select a folder:"
    If xFd.Show = -1 Then
        xSPath = xFd.SelectedItems(1)
    Else
        Exit Sub
    End If
    If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\"
    xCSVFile = Dir(xSPath & "*.csv")
    Do While xCSVFile <> ""
        Application.StatusBar = "Converting: " & xCSVFile
        Workbooks.Open Filename:=xSPath & xCSVFile
        ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xls", vbTextCompare), xlNormal
        ActiveWorkbook.Close
        Windows(xWsheet).Activate
        xCSVFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
End Sub

3。 在弹出对话框中,选择包含要转换的CSV文件的指定文件夹。 看截图:
doc批量转换cvs xls 2

4。 点击 OK,所选文件夹中的所有CSV文件都被转换为XLS文件。
doc批量转换cvs xls 3

小技巧:如果要将CSV文件转换为XLSX文件,请使用下面的VBA代码。

VBA:将CSV文件转换为XLSX

Sub CSVtoXLS()
'UpdatebyExtendoffice20170814
    Dim xFd As FileDialog
    Dim xSPath As String
    Dim xCSVFile As String
    Dim xWsheet As String
    Application.DisplayAlerts = False
    Application.StatusBar = True
    xWsheet = ActiveWorkbook.Name
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Select a folder:"
    If xFd.Show = -1 Then
        xSPath = xFd.SelectedItems(1)
    Else
        Exit Sub
    End If
    If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\"
    xCSVFile = Dir(xSPath & "*.csv")
    Do While xCSVFile <> ""
        Application.StatusBar = "Converting: " & xCSVFile
        Workbooks.Open Filename:=xSPath & xCSVFile
        ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xls", vbTextCompare), xlWorkbookDefault
        ActiveWorkbook.Close
        Windows(xWsheet).Activate
        xCSVFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
End Sub


好导出范围到文件

Kutools for Excel's 导出范围到文件功能 可以导出或保存一个范围来分隔文件
工作簿,pdf,文本,csv或文本。
点击 企业服务 > 进出口 > 导出范围到文件.
doc将单元格范围导出到文件

相关文章:


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

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

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
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.
    viper · 1 months ago
    it changes my date column format from dd-mm-yyyy to dd/mm/yyyy, please can anyone help with how to prevent this script from changing column formats.
    Thanks a lot.
  • To post as a guest, your comment is unpublished.
    lisa6538 · 3 months ago
    Thanks for providing the code. I modified the code to do the XLSX to CSV batch conversion. The code is provided below.


    Sub XLSXtoCSV()
    'UpdatebyExtendoffice20170814
    Dim xFd As FileDialog
    Dim xSPath As String
    Dim xCSVFile As String
    Dim xWsheet As String
    Application.DisplayAlerts = False
    Application.StatusBar = True
    xWsheet = ActiveWorkbook.Name
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Select a folder:"
    If xFd.Show = -1 Then
    xSPath = xFd.SelectedItems(1)
    Else
    Exit Sub
    End If
    If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\"
    xXLSXFile = Dir(xSPath & "*.xlsx")
    Do While xXLSXFile <> ""
    Application.StatusBar = "Converting: " & xXLSXFile
    Workbooks.Open Filename:=xSPath & xXLSXFile
    ActiveWorkbook.SaveAs Replace(xSPath & xXLSXFile, ".xlsx", ".csv", vbTextCompare), xlCSV
    ActiveWorkbook.Close
    Windows(xWsheet).Activate
    xXLSXFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
    End Sub
    • To post as a guest, your comment is unpublished.
      Diego Jaime · 19 days ago
      Thank you for the modification, that´s what I was looking for!
  • To post as a guest, your comment is unpublished.
    Sam · 3 months ago
    One small issue with this code is that some accuracy (number of decimals) can be lost when opening a csv and saving it as xlsx.
    The way to prevent this is to select all cells right after opening, set the number of decimal places to the desired amount (15 for me personally), and *then* saving as xlsx

    Otherwise, the xlsx has less detail (less accuracy in terms of decimals) than the original csv, which can lead to problems in some applications
  • To post as a guest, your comment is unpublished.
    Rose · 3 months ago
    this macro does not show my csv file
  • To post as a guest, your comment is unpublished.
    stb · 4 months ago
    Cool bit of code. Very useful. If I'm not mistaken the code for .xlsx looks the same as the .xls code.

    This line:
    ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xls", vbTextCompare), xlWorkbookDefault

    I assume it should be: ".csv" , ".xlsx", vbTextCompare etc.
    • To post as a guest, your comment is unpublished.
      iBrezel · 4 months ago
      I've changed the code so, described above; now the script works as expected: it generates xlsx files
      • To post as a guest, your comment is unpublished.
        thy · 2 months ago
        no you haven't. it still says xls in both versions.
  • To post as a guest, your comment is unpublished.
    pramod koliar · 6 months ago
    Great Script after searching all other script available in google. Can anybody help how to modify this script by specifying a folder location in local drive instead of allowing it to open the dialog box as i am actually automating the system. Thanks in advance
  • To post as a guest, your comment is unpublished.
    Pramod Koliar · 6 months ago
    Hi, great script which worked for me after trying all other script available in google. I have added few more lines which performs the text to columns operation which i want. Can anybody help me how to modify the script by specifying the folder location in local drive instead of opening the dialog box as i am actually automating the system
    . Thanks in advance
  • To post as a guest, your comment is unpublished.
    HC · 8 months ago
    Hello,

    a great article - thank you! Almost works for me.

    Here's the problem: I have a pipe-delimited csv file. I have changed the default windows delimiter (via control panel/regional settings) to "|". So, when I open the csv file with Excel, it opens, directly reads and parses the fields to columns correctly. All I have to do is to save as xls or xlsx, done. When I look at your code, that should be what the code does: It opens the csv files in a folder, saves them as xls or xlsx, and loops over that.

    Here is the problem: When I open those Excel files, the pipe delimiters are still there, nothing is parsed to the columns. This was the behavior as before my change to the pipe delimited default setting in the Windows settings. So it seems like Excel doesn't use those Windows settings. I verified this by replacing the pipe delimiters with comma delimiters in a copy of the file - of the two files, the pipe-delimited remained unparsed, the comma delimited got converted just fine.

    Any idea where that Excel-inbuilt comma delimitation can be found or changed, or whether there is a way in the code to do that? I would prefer NOT to have to do a search and replace, then save...

    Thanks, HC
    • To post as a guest, your comment is unpublished.
      Sunny · 7 months ago
      If you want to split the data into column based on the pipe delimiter in Excel, just use Text to Columns function to split data by / after coverting the csv files to xls.
  • To post as a guest, your comment is unpublished.
    Fred Maye · 11 months ago
    WHen I gave the folder name, the program said, "No files match your search." But there are 2,609 .csv files in the folder
    • To post as a guest, your comment is unpublished.
      Sunny · 10 months ago
      The code not support the separated comma csv file, are your files in this case?
  • To post as a guest, your comment is unpublished.
    ptee · 1 years ago
    I used the file multiple csv to multiple xls but get wrong xls de csv files have ; seperated info but become together in cells. How can this been solved?
    • To post as a guest, your comment is unpublished.
      Sunny · 1 years ago
      The code cannot support the separated comma csv file.
  • To post as a guest, your comment is unpublished.
    Dragos · 1 years ago
    Hi,

    Your script is great but it doesn't convert the csv separated by semicolons. Can you please post a solution for those?

    Thank you
    • To post as a guest, your comment is unpublished.
      Younes · 1 years ago
      i might be late i just got the issue where i needed to convert the semicolons CSV files and i added this to the code and it worked hope this will help someone

      Change 'Workbooks.Open Filename:=xSPath & xCSVFile' By this 'Workbooks.Open Filename:=xSPath & xCSVFile, Delimiter:=";", Local:=True'
    • To post as a guest, your comment is unpublished.
      Sunny · 1 years ago
      Sorry,here I do not have any solution, maybe you can place your question to our forum, someone else may help you. https://www.extendoffice.com/forum.html
  • To post as a guest, your comment is unpublished.
    Sathish · 1 years ago
    Thankyou so much for your excellent script for CSV to XLS. Its much more useful to my work
  • To post as a guest, your comment is unpublished.
    Matt Leonard · 1 years ago
    "Tip: If you want to convert CSV files to XLXS files, you just need to change .xls to .xlsx in the macro ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xls", vbTextCompare)."

    I tried this, and Excel could not open the resulting files. I got the following message: "Excel cannot open the file 'filename.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

    However, the script was successful for converting to .xls.
    • To post as a guest, your comment is unpublished.
      Sunny · 1 years ago
      I did not find that problem before, thank you for your correct comment. I have corrected the tip.
  • To post as a guest, your comment is unpublished.
    Karen · 1 years ago
    I want to do the opposite - convert XLS to CSV. does this script work?
    Sub CSVtoXLS()
    'UpdatebyExtendoffice20170814
    Dim xFd As FileDialog
    Dim xSPath As String
    Dim xXLSFile As String
    Dim xWsheet As String
    Application.DisplayAlerts = False
    Application.StatusBar = True
    xWsheet = ActiveWorkbook.Name
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Select a folder:"
    If xFd.Show = -1 Then
    xSPath = xFd.SelectedItems(1)
    Else
    Exit Sub
    End If
    If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\"
    xXLSFile = Dir(xSPath & "*.xls")
    Do While xXLSFile <> ""
    Application.StatusBar = "Converting: " & xXLSFile
    Workbooks.Open Filename:=xSPath & xXLSFile
    ActiveWorkbook.SaveAs Replace(xSPath & xXLSFile, ".xls", ".csv", vbTextCompare), xlNormal
    ActiveWorkbook.Close
    Windows(xWsheet).Activate
    xXLSFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
    End Sub