TC官方合作论坛

标题: 纯TC打开,读写EXCEL源码 [打印本页]

作者: cylhb    时间: 2013-10-16 01:36
标题: 纯TC打开,读写EXCEL源码
时不时总会有人问如何操作EXCEL。写了一个简易的示范代码给大家参考。


  1. 变量 xApp,xBook,xSheet//全局变量

  2. 功能 打开(工作簿名,表名)
  3.     xApp=com("Excel.Application")
  4.     xBook=com("Excel.Workbook")
  5.     xSheet=com("Excel.Worksheet")
  6.     xBook=xApp.Workbooks.open(工作簿名)
  7.     xSheet=xApp.Worksheets(表名)
  8. 功能结束

  9. 功能 关闭()
  10.     xBook.Close(真)
  11.     xSheet=null
  12.     xBook=null
  13.     xApp=null
  14. 功能结束

  15. 功能 读内容(行号,列号)
  16.     返回 xSheet.Cells(行号,列号)
  17. 功能结束

  18. 功能 写内容(行号,列号,内容)
  19.     xSheet.Cells(行号,列号).Value=内容
  20. 功能结束


  21. //开始按钮_点击操作
  22. 功能 开始_点击()
  23.     Excel.打开(系统.获取程序路径()&"1.xls","Sheet2") //创建对象,并打开指定的表,调用时请把Excel改成你自己的空间名
  24.     辅助.消息框(Excel.读内容(2,2))   //读操作示范,调用时请把Excel改成你自己的空间名
  25.     Excel.写内容(4,4,"哈哈TC")      //写操作示范,调用时请把Excel改成你自己的空间名
  26.     Excel.关闭() //用完之后别忘了关闭释放对象,调用时请把Excel改成你自己的空间名
  27. 功能结束
复制代码



作者: hu863063648    时间: 2013-10-16 07:04
回复 1楼cylhb的帖子

谢谢分享
作者: 52526262    时间: 2013-10-16 08:03
谢谢分享
作者: klyyz1980    时间: 2013-10-16 08:37
好,好,好
作者: lzq5628989    时间: 2013-10-16 08:41
学习了,谢谢斑竹~
作者: jianqiumy    时间: 2013-10-16 23:09
这方面也是少一点~~
希望能多一点关于这方面的~~
作者: cylhb    时间: 2013-10-16 23:35
回复 6楼jianqiumy的帖子

有了入门的用法,其他的相信只要稍微懂点VBS的都可以写出来了。呵呵。再不然可以百度一下具体的对象方法。
作者: jianqiumy    时间: 2013-10-17 01:06
回复 7楼cylhb的帖子

你说的没错~百度~~只是论坛里~~这方面知识和教程都少~或者说没有!!!
TC应该可以出多点这方面的知识~~~
应该出一些像官网简单脚本视频(办公类)~~或者一些小源码~这样就可以大家学习了
作者: cylhb    时间: 2013-10-17 20:44
确实,不过学这种东西,搜索能力很重要,要善于搜索。
作者: ief675037    时间: 2013-10-24 17:45
不行呀!TC 4.200beta 一点击开始按钮就停止工作了??
作者: mxxgmxxg    时间: 2013-10-30 17:10
这段代码一运行就会报错,不信你试试!还是按键精灵好,不会报错
作者: mxxgmxxg    时间: 2013-10-30 17:12
Sub ShutdownExcel
    VBSBegin
        Set obj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
        Set List = obj.ExecQuery ("Select * from Win32_Process Where Name = 'EXCEL.EXE'")
        For Each objProcess in List
            objProcess.Terminate()
        Next
    VBSEnd
End Sub
Sub OnScriptExit()
    ShutdownExcel
End Sub
ShutdownExcel

sIniFile="C:\Users\Administrator\Desktop\Book1.ini"
UserVar iDelay=DropList{"0.5秒":"500"|"1秒":"1000"|"1.5秒":"1500"|"2秒":"2000"|"3秒":"3000"|"5秒":"5000"}=1000 "设置延时"
Set xlApp = CreateObject("Excel.Application")
xlApp.visible = true
Set xlBook = xlApp.Workbooks.Open("C:\Users\Administrator\Desktop\Book1.xls")
Set xlSheet = xlBook.sheets("Sheet1")
nEndLine = xlsheet.UsedRange.SpecialCells(11).Row
nStartLine = InputBox("请输入起始行号:","" , 2)
nEndLine = InputBox("请输入结束行号:", "", xlSheet.UsedRange.SpecialCells(11).Row)
xlApp.Visible = False
Delay 1000
For i = nStartLine To nEndLine
    未发现运行环境,请安装微软拼音2.0以上版本!= xlsheet.Range("A" & i).Value
    未发现运行环境,请安装微软拼音2.0以上版本!= xlsheet.Range("B" & i).Value
    flag = xlsheet.Range("C" & i).Value
        
    If flag <> "ok" Then
        Call Plugin.Msg.ShowScrTXT(0, 0, 1024, 768, "第" & i & "行正在处理。", "0000FF")
        DoSomeWorks
        Call Plugin.Msg.ShowScrTXT(0, 0, 1024, 768, "第" & i & "行已处理。", "0000FF")
        xlsheet.Range("C" & i).Value = "ok"
        xlsheet.Range("D" & i).Value = now
        xlBook.Save
    End If
Next

xlApp.Quit
ExitScript

Sub DoSomeWorks
    If GetPoint("ssd",sIniFile) Then
        LockMouse
        //输入位置 [ssd] 的操作
        LeftClick 1
       Delay 200
        SayString 未发现运行环境,请安装微软拼音2.0以上版本!
        UnlockMouse
        Delay iDelay
    End If
    If GetPoint("fdfg",sIniFile) Then
        LockMouse
        //输入位置 [fdfg] 的操作
        LeftClick 1
       Delay 200
        SayString 未发现运行环境,请安装微软拼音2.0以上版本!
        UnlockMouse
        Delay iDelay
    End If
