Ruby支持比较且用excel直接打前提excel没特别复杂表结构式渲染等
例:
Ruby代码
outfile = File.open('csvout', 'wb')
CSV::Writer.generate(outfile) do |csv|
csv <<['c1', nil, '', '"', "\r\n", 'c2']
...
end
outfile.close
其般用考虑spreadsheet像楼提供连接所说例适合读取复杂适合参考用复杂建议看面说明
excel文件
想excel文件首先像写文件先加载spreadsheet类库指定编码接着创建Workbook
Ruby代码
book = Spreadsheet::Workbook.new
workbook基础创建Worksheet表单
Ruby代码
sheet1 = book.create_worksheet
用式创建表单:
Ruby代码
sheet2 = book.create_worksheet :name =>'My Second Worksheet'
sheet1.name = 'My First Worksheet'
我采用式加载数据表单Worksheet#[]=,
Worksheet#update_row, 或者直接给指定单元格复制
Ruby代码
sheet1.row(0).concat %w{Name Country Acknowlegement}
sheet1[1,0] = 'Japan'
row = sheet1.row(1)
row.push 'Creator of Ruby'
row.unshift 'Yukihiro Matsumoto'
sheet1.row(2).replace [ 'Daniel J. Berger', 'U.S.A.',
'Author of original code for Spreadsheet::Excel' ]
sheet1.row(3).push 'Charles Lowe', 'Author of the ruby-ole Library'
sheet1.row(3).insert 1, 'Unknown'
sheet1.update_row 4, 'Hannes Wyss', 'Switzerland', 'Author'
于格式处理:
Ruby代码
sheet1.row(0).height = 18
format = Spreadsheet::Format.new :color =>:blue,
:weight =>:bold,
:size =>18
sheet1.row(0).default_format = format
bold = Spreadsheet::Format.new :weight =>:bold
4.times do |x| sheet1.row(x + 1).set_format(0, bold) end
保存excel文件
Ruby代码
book.write '/path/to/output/excel-file.xls'
windows平台考虑WIN23OLE,处理windows转换强
Ruby代码
require 'win23ole'
application = WIN32OLE.new('Excel.Application')
worksheet
=application.Workbooks.Open(excelFileName).Worksheets(workSheetName)
worksheet.Activate
contLoop = true # Dummy counter for the loop
while contLoop do
colVal = worksheet.Cells(row, column).Value
if (colVal) then
# 字段非空则表示行值
# 处理读取
do processing ....
else
# 表明结束
# End the loop
contLoop = false
end
# go to the next Row
row += 1
end
# we are done
application.Workbooks.Close
通常在安装完ruby和sass之后,如果你的sass或scss文件内有中文的话,编译时就会报错,如:Syntax error: Invalid GBK character解决的方案就是为sass编译添加默认的编码方式为utf-8
步骤:
1、找到下面这个文件 C:\Ruby21-x64\lib\ruby\gems\2.1.0\gems\sass-3.4.8\lib\sass\engine.rb
2、找到require 'sass/supports'这一行,在下面一行添加Encoding.default_external = Encoding.find('utf-8')