ruby导出excel图


#!/usr/bin/ruby -w

require 'writeexcel'

workbook = WriteExcel.new('chart.xls')
worksheet = workbook.add_worksheet

chart = workbook.add_chart(:type => 'Chart::Scatter')

# Configure the chart.
chart.add_series(
:categories => '=Sheet1!$A$2:$A$7',
:values => '=Sheet1!$B$2:$B$7'
)

# Add the worksheet data the chart refers to.
data = [
[ 'Category', 2, 3, 4, 5, 6, 7 ],
[ 'Value', 1, 4, 5, 2, 1, 5 ]
]

worksheet.write('A1', data)

workbook.close



#!/usr/bin/ruby -w

require 'writeexcel'

workbook = WriteExcel.new('chart_scatter.xls')
worksheet = workbook.add_worksheet
bold = workbook.add_format(:bold => 1)

# Add the worksheet data that the charts will refer to.
headings = [ 'Number', 'Sample 1', 'Sample 2' ]
data = [
[ 2, 3, 4, 5, 6, 7 ],
[ 1, 4, 5, 2, 1, 5 ],
[ 3, 6, 7, 5, 4, 3 ]
]

worksheet.write('A1', headings, bold)
worksheet.write('A2', data)

# Create a new chart object. In this case an embedded chart.
chart = workbook.add_chart(:type => 'Chart::Scatter', :embedded => 1)

# Configure the first series. (Sample 1)
chart.add_series(
:name => 'Sample 1',
:categories => '=Sheet1!$A$2:$A$7',
:values => '=Sheet1!$B$2:$B$7'
)

# Configure the second series. (Sample 2)
chart.add_series(
:name => 'Sample 2',
:categories => '=Sheet1!$A$2:$A$7',
:values => '=Sheet1!$C$2:$C$7'
)

# Add a chart title and some axis labels.
chart.set_title (:name => 'Results of sample analysis')
chart.set_x_axis(:name => 'Test number')
chart.set_y_axis(:name => 'Sample length (cm)')

# Insert the chart into the worksheet (with an offset).
worksheet.insert_chart('D2', chart, 25, 10)

workbook.close




# creating a chart in excel
require 'win32ole'

# set some parameter variables
xlColumns = 2
xlColumnClustered = 51
xlWhite = 2
xlRed = 3
xlBlue = 5
xlGray = 15

# connect to a running instance of excel
xl = WIN32OLE.connect('Excel.Application')
wb = xl.Workbooks('mlb_stats.xls')

# delete "MLB Scoring" chart if it already exists
xl.DisplayAlerts = false
begin
wb.Charts("MLB Scoring").Delete
rescue
end
xl.DisplayAlerts = true

# create a new chart
mychart = wb.Charts.Add
mychart.Name = "MLB Scoring"
mychart.SetSourceData wb.Worksheets("Runs Scored and Allowed").Range("A1:C15"), xlColumns
mychart.ChartType = xlColumnClustered

# set series names in the legend
mychart.SeriesCollection(1).Name = "Runs Scored"
mychart.SeriesCollection(2).Name = "Runs Allowed "

# set colors
mychart.SeriesCollection(1).Interior.ColorIndex = xlBlue
mychart.SeriesCollection(2).Interior.ColorIndex = xlRed
mychart.ChartArea.Interior.ColorIndex = xlWhite
mychart.ChartArea.Border.ColorIndex = xlBlue
mychart.PlotArea.Interior.ColorIndex = xlGray
mychart.PlotArea.Border.ColorIndex = xlWhite

# set chart title properties
mychart.HasTitle = true
mychart.ChartTitle.Characters.Text = "American League - Runs Scored vs. Runs Allowed"
mychart.ChartTitle.Font.Name = 'Verdana'
mychart.ChartTitle.Font.Size = 16
mychart.ChartTitle.Font.Bold = true




require 'win32ole '

# -4100 is the value for the Excel constant xl3DColumn.
ChartTypeVal = -4100;

# Creates OLE object to Excel
excel = WIN32OLE.new( "excel.application ")

# Create and rotate the chart

excel[ 'Visible '] = TRUE
workbook = excel.Workbooks.Add()
excel.Range( "a1 ")[ 'Value '] = 3
excel.Range( "a2 ")[ 'Value '] = 2
excel.Range( "a3 ")[ 'Value '] = 1
excel.Range( "a1:a3 ").Select()
excelchart = workbook.Charts.Add()
excelchart[ 'Type '] = ChartTypeVal

30.step(180, 10) do |rot|
sleep(1)
excelchart[ 'Rotation '] = rot
end

excelchart2 = workbook.Charts.Add();
excelchart3 = workbook.Charts.Add();

charts = workbook.Charts
charts.each { |i| puts i }

excel.ActiveWorkbook.Close(0);
excel.Quit();




参考:

[url]http://rubyonwindows.blogspot.com/2008/06/automating-excel-chart-axis-and-legend.html[/url]
[url]http://rubyonwindows.blogspot.com/2008/06/automating-excel-creating-charts.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值