找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 22545|回复: 4

[问题求助] [已解决]VBS如何将txt文本数据按指定样式导入/生成Excel表格?

[复制链接]
发表于 2014-6-9 23:55:18 | 显示全部楼层 |阅读模式
本帖最后由 pcl_test 于 2016-7-27 20:50 编辑

EXCEL 读取txt文本数据  

在一个txt文本文件中存有一下格式数据

Your Merchant Order ID: # xxxxxxx
Purchase Date: February 2, 2009 10:17:30 AM PST
Shipping Service: Standard
Buyer Name: John Doe
Buyer E-mail: none@none.com

Your Merchant Order ID: # xxxxxxx
Purchase Date: February 2, 2009 10:17:30 AM PST
Shipping Service: Standard
Buyer Name: John Doe
Buyer E-mail: none@none.com
希望转换数据到excel文件中,并在一行显示同一记录。

Order ID        PurchaseDate        ShippingService        BuyerName        BuyerE-mail
# xxxxxxx        February 2, 2009 10        Standard        John Doe        none@none.com
# xxxxxxx        February 2, 2009 10        Standard        John Doe        none@none.com
  1. Sub DataRead()
  2.   Set fs = CreateObject("Scripting.FileSystemObject")
  3.   Open ActiveWorkbook.Path & "" & "ok.txt" For Input As #1
  4.   i = 2
  5.   Do While Not EOF(1)
  6.         Line Input #1, txt
  7.         Dim a
  8.         a = Split(txt, ":")
  9.         If InStr(txt, "Order ID") > 0 Then
  10.                 Cells(i, 1) = Trim(a(1))
  11.         ElseIf InStr(txt, "Purchase Date") > 0 Then
  12.                 Cells(i, 2) = Trim(a(1))
  13.         ElseIf InStr(txt, "Shipping Service") > 0 Then
  14.                 Cells(i, 3) = Trim(a(1))
  15.         ElseIf InStr(txt, "Buyer Name") > 0 Then
  16.                 Cells(i, 4) = Trim(a(1))
  17.         ElseIf InStr(txt, "Buyer E-mail") > 0 Then
  18.                 Cells(i, 5) = Trim(a(1))
  19.                 i = i + 1
  20.         End If
  21.   Loop
  22.   Close #1
  23.   End Sub
复制代码
上面代码,编译时,  Open ActiveWorkbook.Path & "\" & "ok.txt" For Input As #1 这句提示错误,语句未结束。求解

评分

参与人数 1PB +2 收起 理由
Batcher + 2 感谢给帖子标题标注[已解决]字样

查看全部评分

发表于 2014-6-10 00:15:13 | 显示全部楼层
本帖最后由 CrLf 于 2014-6-10 00:16 编辑

vbs 和 vba 不一样,不能直接套用,可以参考下别人怎么写:
http://blog.csdn.net/chentaocba/article/details/7907800

评分

参与人数 1技术 +1 收起 理由
Batcher + 1 乐于助人

查看全部评分

 楼主| 发表于 2014-6-11 00:38:50 | 显示全部楼层
回复 2# CrLf


    谢谢分享。
发表于 2016-7-27 21:58:15 | 显示全部楼层
  1. Set fso = CreateObject("Scripting.FileSystemObject")
  2. Set ExcelApp = CreateObject("Excel.Application")
  3. ExcelApp.Visible = True
  4. Set objBook= ExcelApp.Workbooks.Add
  5. str = split("Order ID|Purchase Date|Shipping Service|Buyer Name|Buyer E-mail", "|")
  6. ExcelApp.WorkSheets(1).Activate
  7. For i=0 to Ubound(str)
  8.     ExcelApp.Cells(1, i+1) = str(i)
  9. Next

  10. i=1
  11. Set f = fso.OpenTextFile("文本.txt", 1)
  12. Do While f.AtEndOfStream <> true
  13.     line = f.ReadLine
  14.     If InStr(line, str(0)) >0 Then i=i+1:ExcelApp.Cells(i, 1) = split(line, ":")(1)
  15.     If InStr(line, str(1)) >0 Then ExcelApp.Cells(i, 2) = split(line, ":")(1)
  16.     If InStr(line, str(2)) >0 Then ExcelApp.Cells(i, 3) = split(line, ":")(1)
  17.     If InStr(line, str(3)) >0 Then ExcelApp.Cells(i, 4) = split(line, ":")(1)
  18.     If InStr(line, str(4)) >0 Then ExcelApp.Cells(i, 5) = split(line, ":")(1)
  19. Loop

  20. objBook.SaveAs(fso.GetFolder(".").Path&"\新建.xls")
  21. objBook.Close
  22. ExcelApp.Quit
复制代码
发表于 2020-11-25 12:16:40 | 显示全部楼层
留个记号,答题
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 07:43 , Processed in 0.085037 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表