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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年03月25日 23时01分58秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
三郎数据结构学习笔记:双向循环链表(判断是否对称)附源码
2019-04-26
三郎数据结构算法学习笔记:基数排序
2019-04-26
三郎数据结构算法学习笔记:斐波那契(黄金分割法)查找算法
2019-04-26
Java中标识符的命名规则是什么?硬性要求和非硬性要求
2019-04-26
Java中八种基本数据类型的大小,以及他们的封装类
2019-04-26
Spring依赖注入的方式有几种,各是什么?
2019-04-26
SpringMVC怎么样设定重定向和转发的?
2019-04-26
SpringMVC常用的注解有哪些?
2019-04-26
spring bean的生命周期
2019-04-26
计算机网络子网划分详解
2019-04-26
计算机网络生成树算法STP简介
2019-04-26
三郎数据结构算法学习笔记:哈希表查找
2019-04-26
三郎数据结构算法学习笔记:二叉树的三种遍历及增删改查
2019-04-26
三郎数据结构算法学习笔记:顺序存储二叉树
2019-04-26
三郎数据结构算法学习笔记:线索二叉树
2019-04-26
解释Spring支持的几种bean的作用域。
2019-04-26
Spring框架中都用到了哪些设计模式?
2019-04-26
什么是MyBatis?
2019-04-26
MyBatis的优点和缺点
2019-04-26
#{}和${}的区别是什么?
2019-04-26