用于将.csv文件转换为.xlsx的脚本

时间:2015-10-11 11:50:47

标签: excel csv vbscript

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

2 个答案:

答案 0 :(得分:0)

使用Pyexcel https://pyexcel.readthedocs.org/en/latest/进行Python非常简单。

if ($skuIndex == -1) {

答案 1 :(得分:0)

如果您要转换的所有文件都在同一文件夹中,则可以使用此代码。作为选项,如果您不需要保存原始文件,请删除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