VBScript Excel到CSV转换器非常慢,而受保护视图的速度更慢

VBScript Excel到CSV转换器非常慢,而受保护视图的速度更慢,excel,vbscript,Excel,Vbscript,我找到了一个VBScript来将Excel文件转换为CSV文件,并根据我的需要对其进行了修改。我处理的文件通常处于受保护视图中,因此我有以下代码: format = 6 Set objFSO = CreateObject("Scripting.FileSystemObject") src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0)) dest_file = objFSO.GetAbsolutePathName(WSc

我找到了一个VBScript来将Excel文件转换为CSV文件,并根据我的需要对其进行了修改。我处理的文件通常处于受保护视图中,因此我有以下代码:

format = 6

Set objFSO = CreateObject("Scripting.FileSystemObject")

src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))

Dim oExcel
Set oExcel = CreateObject("Excel.Application")

oExcel.DisplayAlerts = False
oExcel.ProtectedViewWindows.Open(src_file)
If oExcel.ProtectedViewWindows.Count > 0 Then
    oExcel.ActiveProtectedViewWindow.Edit
End If
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)

oBook.Worksheets(5).Activate

oBook.SaveAs dest_file, format

oBook.Close False
oExcel.DisplayAlerts = True
oExcel.Quit 
此脚本从Excel文件中删除受保护的视图,并将当前选定的工作表另存为csv文件。如果我第一次运行它,将4个受保护的view excel文件转换为csv文件大约需要25-27秒。当我第二次运行它时,excel文件不再处于受保护的视图中,我得到了类似的结果。我很好奇,如果我删除了对受保护视图的检查,会发生什么情况,因此我做了以下操作:

format = 6

Set objFSO = CreateObject("Scripting.FileSystemObject")

src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))

Dim oExcel
Set oExcel = CreateObject("Excel.Application")

oExcel.DisplayAlerts = False

Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)

oBook.Worksheets(5).Activate

oBook.SaveAs dest_file, format

oExcel.AutomationSecurity = lSecurity

oBook.Close False
oExcel.DisplayAlerts = True
oExcel.Quit
现在,我无法使用此脚本打开受保护的excel文件,但如果excel文件未受保护,则转换它们的速度将比以前的脚本快得多。现在不在受保护视图中的相同4个excel文件平均需要14秒才能转换为csv文件。我已经运行了很多次,以确保我每次都得到相同的结果


为什么检查受保护的视图会导致脚本花费更长的时间?有没有办法让它跑得更快?另外,通过命令行将Excel文件转换为CSV还有其他选择吗?我还尝试了Powershell,它甚至比VBScript更快!但我无法让它对受保护的视图文件起作用。我应该提到,我正在Qt5.6中通过QProcess运行这些脚本。感谢您的帮助

在第一次打开文件时,您将打开文件2次!一个用于
oExcel.ProtectedViewsWindow
,一个用于
oExcel.Workbook

尝试以下方法(未经测试):


你是个救生员。它起作用了!现在转换4个excel文件大约需要17秒,这是一个很大的改进。然而,有没有可能使它更快呢?在我的GUI程序中,我根据excel文件的数量运行该脚本多次(在本例中,脚本运行了4次)。你认为如果我只运行一次脚本并迭代excel文件会更快吗?这样我就不必创建许多Excel.com应用程序。再次感谢你,我对VB了解不多。但我认为这是可能的,可能是由于
Excel.Application
Windows
属性。没问题。我设法弄明白了,它确实减少了大约4秒的时间。
format = 6

Set objFSO = CreateObject("Scripting.FileSystemObject")

src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))

Dim oExcel
Set oExcel = CreateObject("Excel.Application")

oExcel.DisplayAlerts = False
oExcel.ProtectedViewWindows.Open(src_file)
Dim oBook
If oExcel.ProtectedViewWindows.Count > 0 Then
    Set oBook = oExcel.ActiveProtectedViewWindow.Edit
Else
    Set oBook = oExcel.ActiveWorkbook
End If

oBook.Worksheets(5).Activate

oBook.SaveAs dest_file, format

oBook.Close False
oExcel.DisplayAlerts = True
oExcel.Quit