Skip to content

Commit

Permalink
Initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeming committed Jul 27, 2010
0 parents commit 85f62df
Show file tree
Hide file tree
Showing 26 changed files with 919 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
.DS_Store
.project
.idea
nbproject
.*~
tmp
*.swp
TAGS
507 changes: 507 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions NOTICE
@@ -0,0 +1,21 @@
Apache POI
Copyright 2009 The Apache Software Foundation

This product includes software developed by
The Apache Software Foundation (http://www.apache.org/).

This product contains the DOM4J library (http://www.dom4j.org).
Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.

This product contains parts that were originally based on software from BEA.
Copyright (c) 2000-2003, BEA Systems, <http://www.bea.com/>.

This product contains W3C XML Schema documents. Copyright 2001-2003 (c)
World Wide Web Consortium (Massachusetts Institute of Technology, European
Research Consortium for Informatics and Mathematics, Keio University)

This product contains the Piccolo XML Parser for Java
(http://piccolo.sourceforge.net/). Copyright 2002 Yuval Oren.

This product contains the chunks_parse_cmds.tbl file from the vsdump program.
Copyright (C) 2006-2007 Valek Filippov (frob@df.ru)
31 changes: 31 additions & 0 deletions README.markdown
@@ -0,0 +1,31 @@
jruby-poi
=========

This little gem provides an alternative interface to the Apache POI java library, for jruby.

INSTALL:
========

* gem install jruby-poi

Contributors
============

* [Scott Deming](http://github.com/sdeming)

Note on Patches/Pull Requests
=============================

* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a future version unintentionally.
* Commit, do not mess with rakefile, version, or history.
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
* Send me a pull request.

Copyright
=========

Copyright (c) 2010 Scott Deming and others.
See NOTICE and LICENSE for details.

14 changes: 14 additions & 0 deletions Rakefile
@@ -0,0 +1,14 @@
begin
require 'jeweler'
Jeweler::Tasks.new do |gemspec|
gemspec.name = "jruby-poi"
gemspec.summary = "Apache POI class library for jruby"
gemspec.description = "A rubyesque library for manipulating spreadsheets and other document types for jruby, using Apache POI."
gemspec.email = "sdeming@makefile.com"
gemspec.homepage = "http://github.com/sdeming/jruby-poi"
gemspec.authors = ["Scott Deming"]
end
rescue LoadError
puts "Jeweler not available. Install it with: gem install jeweler"
end

Binary file added lib/ooxml-lib/dom4j-1.6.1.jar
Binary file not shown.
Binary file added lib/ooxml-lib/geronimo-stax-api_1.0_spec-1.0.jar
Binary file not shown.
Binary file added lib/ooxml-lib/xmlbeans-2.3.0.jar
Binary file not shown.
Binary file added lib/poi-3.6-20091214.jar
Binary file not shown.
Binary file added lib/poi-contrib-3.6-20091214.jar
Binary file not shown.
Binary file added lib/poi-examples-3.6-20091214.jar
Binary file not shown.
Binary file added lib/poi-ooxml-3.6-20091214.jar
Binary file not shown.
Binary file added lib/poi-ooxml-schemas-3.6-20091214.jar
Binary file not shown.
Binary file added lib/poi-scratchpad-3.6-20091214.jar
Binary file not shown.
14 changes: 14 additions & 0 deletions lib/poi.rb
@@ -0,0 +1,14 @@
JRUBY_POI_LIB_PATH=File.expand_path(File.dirname(__FILE__))

# Java
require 'java'
require File.join(JRUBY_POI_LIB_PATH, 'poi-3.6-20091214.jar')
require File.join(JRUBY_POI_LIB_PATH, 'poi-ooxml-3.6-20091214.jar')
require File.join(JRUBY_POI_LIB_PATH, 'poi-ooxml-schemas-3.6-20091214.jar')
require File.join(JRUBY_POI_LIB_PATH, 'poi-scratchpad-3.6-20091214.jar')
require File.join(JRUBY_POI_LIB_PATH, 'poi-contrib-3.6-20091214.jar')
require File.join(JRUBY_POI_LIB_PATH, 'ooxml-lib', 'xmlbeans-2.3.0.jar')
require File.join(JRUBY_POI_LIB_PATH, 'ooxml-lib', 'dom4j-1.6.1.jar')

# Ruby
require File.join(JRUBY_POI_LIB_PATH, 'poi', 'workbook')
4 changes: 4 additions & 0 deletions lib/poi/workbook.rb
@@ -0,0 +1,4 @@
require File.join(JRUBY_POI_LIB_PATH, 'poi', 'workbook', 'workbook')
require File.join(JRUBY_POI_LIB_PATH, 'poi', 'workbook', 'worksheet')
require File.join(JRUBY_POI_LIB_PATH, 'poi', 'workbook', 'row')
require File.join(JRUBY_POI_LIB_PATH, 'poi', 'workbook', 'cell')
42 changes: 42 additions & 0 deletions lib/poi/workbook/cell.rb
@@ -0,0 +1,42 @@
module POI
class Cells
include Enumerable

def initialize(row)
@row = row
@poi_row = row.poi_row
end

def [](index)
Cell.new(@poi_row.getCell(index))
end

def size
@poi_row.getPhysicalNumberOfCells
end

def each
it = @poi_row.cellIterator
yield Cell.new(it.next) while it.hasNext
end
end

class Cell
def initialize(cell)
@cell = cell
end

def index
@cell.getColumnIndex
end

def to_s
@cell.getStringCellValue
end

def poi_cell
@cell
end
end
end

42 changes: 42 additions & 0 deletions lib/poi/workbook/row.rb
@@ -0,0 +1,42 @@
module POI
class Rows
include Enumerable

def initialize(worksheet)
@worksheet = worksheet
@poi_worksheet = worksheet.poi_worksheet
end

def [](index)
Row.new(@poi_worksheet.getRow(index))
end

def size
@poi_worksheet.getPhysicalNumberOfRows
end

def each
it = @poi_worksheet.rowIterator
yield Row.new(it.next) while it.hasNext
end
end

class Row
def initialize(row)
@row = row
end

def cells
Cells.new(self)
end

def index
@row.getRowNum
end

def poi_row
@row
end
end
end

41 changes: 41 additions & 0 deletions lib/poi/workbook/workbook.rb
@@ -0,0 +1,41 @@
module POI
class Workbook
def self.open(filename)
raise Exception, "FileNotFound" unless File.exists? filename
instance = self.new(filename)

if block_given?
result = yield instance
return result
end

instance
end

def initialize(filename)
@filename = filename
@workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create(java.io.FileInputStream.new(filename))
end

def save
@workbook.write(java.io.FileOutputStream.new(@filename))
end

def save_as(filename)
@workbook.write(java.io.FileOutputStream.new(filename))
end

def close
#noop
end

def worksheets
Worksheets.new(self)
end

def poi_workbook
@workbook
end
end
end

47 changes: 47 additions & 0 deletions lib/poi/workbook/worksheet.rb
@@ -0,0 +1,47 @@
module POI
class Worksheets
include Enumerable

def initialize(workbook)
@workbook = workbook
@poi_workbook = workbook.poi_workbook
end

def [](index)
worksheet = case
when index.kind_of?(Numeric)
@poi_workbook.getSheetAt(index)
else
@poi_workbook.getSheet(index)
end
Worksheet.new(worksheet)
end

def size
@poi_workbook.getNumberOfSheets
end

def each
(0...size).each { |i| yield Worksheet.new(@poi_workbook.getSheetAt(i)) }
end
end

class Worksheet
def initialize(worksheet = nil)
@worksheet = worksheet
end

def name
@worksheet.getSheetName
end

def rows
Rows.new(self)
end

def poi_worksheet
@worksheet
end
end
end

Binary file added specs/data/simple_with_picture.ods
Binary file not shown.
Binary file added specs/data/simple_with_picture.xls
Binary file not shown.
Binary file added specs/data/simple_with_picture.xlsx
Binary file not shown.
50 changes: 50 additions & 0 deletions specs/io_spec.rb
@@ -0,0 +1,50 @@
require File.join(File.dirname(__FILE__), 'spec_helper')

describe POI::Workbook do
it "should read an xlsx file" do
name = TestDataFile.expand_path("simple_with_picture.xlsx")
book = nil
lambda { book = POI::Workbook.open(name) }.should_not raise_exception
book.should be_kind_of POI::Workbook
end

it "should read an xls file" do
name = TestDataFile.expand_path("simple_with_picture.xls")
book = nil
lambda { book = POI::Workbook.open(name) }.should_not raise_exception
book.should be_kind_of POI::Workbook
end

it "should read an ods file" do
pending "get a valid, non excel produced ods file" do
name = TestDataFile.expand_path("simple_with_picture.ods")
book = nil
lambda { book = POI::Workbook.open(name) }.should_not raise_exception
book.should be_kind_of POI::Workbook
end
end

it "should write an open workbook" do
name = TestDataFile.expand_path("simple_with_picture.xlsx")
POI::Workbook.open(name) do |book|
lambda { book.save }.should_not raise_exception
end
end

it "should write an open workbook to a new file" do
pending "write to a temp directory to avoid git polution" do
name = TestDataFile.expand_path("simple_with_picture.xlsx")
new_name = TestDataFile.expand_path("saved-as.xlsx")

POI::Workbook.open(name) do |book|
lambda { book.save_as(new_name) }.should_not raise_exception
end

book = nil
lambda { book = POI::Workbook.open(new_name) }.should_not raise_exception
book.should be_kind_of POI::Workbook

File.exists?(new_name).should == true
end
end
end
10 changes: 10 additions & 0 deletions specs/spec_helper.rb
@@ -0,0 +1,10 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'poi'))

class TestDataFile
def self.expand_path(name)
File.expand_path(File.join(File.dirname(__FILE__), 'data', name))
end
end

Spec::Runner.configure do |config|
end

0 comments on commit 85f62df

Please sign in to comment.