首页 > 代码库 > RubyWin32Api Win32OLE

RubyWin32Api Win32OLE

#ruby提供了多种运行外部程序的方法#1.%x %x不需要使用引号包含。#2. system方法 #3.exec类似system但是会中断当前的代码执行#system和exec不能捕获执行程序的输出。list=%x(dir d:\\) #捕获到输出结果system(notepad)p systemexec(notepad)p exec#被exec中断,不会执行下面的代码require Win32API#调用Win32apiget_cur=Win32API.new("user32","GetCursorPos",[P],V)set_cur=Win32API.new("user32","SetCursorPos",[i]*2,V)lpoint=" "*8get_cur.call(lpoint)x,y=lpoint.unpack("LL")p "当前鼠标的坐标为:X:#{x},Y:#{y}"new_xy=[12,12]set_cur.call new_xy[0],new_xy[1]require win32ole#调用win32oleexcel=WIN32OLE.new(excel.application)excel.Visible=trueexcel.WorkBooks.Add excel.Range("a1").value=http://www.mamicode.com/3excel.Range(a2).value=http://www.mamicode.com/2excel.Range(a3).value=http://www.mamicode.com/1excel.Range(b1).value=http://www.mamicode.com/"win32ole操作excel栗子"excel.Range(a1:a3).selectexcel_chart=excel.charts.addexcel_chart.type=-4100excel.ActiveWorkbook.SaveAs("c:\\test.xls")excel.ActiveWorkbook.Close(0)excel.Quitword=WIN32OLE.new(word.application)word.Visible=trueword.Documents.Addword.Selection.TypeText("你好")word.Selection.TypeParagraphword.Selection.TypeText("win32ole操作word栗子")#word.Selection.TypeParagraphword.Selection.InlineShapes.AddPicture("http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png")#本地和网络图片均可word.ActiveDocument.SaveAs("c:test.doc")word.ActiveDocument.closeword.quitie=WIN32OLE.new(internetexplorer.application)ie.visible=trueie.left=100ie.top=100ie.width=700ie.height=500ie.navigate http://www.baidu.com/s?wd=你好sleep 0.1 while ie.busyscript=ie.document.scriptscript.alert(这是ruby调用的js脚本)#script.eval(‘document.location=$("h3>a:eq(0)").attr("href")‘)#这是个问题。。怎么执行呢ie.Document.title=修改它的标题puts ie.documentie.quit

 

RubyWin32Api Win32OLE