Created
September 30, 2012 10:22
-
-
Save katoy/3806401 to your computer and use it in GitHub Desktop.
Sample using jruby-poi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-*- coding: utf-8 -*- | |
# See | |
# | |
# $ jruby -S gem install jruby-pi | |
# $ jruby reader.rb Book1.xslx | |
# | |
# Env: | |
# jruby: 1.6.8 | |
# jruby-poi: 0.8.2 | |
# | |
# http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCell.html | |
# http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html | |
# http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFWorkbook.html | |
# | |
require 'rubygems' | |
require 'poi' | |
require 'pp' | |
def get_info(filename) | |
infos = {} | |
workbook = POI::Workbook.open(filename) | |
sheet_count = workbook.number_of_sheets | |
(0...sheet_count).each do |sheet_index| | |
matrix = {} | |
sheet_name = workbook.sheet_at(sheet_index).sheet_name | |
sheet = workbook.worksheets[sheet_name] | |
sheet.rows.each do |row| | |
pos_r = row.index | |
row.cells.each_with_index {|c, pos_c| | |
matrix[[pos_c, pos_r]] = "[#{c.poi_cell}] (#{c.poi_cell.getCellType()})" | |
} | |
end | |
infos[sheet_name] = matrix | |
end | |
infos | |
end | |
if __FILE__ == $0 | |
filename = ARGV[0] | |
sheets = get_info(filename) | |
pp sheets | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ jruby reader.rb Book1.xlsx | |
{"Sheet1"=> | |
{[0, 0]=>"[a] (1)", | |
[1, 0]=>"[b] (1)", | |
[2, 0]=>"[c] (1)", | |
[0, 1]=>"[1.0] (0)", | |
[1, 1]=>"[2.0] (0)", | |
[2, 1]=>"[3.0] (0)", | |
[3, 1]=>"[SUM(A2:D2)] (2)", | |
[0, 2]=>"[27-9-2012] (0)", | |
[0, 5]=>"[5x5] (1)"}, | |
"Sheet2"=>{}, | |
"Sheet3"=>{}} | |
$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment