vbs-实现模拟打开excel和强制计算和保存

发布于:2025-07-31 ⋅ 阅读:(15) ⋅ 点赞:(0)

原因是excel里面包含了许多的计算公式,而且是跨sheet的,java代码不好处理

' Excel Batch Processor - Double-Click Compatible
' 使用脚本所在目录作为目标路径
targetFolder = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\") - 1)

' Check if running with WScript (GUI mode)
If InStr(1, WScript.FullName, "wscript.exe", vbTextCompare) > 0 Then
    ' Relaunch with CScript
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run "cmd /c cscript.exe //nologo """ & WScript.ScriptFullName & """ & pause", 1, True
    WScript.Quit
End If

Set fso = CreateObject("Scripting.FileSystemObject")

' Verify target folder exists
If Not fso.FolderExists(targetFolder) Then
    WScript.Echo "ERROR: Target folder not found: " & targetFolder
    WScript.Quit 1
End If

Set folder = fso.GetFolder(targetFolder)

' --- START PROCESSING ---
startTime = Timer
WScript.Echo "================================================================="
WScript.Echo " STARTING EXCEL FILE PROCESSING"
WScript.Echo " Directory: " & targetFolder
WScript.Echo " Start Time: " & Now()
WScript.Echo "================================================================="

On Error Resume Next
Set excel = CreateObject("Excel.Application")
If Err.Number <> 0 Then
    WScript.Echo "ERROR: Excel not installed or access denied"
    WScript.Quit 2
End If

excel.Visible = False
excel.DisplayAlerts = False
excel.AskToUpdateLinks = False
excel.EnableEvents = False
excel.Calculation = -4105  ' xlCalculationAutomatic

fileCount = 0
successCount = 0
errorCount = 0
skippedCount = 0  ' 新增跳过计数

' 获取Excel文件列表
Dim excelFiles
Set excelFiles = CreateObject("System.Collections.ArrayList")

For Each file In folder.Files
    ext = LCase(fso.GetExtensionName(file.Path))
    If ext = "xlsx" Or ext = "xlsm" Or ext = "xls" Then
        fileCount = fileCount + 1
        excelFiles.Add file.Path
    End If
Next

If fileCount = 0 Then
    WScript.Echo "No Excel files found in target directory"
    excel.Quit
    WScript.Quit 0
End If

WScript.Echo "Found " & fileCount & " Excel files to process"
WScript.Echo String(60, "-")

' Process files
current = 0
For Each filePath In excelFiles
    current = current + 1
    fileName = fso.GetFileName(filePath)
    WScript.Echo "PROCESSING [" & current & "/" & fileCount & "]: " & fileName
    
    ' 跳过已打开的Excel文件
    On Error Resume Next
    Set workbook = excel.Workbooks.Open(filePath, 0, False)
    
    If Err.Number = 0 Then
        ' 跳过只读文件
        If workbook.ReadOnly Then
            WScript.Echo "  SKIPPED: File is read-only"
            workbook.Close False
            skippedCount = skippedCount + 1
        Else
            workbook.ForceFullCalculation = True
            excel.CalculateFullRebuild
            
            workbook.Save
            workbook.Close False
            successCount = successCount + 1
            WScript.Echo "  STATUS: SUCCESS"
        End If
        Set workbook = Nothing
    Else
        errorMsg = "  ERROR: " & Err.Description
        Select Case Err.Number
            Case -2146827284: errorMsg = "  ERROR: File not found or access denied"
            Case 1004: errorMsg = "  ERROR: File is already open or locked"
        End Select
        WScript.Echo errorMsg
        errorCount = errorCount + 1
        Err.Clear
    End If
    
    WScript.Echo String(60, "-")
Next

' Cleanup
excel.Quit
Set excel = Nothing

' --- END PROCESSING ---
endTime = Timer
processingTime = Round(endTime - startTime, 2)

WScript.Echo "================================================================="
WScript.Echo " PROCESSING COMPLETE"
WScript.Echo " Finish Time: " & Now()
WScript.Echo " Processing Duration: " & processingTime & " seconds"
WScript.Echo "-----------------------------------------------------------------"
WScript.Echo " Excel files found: " & fileCount
WScript.Echo " Successfully processed: " & successCount
WScript.Echo " Failed: " & errorCount
WScript.Echo " Skipped (read-only): " & skippedCount
WScript.Echo " Success Rate: " & Round((successCount/(fileCount - skippedCount))*100, 2) & "%" & _
              " (excluding skipped)"
WScript.Echo "================================================================="

' ===== 新增:创建成功标记文件 =====
successFilePath = targetFolder & "\success.txt"
Set successFile = fso.CreateTextFile(successFilePath, True)  ' True表示覆盖现有文件
successFile.WriteLine "Excel Batch Processing Completed Successfully"
successFile.WriteLine "Completion Time: " & Now()
successFile.WriteLine "Total Files Processed: " & fileCount
successFile.WriteLine "Success Count: " & successCount
successFile.WriteLine "Error Count: " & errorCount
successFile.WriteLine "Skipped Count: " & skippedCount
successFile.Close
Set successFile = Nothing

WScript.Echo "Success marker file created: " & successFilePath
WScript.Echo "Processing complete. Excel processes have exited."
WScript.Echo "Files are unlocked and ready for other applications."
WScript.Echo "Press any key to close this window..."
Set fso = Nothing
WScript.StdIn.Read(1)


网站公告

今日签到

点亮在社区的每一天
去签到