Skip to content

Instantly share code, notes, and snippets.

@davisinfo
Created April 10, 2019 10:00
Show Gist options
  • Save davisinfo/59aac41db924bedefa05fafa2a9f2516 to your computer and use it in GitHub Desktop.
Save davisinfo/59aac41db924bedefa05fafa2a9f2516 to your computer and use it in GitHub Desktop.
Using java libraries to fill a XLS file from Ruby - works both with MRI and jruby
if !(RUBY_PLATFORM =~ /java/)
require 'rjb-loader'
RjbLoader.before_load do |config|
# This code changes the JVM classpath, so it has to run BEFORE loading Rjb.
Dir[File.join(Rails.root , '/resources/*.jar')].each do |path|
config.classpath << File::PATH_SEPARATOR + File.expand_path(path)
config.java_options = ['-Xms128M', '-Xmx256M']
end
end
RjbLoader.after_load do |config|
FILE_CLASS = Rjb::import('java.io.FileOutputStream')
WORKBOOK_CLASS = Rjb::import('org.apache.poi.hssf.usermodel.HSSFWorkbook')
POIFS_CLASS = Rjb::import('org.apache.poi.poifs.filesystem.POIFSFileSystem')
Rjb::import('org.apache.poi.hssf.usermodel.HSSFCreationHelper')
Rjb::import('org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator')
CELL_REFERENCE_CLASS = Rjb::import('org.apache.poi.hssf.util.CellReference')
CELL_CLASS = Rjb::import('org.apache.poi.hssf.usermodel.HSSFCell')
FILE_INPUT_CLASS = Rjb::import('java.io.FileInputStream')
FILE_OUTPUT_CLASS = Rjb::import('java.io.FileOutputStream')
end
else
apache_poi_path = File.join(Rails.root , '/resources/poi-3.9-20121203.jar')
require 'java'
require apache_poi_path
FILE_CLASS = java.io.FileOutputStream
WORKBOOK_CLASS = org.apache.poi.hssf.usermodel.HSSFWorkbook
POIFS_CLASS = org.apache.poi.poifs.filesystem.POIFSFileSystem
CELL_REFERENCE_CLASS = org.apache.poi.hssf.util.CellReference
CELL_CLASS = org.apache.poi.hssf.usermodel.HSSFCell
FILE_INPUT_CLASS = java.io.FileInputStream
FILE_OUTPUT_CLASS = java.io.FileOutputStream
end
_docs = []
_orders = self.payment_orders.all
_orders.delete_if { |po| po.send_by_post? }
done = (_orders.size == 0)
sheet_no = 1
@file_input = FILE_INPUT_CLASS.new(File.join(Rails.root, "/resources/cscs-payment-new.xls"))
@fs = POIFS_CLASS.new(@file_input)
@book = WORKBOOK_CLASS.new(@fs)
@sheet = @book.getSheetAt(0)
while !done do
0.upto(49) do |i|
order = _orders[(sheet_no-1)*50+i]
if order then
_default_address = order.sales_order_product.shipping_address
@row = @sheet.getRow(20+i)
@row ||= @sheet.createRow(20+i)
@cell = @row.getCell(2)
@cell ||= @row.createCell(2)
@cell.setCellValue("#{order.contact.title} #{order.contact.full_name}")
@cell = @row.getCell(3)
@cell ||= @row.createCell(2)
@cell.setCellValue("#{order.contact.nino}")
@cell = @row.getCell(4)
@cell ||= @row.createCell(2)
@cell.setCellValue("#{order.contact.formatted_date_of_birth}")
else
done = true
end
end
if RUBY_PLATFORM =~ /java/
@file_output = java.io.FileOutputStream.new(File.join(Rails.root, 'tmp', "cscs-payment-new-#{self.id}.xls"))
else
@file_output = FILE_OUTPUT_CLASS.new(File.join(Rails.root, 'tmp', "cscs-payment-new-#{self.id}.xls"))
end
@book.write(@file_output)
@file_output.close
_doc = open("#{Rails.root}/tmp/cscs-payment-new-#{self.id}.xls").read
_docs << {:data => _doc, :filename => "supportline_applications_#{Date.today.to_s}_new_#{sheet_no}.xls"}
sheet_no += 1
end
_docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment