- 安装SpreadSheet插件
gem install spreadsheet
- 检查是否安装 gem list spreadsheet
gem list spreadsheet
这里显示安装的版本,表示安装成功
3.编写代码
#encoding:utf-8
require "spreadsheet/excel"
#设置表格的编码为utf-8
Spreadsheet.client_encoding="utf-8"
#创建表格对象
book=Spreadsheet::Workbook.new
puts book
#创建工作表
sheet1=book.create_worksheet :name => "test1"
#在表格第一行设置分类
sheet1.row(0)[0]="姓名"
sheet1.row(0)[1]="爱好"
sheet1.row(0)[2]="国家"
5.times do |i|
sheet1.row(i+1)[0]="张山#{i}"
sheet1.row(i+1)[1]="爱好#{i}"
sheet1.row(i+1)[2]="国家#{i}"
end
#在指定路径下面创建test1.xls表格,并写book对象
# book.write "#{Dir.pwd}/../../public/test1.xls"
book.write "/Users/yashino/Documents/RubyTable/test1.xls"
# /Users/yashino/Documents/RubyTable
https://www.jianshu.com/p/9e1e1db83f6d
gem install creek
gem install rubyXL
ruby下的excel库
http://www.bubuko.com/infodetail-3220100.html
网友评论