【问题标题】:Copy the content of last line from text file and paste in excel column从文本文件中复制最后一行的内容并粘贴到 excel 列中
【发布时间】:2014-02-06 16:31:17
【问题描述】:

我想为以下场景编写一个批处理文件:

我有多个文本文件,例如 1.txt、2.txt...n.txt。 现在我想从文本文件中复制最后一行并将其粘贴到指定的 Excel 工作表列中,如 coln B 或 H。

谁能帮帮我?

谢谢

【问题讨论】:

标签: excel batch-file text copy


【解决方案1】:

在 VBScript 中你可以这样做:

  1. 创建您的 excel 文件

    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open("C:\test.xls")
    
  2. 循环浏览您的文本文件,打开它们并阅读最后一行

    Set n = 10 'the number of your files
    For i = 0 To n 
    
        Const ForReading = 1
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFile = objFSO.OpenTextFile(i & ".txt", ForReading)
    
        Do Until objFile.AtEndOfStream
            strLine = objFile.ReadLine
        Loop
    
        objFile.Close
    
  3. 将strLine中存储的数据写入你的excel文件

        objExcel.Application.Visible = True
        objExcel.Workbooks.Add
        objExcel.Cells(i, 1).Value = strLine
    
    Next
    
  4. 保存并关闭您的 Excel 文件

    objExcel.ActiveWorkbook.Save "C:\test.xls"
    objExcel.ActiveWorkbook.Close
    
    objExcel.Application.Quit
    

我没有测试过这段代码,它应该只是告诉你它是如何工作的。

【讨论】:

  • 我尝试运行,但在第 3 行显示错误为“需要对象:['number:10]'。有帮助吗??谢谢
猜你喜欢
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多