【问题标题】:Script to convert .csv file to .xlsx将 .csv 文件转换为 .xlsx 的脚本
【发布时间】:2015-10-11 11:50:46
【问题描述】:

我想将 .csv 文件转换为 .xls。这些文件是由另一个没有 .csv 后缀的应用程序创建的(例如ex.1)。我有 C++ 和 C# 的背景。 有很多 VBScript 示例,但我不知道如何开始,在哪里编写脚本,如何运行它。我会很感激详细的回答。

【问题讨论】:

  • Google vba 初学者,你会发现所描述的一切——如何在 excel 中启动 Visual Basic 并运行代码。喜欢这个excel-vba.com/excel-vba-solutions-beginners.htm
  • any CSV 文件一个“.csv”文件扩展名,Excel 会打开它。然后执行“另存为”并更改为 .XLSX 扩展名...如果您需要对许多文件重复此操作,请记录每个步骤的一些宏(即,将文件作为一个步骤打开,另存为另一个步骤)。我们可以帮助您将它们组合在一起,但 SO 不是代码编写服务,您应该付出比“告诉我从哪里开始”(Google...)更多的努力

标签: excel csv vbscript


【解决方案1】:

这对于 Python 的 Pyexcel https://pyexcel.readthedocs.org/en/latest/ 非常简单。

import pyexcel.cookbook import merge_all_to_a_book
import pyexcel.ext.xlsx
import glob

merge_all_to_a_book(glob.glob("test.csv"), "excelformat.xlsx")

【讨论】:

    【解决方案2】:

    如果您要转换的所有文件都在同一个文件夹中,您可以使用此代码。作为一个选项,如果您不需要保存原始文件,请删除 oFSO.CopyFile 行并取消注释掉 oFSO.MoveFile 行。如果其他文件位于同一文件夹中,则可能需要进行额外检查。

    Option Explicit                                                         'Require all vars to be declared
    Dim oXLApp, sCSVFiles, oFSO, oFiles                                     'Vars required
    Dim oFile, sCSVName, sDestinName, oWorkBook                             'Vars required
    Set oXLApp = CreateObject("Excel.Application")                          'Create excel object
    oXLApp.Visible = True                                                   'Make excel visible
    oXLApp.DisplayAlerts = False                                            'Tell excel to not alert when saving files
    sCSVFiles = "E:\Sample\"                                                'Folder where files to convert are
    Set oFSO = CreateObject("Scripting.FileSystemObject")                   'Create a File System Object
    Set oFiles = oFSO.GetFolder(sCSVFiles).Files                            'Get list of files in the folder
    For Each oFile In oFiles                                                'Look at each file in the folder
        sCSVName = oFile.Path & ".csv"                                      'Create the csv file name
        sDestinName = oFile.Path & ".xlsx"                                  'Create the excel file name
        If oFSO.FileExists(sCSVName) Then oFSO.DeleteFile(sCSVName)         'If csv name exists delete it
        If oFSO.FileExists(sDestinName) Then oFSO.DeleteFile(sDestinName)   'If excel name exists delete it
        oFSO.CopyFile oFile.Path, sCSVName                                  'Copy the file into CSV file ext
        'oFSO.MoveFile oFile.Path, sCSVName                                 'Rename the existing file
        Set oWorkBook = oXLApp.Workbooks.Open(sCSVName)                     'Open the CSV file
        oWorkbook.SaveAs sDestinName                                        'Save it as the default excel file type
        oXLApp.Workbooks.Close                                              'Close the workbook
    Next                                                                    'Process next file in the folder
    oXLApp.DisplayAlerts = True                                             'Excel bug Fix: turn back on alerts
    oXLApp.Quit                                                             'Exit excel
    Set oFiles = Nothing                                                    'Destroy the oFile object
    Set oFSO = Nothing                                                      'Destroy the FSO object
    Set oXLApp = Nothing                                                    'Destroy the excel object
    

    【讨论】:

      猜你喜欢
      • 2016-12-25
      • 2015-07-04
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 2018-07-22
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多