[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
  1. 'By Broly
  2. 'From http://bbs.bathome.net/
  3. Dim fso,f,choice
  4. Dim xlsPath,txtPath,strText,target
  5. xlsPath = "d:\myExcel.xls"
  6. txtPath = "d:\myText.txt"
  7. target = "d:\tatget.txt"
  8. choice = InputBox("1.TXT -> Excel" & vbCrLf & "2.Excel -> TXT","请选择")
  9. Set fso = CreateObject("Scripting.FileSystemObject")
  10. Select Case choice
  11. Case 1:
  12. If Not fso.FileExists(xlsPath) Then CreateXlsFile(xlsPath)
  13. strText = GetText(txtPath)
  14. WriteExcel xlsPath,strText
  15. MsgBox "Succeed."
  16. Case 2:
  17. Set f = fso.OpenTextFile(target,8,True)
  18. f.Write GetExcelInfo(xlsPath)
  19. f.Close
  20. Set f = Nothing
  21. MsgBox "Succeed."
  22. Case Else:
  23. MsgBox "Wrong choice."
  24. End Select
  25. Set fso = Nothing
  26. Function CreateXlsFile(Path)
  27. Dim objExcel
  28. Set objExcel = CreateObject("Excel.Application")
  29. objExcel.Workbooks.Add()
  30. objExcel.ActiveWorkbook.SaveAs Path
  31. objExcel.Quit
  32. Set objExcel = Nothing
  33. End Function
  34. Function GetText(Path)
  35. Set f = fso.OpenTextFile(Path,1,False)
  36. If Not f.AtEndOfStream Then
  37. GetText = f.ReadAll
  38. End If
  39. f.Close
  40. Set f = Nothing
  41. End Function
  42. Function WriteExcel(Path,strText)
  43. Dim oExcel,oWorkBooks,oWorkSheet
  44. Dim arr,i,arr2
  45. arr = Split(strText,vbCrLf,-1,1)
  46. Set oExcel = CreateObject("Excel.Application")
  47. Set oWorkBooks = oExcel.Workbooks.Open(Path)
  48. Set oWorkSheet = oWorkBooks.Sheets(1)
  49. oWorkSheet.Activate
  50. oWorkSheet.Columns(1).ColumnWidth = 45
  51. oWorkSheet.Columns(2).ColumnWidth = 35
  52. For i = 0 To UBound(arr)
  53. arr2 = Split(arr(i),":",-1,1)
  54. ReDim Preserve arr2(2)
  55. If InStr(arr2(0),"-----") = 0 And _
  56. arr2(0) <> "" _
  57. Then
  58. oWorkSheet.Cells(i+1,1).Value = arr2(0) & ":"
  59. Else
  60. oWorkSheet.Cells(i+1,1).Value = arr2(0)
  61. End If
  62. oWorkSheet.Cells(i+1,2).Value = arr2(1)
  63. Next
  64. oWorkBooks.Save
  65. oWorkBooks.Close
  66. oExcel.Quit
  67. Set oExcel = Nothing
  68. Set oWorkBooks = Nothing
  69. Set oWorkSheet = Nothing
  70. End Function
  71. Function GetExcelInfo(Path)
  72. Dim oExcel,oWorkBooks,oWorkSheet
  73. Dim i,strText
  74. Set oExcel = CreateObject("Excel.Application")
  75. Set oWorkBooks = oExcel.Workbooks.Open(Path)
  76. Set oWorkSheet = oWorkBooks.Sheets(1)
  77. oWorkSheet.Activate
  78. i = 1
  79. Do While oWorkSheet.Cells(i,1).Value <> ""
  80. strText = strText & vbCrLf _
  81. & oWorkSheet.Cells(i,1).Value _
  82. & oWorkSheet.Cells(i,2).Value
  83. i = i + 1
  84. Loop
  85. oWorkBooks.Close
  86. oExcel.Quit
  87. GetExcelInfo = strText
  88. Set oExcel = Nothing
  89. Set oWorkBooks = Nothing
  90. Set oWorkSheet = Nothing
  91. End Function
复制代码
---学无止境---

TOP

返回列表