Board logo

标题: [问题求助] 如何用vbs将excel表格的某一行数据提取出来另存csv文件? [打印本页]

作者: siweiqi    时间: 2014-12-5 10:35     标题: 如何用vbs将excel表格的某一行数据提取出来另存csv文件?

如何用vbs将excel表格的某一行数据提取出来另存csv文件?
作者: yu2n    时间: 2014-12-5 20:29

VBS 提取Excel第七行存入CSV文件
  1. CommandMode "VBS 提取Excel第七行存入CSV文件  By  Yu2n@qq.com"
  2. Main
  3. Sub Main()
  4.   On Error Resume Next
  5.   ' 选择文件夹
  6.   Dim strFolder, arrPath, strPath, nFileCount, i
  7.   WScript.Echo "请选择 Excel 文件路径:"
  8.   strFolder = BrowseForFolder("请选择 Excel 文件路径:")
  9.   If strFolder = "" Then Exit Sub
  10.   arrPath = ScanFolder(strFolder)
  11.   ' 统计XLS、XLSX个数,用于显示进度
  12.   For Each strPath In arrPath
  13.     If LCase(Right(strPath,4))=".xls" Or LCase(Right(strPath,5))=".xlsx" Then
  14.       nFileCount = nFileCount + 1
  15.     End If
  16.   Next
  17.   ' 执行转换
  18.   Dim dtStart, objExcel
  19.   dtStart = Now()
  20.   Set objExcel = Excel_Init()
  21.   For Each strPath In arrPath
  22.     If LCase(Right(strPath,4))=".xls" Or LCase(Right(strPath,5))=".xlsx" Then
  23.       i = i + 1
  24.       ' 显示进度
  25.       WScript.Echo "[" & i & "/" & nFileCount & "]" & strPath
  26.       ' 提取Excel第七行存入CSV文件
  27.       Change_Excel objExcel, strPath
  28.     End If
  29.   Next
  30.   objExcel.Quit
  31.   WScript.Echo nFileCount & " 个文档完成,耗时 " & DateDiff("s",dtStart,Now()) & " 秒。"
  32.   Msgbox nFileCount & " 个文档完成,耗时 " & DateDiff("s",dtStart,Now()) & " 秒。", vbInformation+vbOKOnly, WScript.ScriptName
  33. End Sub
  34. ' 打开XLS/XLSX,提取Excel第七行存入CSV文件
  35. Function Change_Excel(ByVal objExcel, ByVal FilePath)
  36.   On Error Resume Next
  37.   Const xlCSV = 6
  38.   Dim objWorkBook, strNew, strOld
  39.   Set objWorkBook = objExcel.Workbooks.Open(FilePath)
  40.   Set objWorkbook1 = objExcel.Workbooks.Add
  41.   ' 修改XLS/XLSX活动Sheet
  42.   objWorkBook.ActiveSheet.Rows("7:7").Copy _
  43.     objWorkBook1.ActiveSheet.Rows("7:7")
  44.   objWorkBook.Close False
  45.   objWorkBook1.SaveAs _
  46.       FilePath & ".csv", xlCSV, False
  47.   objWorkBook1.Close False
  48.   Set objWorkBook = Nothing
  49.   If Not Err.Number = 0 Then Change_Excel = True
  50. End Function
  51. ' 创建 Excel 对象
  52. Function Excel_Init()
  53.   On Error Resume Next
  54.   Const msoAutomationSecurityForceDisable = 3
  55.   Set objExcel = CreateObject("Excel.Application")
  56.   If Not Err.Number = 0 Then
  57.     Msgbox "错误:无法创建 Excel 对象,你可能没有安装 Excel 。", vbCritical+vbOKOnly, WScript.ScriptName
  58.     WScript.Quit(999)
  59.   End If
  60.   If Not objExcel.application.version >= 12.0 Then
  61.     Msgbox "警告:请使用 Office 2007 以上版本。", vbExclamation+vbOKOnly, WScript.ScriptName
  62.   End If
  63.   ' 隐藏运行,屏蔽提示
  64.   objExcel.Visible = False
  65.   objExcel.DisplayAlerts = False
  66.   objExcel.AutomationSecurity = msoAutomationSecurityForceDisable
  67.   Set Excel_Init = objExcel
  68. End Function
  69. ' 以命令提示符环境运行(保留参数)
  70. Sub CommandMode(ByVal sTitle)
  71.   If (LCase(Right(WScript.FullName,11)) = "wscript.exe") Then
  72.     Dim i, sArgs
  73.     For i = 1 To WScript.Arguments.Count
  74.       sArgs = sArgs & " " & """" & WScript.Arguments(i-1) & """"
  75.     Next
  76.     CreateObject("WScript.Shell").Run( _
  77.       "cmd /c Title " & sTitle & " &cscript.exe //NoLogo  """ & _
  78.       WScript.ScriptFullName & """ " & sArgs & " &pause"),3
  79.       WScript.Quit
  80.   End If
  81. End Sub
  82. ' 浏览文件夹
  83. Function BrowseForFolder(ByVal strTips)
  84.   Dim objFolder
  85.   Set objFolder = CreateObject("Shell.Application").BrowseForFolder (&H0, strTips, &H0010 + &H0001)
  86.   If (Not objFolder Is Nothing) Then BrowseForFolder = objFolder.Self.Path  'objFolder.Items().Item().Path
  87. End Function
  88. ' 获取文件夹所有文件夹、文件列表(数组)
  89. Function ScanFolder(ByVal strPath)
  90.   Dim arr() : ReDim Preserve arr(-1)
  91.   Call SCAN_FOLDER(arr, strPath) : ScanFolder = arr
  92. End Function
  93. Function SCAN_FOLDER(ByRef arr, ByVal folderSpec)
  94.   On Error Resume Next
  95.   Dim fso, objItems, objFile, objFolder
  96.   Set fso = CreateObject("Scripting.FileSystemObject")
  97.   Set objItems = fso.GetFolder(folderSpec)
  98.   If Right(folderSpec, 1) <> "\" Then folderSpec = folderSpec & "\"
  99.   If (Not fso.FolderExists(folderSpec)) Then Exit Function
  100.   For Each objFile In objItems.Files
  101.     ReDim Preserve arr(UBound(arr) + 1)
  102.     arr(UBound(arr)) = objFile.Path
  103.   Next
  104.   For Each objFolder In objItems.subfolders
  105.     Call SCAN_FOLDER(arr, objFolder.Path)
  106.   Next
  107.   ReDim Preserve arr(UBound(arr) + 1)
  108.   arr(UBound(arr)) = folderSpec
  109. End Function
复制代码

作者: 34205    时间: 2015-1-22 17:53

XUE XI L E A  AAAAAAAAAAAA




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2