ruby导出excel图
发布日期:2021-09-29 20:08:55 浏览次数:11 分类:技术文章

本文共 4086 字,大约阅读时间需要 13 分钟。

#!/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]

转载地址:https://blog.csdn.net/horace_lee/article/details/83939187 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:rails 3 generate scaffold 会用到
下一篇:macox mysql 5.5.10

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年03月25日 23时01分58秒