当前位置: 首页 > news >正文

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

原因是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)

http://www.dtcms.com/a/297761.html

相关文章:

  • 7月25日总结
  • Android Kotlin 协程全面指南
  • Thinkphp8 Redis队列与消息队列Queue
  • C#模拟pacs系统接收并解析影像设备数据(DICOM文件解析)
  • Pattern正则表达式知识点
  • 第二十天(正则表达式与功能实际运用)
  • VUE 学习笔记6 vue数据监测原理
  • 设计模式十:单件模式 (Singleton Pattern)
  • 空间信息与数字技术专业能从事什么工作?
  • 【LeetCode数据结构】二叉树的应用(二)——二叉树的前序遍历问题、二叉树的中序遍历问题、二叉树的后序遍历问题详解
  • uniapp创建vue3+ts+pinia+sass项目
  • 2025年RISC-V中国峰会 主要内容
  • 绘图库 Matplotlib Search
  • RISC-V VP、Gem5、Spike
  • 恋爱时间倒计时网页设计与实现方案
  • 借助Aspose.HTML控件,在 Python 中将 SVG 转换为 PDF
  • Vue nextTick
  • 基于超176k铭文数据,谷歌DeepMind发布Aeneas,首次实现古罗马铭文的任意长度修复
  • MySQL存储引擎深度解析与实战指南
  • Java面试题及详细答案120道之(001-020)
  • JAVA_FIFTEEN_异常
  • LeetCode 233:数字 1 的个数
  • Zero-Shot TrackingT0:对象分割+运动感知记——当“切万物”武士学会运动记忆,目标跟踪稳如老狗
  • 力扣面试150题--寻找旋转排序数组中的最小值
  • 互联网金融项目实战(大数据Hadoop hive)
  • 代码随想录算法训练营第五十三天|图论part4
  • Hive【Hive架构及工作原理】
  • Hive-vscode-snippets
  • 微信小程序文件下载与预览功能实现详解
  • nacos安装