繁体   English   中英

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

[英]Script to convert .csv file to .xlsx

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

这对于 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")

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM