TC官方合作论坛

 找回密码
 立即注册
查看: 3130|回复: 19
打印 上一主题 下一主题

[源码分享] 纯TC打开,读写EXCEL源码

  [复制链接]
跳转到指定楼层
楼主
发表于 2013-10-16 01:36:45 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

马上加入TC

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
时不时总会有人问如何操作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. 功能结束
复制代码


评分

参与人数 2金币 +20 收起 理由
T星人 + 10 赞一个!
境界程序员 + 10 很给力!

查看全部评分

回复

使用道具 举报

推荐
发表于 2013-11-7 10:52:32 | 只看该作者
楼上是按键精灵吧?我强烈要求版主开一个办公交流版块。我们搞办公的用TC确实太缺少资源了。
回复 支持 1 反对 0

使用道具 举报

20#
发表于 2014-11-23 00:27:30 | 只看该作者
很强大,谢谢分享~
回复 支持 反对

使用道具 举报

19#
发表于 2014-11-22 19:32:04 | 只看该作者
不能用哦

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 支持 反对

使用道具 举报

18#
发表于 2014-8-9 17:14:44 | 只看该作者
多谢分享,如果还多一点基础的介绍就好了
回复 支持 反对

使用道具 举报

17#
发表于 2014-7-6 13:40:59 | 只看该作者
老大!
    xApp=com("Excel.Application")
    xBook=com("Excel.Workbook")
    xSheet=com("Excel.Worksheet")
    xBook=xApp.Workbooks.open("E:\\材料库.xlsx")
    xSheet=xApp.Worksheets(“蓝色”)
脚本第四行错误。。说是 xApp.Workbooks 不是合法的变量名或者是没有定义 怎么破啊!
回复 支持 反对

使用道具 举报

16#
发表于 2014-4-25 00:12:14 | 只看该作者
本帖最后由 moist26 于 2014-4-25 00:22 编辑

有报错哦:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 支持 反对

使用道具 举报

15#
发表于 2013-11-12 16:36:48 | 只看该作者
我发现即使是简单的功能,TC都有可能出错,按键很少,这方面来讲,TC要加油啊
回复 支持 反对

使用道具 举报

14#
发表于 2013-11-12 16:20:27 | 只看该作者
真的是不行,一运行就出错,TC功能好多出错啊
回复 支持 反对

使用道具 举报

12#
发表于 2013-10-30 17:12:00 | 只看该作者
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
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /2 下一条

关闭

小黑屋|TC官方合作论坛 (苏ICP备18043773号

GMT+8, 2025-9-24 08:02 , Processed in 0.176517 second(s), 27 queries .

Powered by 海安天坑软件科技有限公司

© 2001-2013 Comsenz Inc.

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