ruby 操作 Win32ole (windows自动化接口)
2016-01-18 23:58
323 查看
Win32ole为标准Ruby发行版的一部分。Win32ole是访问Windows自动化的接口,可以让Ruby和Windows应用进行交互。具体说来Win32ole可以操作Word,Excel,IE,Outlook等。
以下均为代码片段
Word
创建一个Word文件
Ruby代码
require 'win32ole'
word = WIN32OLE.new('Word.Application')
word.visible=true #是否打开文件
word.Documents.Add()
for i in(0..100)
word.Selection.Font.Size=12
word.Selection.Font.ColorIndex = 2
word.Selection.TypeText("Word with Ruby \n")
end
word.DefaultSaveFormat
word.Documents.close()
Outlook
调用Outlook发送邮件
Ruby代码
require 'win32ole'
outlook = WIN32OLE('Outlook.Application')
message = outlook.CreateItem(0)
message.Subject = 'Subject line here'
message.Body = 'This is the body of your message.'
message.To = 'xiaofan2350@yahoo.com.cn'
message.Attachments.Add('c:\really\one.txt', 1)
message.Send
Excel
创建一个Excel文件
Ruby代码
require 'win32ole'
excel = WIN32OLE.new('Excel.Application')
book = excel.workbooks.add
sheets = book.worksheets(1)
cells = sheets.cells("A1:A5")
cells.each do |cell|
cell.value = 10
end
Ruby代码
require 'win32ole'
excel = WIN32OLE.new("Excel.Application")
excel.Visible = true #是否打开文件
excel.WorkBooks.Open("d:\\really.xls") #打开excel
worksheet = excel.ActiveWorkbook.WorkSheets(1)
# Output the sheet count of the current work book.
rows = worksheet.UsedRange.Rows #得到excel文件的行数
worksheet.Range('A1:D1').value = ['North','South','East','West'] #往excel指定区域写入数据
worksheet.Range('A2')['value'] = "really"
worksheet.Range('B2')['value'] = "notreally"
worksheet.Range('C2')['value'] = "javaeye"
worksheet.Range('D2')['value'] = "notreally.javaeye.com"
excel.ActiveWorkbook.WorkSheets.add({'count'=>1, 'after'=>worksheet})
#添加一个excel工作区
excel.ActiveWorkbook.Close #关闭工作区
excel.Quit()
IE
创建一个ie浏览器的实例
Ruby代码
require "win32ole" #包含库
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = true #这个时候就可以看到一个ie的界面出来了
ie.navigate('http://www.ask123.net') #转到这个页面
以下均为代码片段
Word
创建一个Word文件
Ruby代码
require 'win32ole'
word = WIN32OLE.new('Word.Application')
word.visible=true #是否打开文件
word.Documents.Add()
for i in(0..100)
word.Selection.Font.Size=12
word.Selection.Font.ColorIndex = 2
word.Selection.TypeText("Word with Ruby \n")
end
word.DefaultSaveFormat
word.Documents.close()
Outlook
调用Outlook发送邮件
Ruby代码
require 'win32ole'
outlook = WIN32OLE('Outlook.Application')
message = outlook.CreateItem(0)
message.Subject = 'Subject line here'
message.Body = 'This is the body of your message.'
message.To = 'xiaofan2350@yahoo.com.cn'
message.Attachments.Add('c:\really\one.txt', 1)
message.Send
Excel
创建一个Excel文件
Ruby代码
require 'win32ole'
excel = WIN32OLE.new('Excel.Application')
book = excel.workbooks.add
sheets = book.worksheets(1)
cells = sheets.cells("A1:A5")
cells.each do |cell|
cell.value = 10
end
Ruby代码
require 'win32ole'
excel = WIN32OLE.new("Excel.Application")
excel.Visible = true #是否打开文件
excel.WorkBooks.Open("d:\\really.xls") #打开excel
worksheet = excel.ActiveWorkbook.WorkSheets(1)
# Output the sheet count of the current work book.
rows = worksheet.UsedRange.Rows #得到excel文件的行数
worksheet.Range('A1:D1').value = ['North','South','East','West'] #往excel指定区域写入数据
worksheet.Range('A2')['value'] = "really"
worksheet.Range('B2')['value'] = "notreally"
worksheet.Range('C2')['value'] = "javaeye"
worksheet.Range('D2')['value'] = "notreally.javaeye.com"
excel.ActiveWorkbook.WorkSheets.add({'count'=>1, 'after'=>worksheet})
#添加一个excel工作区
excel.ActiveWorkbook.Close #关闭工作区
excel.Quit()
IE
创建一个ie浏览器的实例
Ruby代码
require "win32ole" #包含库
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = true #这个时候就可以看到一个ie的界面出来了
ie.navigate('http://www.ask123.net') #转到这个页面
相关文章推荐
- swig基本入门
- Sass-Ruby及Sass、Compass在Windows下安装
- Deepin 安装RVM ,ruby, rails
- Making Fabric Play Nice with RubyMotion
- ruby安装mysql2模块
- ruby访问mysql
- 180行ruby代码搞定游戏2048
- ruby函数回调的实现方法
- ruby函数回调的实现方法
- CocoaPods安装使用
- ruby each do 用法
- 如何在Mac OS X上安装 Ruby运行环境
- dock 下安装ruby搭建rails 服务
- Ruby笔记
- Ruby安装
- ruby语言是什么东西
- RVM切换ruby版本
- 《Java & Ruby 代码分析系列》之基本的类实例对比
- ruby postgres 连接
- CocoaPods的下载和安装及常见问题