Ruby 调用ie ,excel等

转载自(http://notreally.iteye.com/blog/182986),供日后学习

Win32ole为标准Ruby发行版的一部分。Win32ole是访问Windows自动化的接口,可以让Ruby和Windows应用进行交互。具体说来Win32ole可以操作Word,Excel,IE,Outlook等。 



以下均为代码片段 
Word 
创建一个Word文件 
Ruby代码    收藏代码
  1. require 'win32ole'    
  2. word = WIN32OLE.new('Word.Application')     
  3. word.visible=true  #是否打开文件  
  4. word.Documents.Add()     
  5. for i in(0..100)     
  6.   word.Selection.Font.Size=12     
  7.   word.Selection.Font.ColorIndex = 2     
  8.   word.Selection.TypeText("Word with Ruby \n")     
  9. end    
  10. word.DefaultSaveFormat     
  11. word.Documents.close()   


Outlook 
调用Outlook发送邮件 
Ruby代码    收藏代码
  1. require 'win32ole'  
  2. outlook = WIN32OLE('Outlook.Application')  
  3. message = outlook.CreateItem(0)  
  4. message.Subject = 'Subject line here'  
  5. message.Body = 'This is the body of your message.'  
  6. message.To = 'xiaofan2350@yahoo.com.cn'  
  7. message.Attachments.Add('c:\really\one.txt', 1)  
  8. message.Send  


Excel 
创建一个Excel文件 
Ruby代码    收藏代码
  1. require 'win32ole'  
  2. excel = WIN32OLE.new('Excel.Application')  
  3. book = excel.workbooks.add  
  4. sheets = book.worksheets(1)  
  5. cells = sheets.cells("A1:A5")  
  6. cells.each do |cell|  
  7.   cell.value = 10  
  8. end  


Ruby代码    收藏代码
  1. require 'win32ole'     
  2. excel = WIN32OLE.new("Excel.Application")            
  3. excel.Visible = true  #是否打开文件  
  4. excel.WorkBooks.Open("d:\\really.xls")   #打开excel  
  5. worksheet = excel.ActiveWorkbook.WorkSheets(1)     
  6. # Output the sheet count of the current work book.  
  7. rows = worksheet.UsedRange.Rows  #得到excel文件的行数  
  8. worksheet.Range('A1:D1').value = ['North','South','East','West'#往excel指定区域写入数据  
  9. worksheet.Range('A2')['value'] = "really"  
  10. worksheet.Range('B2')['value'] = "notreally"  
  11. worksheet.Range('C2')['value'] = "javaeye"  
  12. worksheet.Range('D2')['value'] = "notreally.iteye.com"  
  13. excel.ActiveWorkbook.WorkSheets.add({'count'=>1, 'after'=>worksheet})  
  14. #添加一个excel工作区  
  15. excel.ActiveWorkbook.Close  #关闭工作区  
  16. excel.Quit()    


IE 
创建一个ie浏览器的实例 
Ruby代码    收藏代码
  1. require "win32ole" #包含库  
  2. ie = WIN32OLE.new('InternetExplorer.Application')  
  3. ie.visible = true #这个时候就可以看到一个ie的界面出来了  
  4. ie.navigate('http://www.ask123.net'#转到这个页面  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值