Ruby/Windows Platform/Excel
Материал из Wiki.crossplatform.ru
Содержание |
Add some more sheets
require "win32ole" excel = WIN32OLE.new("excel.application") excel["Visible"] = true workbook = excel.Workbooks.Add(1) worksheet= workbook.Worksheets("Sheet1") worksheet.name="Ruby Greetings" worksheet.Cells(1, 1)["Value"]="Hello Ruby Fans" worksheet.Cells(1, 1).Font["Bold"]=true worksheet.Cells(1, 1).font["size"]=18 for name in ["dog", "cat", "rabbit", "bird"] worksheet = workbook.Worksheets.Add worksheet.name=name for row in 2..6 for col in 2..10 worksheet.Cells(row, col)["Value"]="(#{row}, #{col})" end end for col in 2..10 worksheet.Columns(col).AutoFit end end
Create a chart and rotate it
require "win32ole" ChartTypeVal = 4100; excel = WIN32OLE.new("excel.application") excel["Visible"] = TRUE excel.Workbooks.Add() excel.Range("a1")["Value"] = 3 excel.Range("a2")["Value"] = 2 excel.Range("a3")["Value"] = 1 excel.Range("a1:a3").Select() excelchart = excel.Charts.Add() excelchart["Type"] = ChartTypeVal 30.step(180, 5) do |rot| excelchart.rotation = rot sleep(0.1) end excel.ActiveWorkbook.Close(0) excel.Quit()
Create a chart in Excel
require "win32ole" knowledge_profit=gets excel=WIN32OLE.new("excel.application") excel["Visible"]=TRUE workbook=excel.workbooks.add() excel.Range("a1")["Value"]= 1 excel.Range("a2")["Value"]= 2 excel.Range("a3")["Value"]= 3 excel.Range("a4")["Value"]= 4 excel.Range("a1:a4").select() excel.Range("b1")["Value"]= 1 excel.Range("b2")["Value"]= 2 excel.Range("b3")["Value"]= 3 excel.Range("b4")["Value"]= 4 excel.Range("b1:b4").select() excelchart1=workbook.charts.add() excelchart2=workbook.charts.add() excelchart1["Type"]=-4099 excelchart2["Type"]=-4099 workbook.SaveAs "Graph.xls" excel.Quit # chart constants specified below: # xlRadar = -4151 # xlXYScatter = -4169 # xlCombination = -4111 # xl3DArea = -4098 # xl3DBar = -4099 # xl3DColumn = -4100 # xl3DLine = -4101 # xl3DPie = -4102 # xl3DSurface = -4103 # xlDoughnut = -4120
Set cell value and read it back
require "win32ole" excel = WIN32OLE.new("excel.application") excel.Workbooks.Add excel.Range("a1").Value = 10 excel.Range("a2").Value = 20 excel.Range("a3").Value = "=a1+a2" excel.Range("a1:a3").each do |cell| p cell.Value end
Using WIN32OLE with Excel
require "win32ole" excelobj = WIN32OLE.new("excel.application") excelobj["Visible"]=TRUE excelobj.workbooks.add() excelobj.Range("a6")["Value"] = 100 excelobj.Range("a1")["Value"] = 100 excelobj.Range("a2")["Value"] = 200 excelobj.Range("a3")["Value"] = 300 excelobj.Range("a4")["Value"] = 400 excelobj.Range("a1:a4").select()