lib/poi/workbook/workbook.rb in jruby-poi-0.4.0 vs lib/poi/workbook/workbook.rb in jruby-poi-0.5.0
- old
+ new
@@ -5,11 +5,11 @@
class Workbook
def self.open(filename_or_stream)
name, stream = if filename_or_stream.kind_of?(java.io.InputStream)
[File.join(Dir.tmpdir, "spreadsheet.xlsx"), filename_or_stream]
elsif filename_or_stream.kind_of?(IO) || StringIO === filename_or_stream || filename_or_stream.respond_to?(:read)
- # NOTE: the String.unpack here can be very inefficient
+ # NOTE: the String.unpack here can be very inefficient on large files
[File.join(Dir.tmpdir, "spreadsheet.xlsx"), java.io.ByteArrayInputStream.new(filename_or_stream.read.unpack('c*').to_java(:byte))]
else
raise Exception, "FileNotFound" unless File.exists?( filename_or_stream )
[filename_or_stream, java.io.FileInputStream.new(filename_or_stream)]
end
@@ -31,18 +31,31 @@
def save
save_as(@filename)
end
def save_as(filename)
- @workbook.write(java.io.FileOutputStream.new(filename))
+ output = output_stream filename
+ begin
+ @workbook.write(output)
+ ensure
+ output.close
+ end
end
+
+ def output_stream name
+ java.io.FileOutputStream.new(name)
+ end
def close
#noop
end
def worksheets
Worksheets.new(self)
+ end
+
+ def [](sheet_index)
+ worksheets[sheet_index]
end
def poi_workbook
@workbook
end