End Sub
Function GetPoint(sPointName,sIniFile)
    GetPoint =false
    sRegion=Plugin.File.ReadINI(sPointName, "点位置", sIniFile)
    If sRegion="" then
        MsgBox "设置""" & sPointName & """点位置", vbInformation Or vbSystemModal, "提示"
        Delay 500
        sTemp = Plugin.QM_GetPoint.GetPoint()
        sRegion = sTemp  
        Plugin.File.WriteINI sPointName, "点位置", sTemp, sIniFile  
        sArrayRegion = Split(sTemp, "|")
        Plugin.File.WriteINI sPointName, "RegionX", sArrayRegion(0), sIniFile
        Plugin.File.WriteINI sPointName, "RegionY", sArrayRegion(1), sIniFile   
    End if
    If sRegion<>"" then
        x=Cint(Plugin.File.ReadINI(sPointName, "RegionX", sIniFile))
        y=Cint(Plugin.File.ReadINI(sPointName, "RegionY", sIniFile))  
        MoveTo x,y
        Delay 100
        GetPoint=true
    Else
        MsgBox "没有设置要找的""" &sPointName & """点位置"
    End if
End Function
Function WaitMyPic(sPicName,sIniFile,nDelay)
    WaitMyPic=true
    wait=0
    Do While Not FindMyPic(sPicName, sIniFile )
        Wait = Wait + 1
        If Wait > 20 Then
            WaitMyPic=False
            Exit Do
        End If
        Delay nDelay
    Loop
End Function
Function FindMyPic(sPicName, sIniFile)
    FindMyPic = False
    sPic = Plugin.File.ReadINI(sPicName, "图片", sIniFile)
    If sPic = "" Then
        MsgBox "设置""" & sPicName & """图片", vbInformation Or vbSystemModal, "提示"
        Delay 500
        sPic = Plugin.QM_SetFindPic.GetFindPic()
        If sPic <> "" Then
            Plugin.File.WriteINI sPicName, "图片", sPic, sIniFile
            '//图片尺寸
            Delay 500
            sTemp = Plugin.QM_SetFindPic.GetPicSize(sPic)
            sArraySize = Split(sTemp, "|")
            Plugin.File.WriteINI sPicName, "PicWidth", sArraySize(0), sIniFile
            Plugin.File.WriteINI sPicName, "PicHeight", sArraySize(1), sIniFile
            Plugin.File.WriteINI sPicName, "RegionX1", "0", sIniFile
            Plugin.File.WriteINI sPicName, "RegionY1", "0", sIniFile
            Plugin.File.WriteINI sPicName, "RegionX2","0", sIniFile
            Plugin.File.WriteINI sPicName, "RegionY2", "0", sIniFile
        End If
    End If
          If sPic <> "" Then
        x1 = CInt(Plugin.File.ReadINI(sPicName, "RegionX1", sIniFile))
        y1 = CInt(Plugin.File.ReadINI(sPicName, "RegionY1", sIniFile))
        x2 = CInt(Plugin.File.ReadINI(sPicName, "RegionX2", sIniFile))
        y2 = CInt(Plugin.File.ReadINI(sPicName, "RegionY2", sIniFile))
        If x1 = x2 and y1 = y1 Then
            MsgBox "设置""" & sPicName & """图片查找区域", vbInformation Or vbSystemModal, "提示"
            '//查找图片的区域
            Delay 500
            sTemp = Plugin.QM_SetFindPic.GetFindRegion()
            If sTemp <> "" Then
                sArrayRegion = Split(sTemp, "|")
                Plugin.File.WriteINI sPicName, "RegionX1", sArrayRegion(0), sIniFile
                Plugin.File.WriteINI sPicName, "RegionY1", sArrayRegion(1), sIniFile
                Plugin.File.WriteINI sPicName, "RegionX2", sArrayRegion(2), sIniFile
                Plugin.File.WriteINI sPicName, "RegionY2", sArrayRegion(3), sIniFile
                x1 = CInt(sArrayRegion(0))
                y1 = CInt(sArrayRegion(1))
                x2 = CInt(sArrayRegion(2))
                y2 = CInt(sArrayRegion(3))
            End If
        End If
        If x1 <> x2 OR y1 <> y2 Then
            Width = CInt(Plugin.File.ReadINI(sPicName, "PicWidth", sIniFile))
            Height = CInt(Plugin.File.ReadINI(sPicName, "Picheight", sIniFile))
            FindPic x1, y1, x2, y2, sPic, 0.9, intX, intY
            If intX > 0 And intY > 0 Then
                MoveTo intX + Width / 2, intY + Height / 2
                Delay 500
                FindMyPic = True
            End If
        Else
            MsgBox "没有设置图片的查找区域", vbInformation Or vbSystemModal, "提示"
        End If
    Else
        MsgBox "没有设置要找的""" & sPicName & """图片", vbInformation Or vbSystemModal, "提示"
    End If
End Function

作者: xkc06zc    时间: 2013-11-7 10:52
楼上是按键精灵吧?我强烈要求版主开一个办公交流版块。我们搞办公的用TC确实太缺少资源了。
作者: haowork    时间: 2013-11-12 16:20
真的是不行,一运行就出错,TC功能好多出错啊
作者: haowork    时间: 2013-11-12 16:36
我发现即使是简单的功能,TC都有可能出错,按键很少,这方面来讲,TC要加油啊
作者: moist26    时间: 2014-4-25 00:12
本帖最后由 moist26 于 2014-4-25 00:22 编辑

有报错哦:


作者: rxuehao    时间: 2014-7-6 13:40
老大!
    xApp=com("Excel.Application")
    xBook=com("Excel.Workbook")
    xSheet=com("Excel.Worksheet")
    xBook=xApp.Workbooks.open("E:\\材料库.xlsx")
    xSheet=xApp.Worksheets(“蓝色”)
脚本第四行错误。。说是 xApp.Workbooks 不是合法的变量名或者是没有定义 怎么破啊!
作者: 亚太丶    时间: 2014-8-9 17:14
多谢分享,如果还多一点基础的介绍就好了
作者: tst    时间: 2014-11-22 19:32
不能用哦
[attach]18323[/attach]

作者: T星人    时间: 2014-11-23 00:27
很强大,谢谢分享~




欢迎光临 TC官方合作论坛 (http://bbs.52tc.co/) Powered by Discuz! X3.1