1

I'm having problems trying to open an XLS file with Apache POI. I am integrating HSSF in rails application.

My code is like:

require "rjb"

  Rjb::add_jar(File.expand_path('demo/poi-3.9/poi-3.9-20121203.jar'))
  Rjb::add_jar(File.expand_path('demo/poi-3.9/commons-codec-1.7.jar'))

  Black = Rjb::import 'org.apache.poi.hssf.util.HSSFColor$BLACK'
  HSSFCell = Rjb::import 'org.apache.poi.hssf.usermodel.HSSFCell'
  HSSFCellStyle = Rjb::import 'org.apache.poi.hssf.usermodel.HSSFCellStyle'
  HSSFFont = Rjb::import 'org.apache.poi.hssf.usermodel.HSSFFont'
  HSSFWorkbook = Rjb::import 'org.apache.poi.hssf.usermodel.HSSFWorkbook'
  IOUtils = Rjb::import 'org.apache.poi.util.IOUtils'
  JavaFont = Rjb::import 'java.awt.Font'
  FileInputStream = Rjb::import 'java.io.FileInputStream'
  FileOutputStream = Rjb::import 'java.io.FileOutputStream'

def self.export_xls (params)
    file = "tmp/#{Time.now.to_formatted_s(:number)}_#{abc}.xls"
    @book = HSSFWorkbook.new
    @sheet1 = @book.createSheet l(:report_name)
    @drawing = @sheet1.createDrawingPatriarch()
    @number_of_column = 0
    @number_of_column_to_merge = 10
    black = Black.index

    @styleTableTitle = @book.createCellStyle()       
    @styleTableTitle.setBorderBottom(HSSFCellStyle.BORDER_THIN)
    @styleTableTitle.setBottomBorderColor(black)

    out = FileOutputStream.new("#{Rails.root}/" + file)
    @book.write(out)
    out.close()
    file
end

Exception throw:

RuntimeError (java.lang.ClassNotFoundException: org.apache.poi.hssf.util.HSSFColor$BLACK)

Imported jars (Apache POI):

poi-3.9-20121203 poi-3.7-20101029 commons-codec-1.7

BlackBeard
  • 10,246
  • 7
  • 52
  • 62
Rahul2692
  • 334
  • 1
  • 10
  • Looks like a problem with the line *JavaFont = Rjb::import 'java.awt.Font'*, try commenting it out and seeing if the error persists, if not that's your problem – Mark Jun 27 '18 at 14:49
  • 1
    Why are you using such old versions of Apache POI? Why are you trying to use two different old versions at the same time? What happens when you stop this and just have one new POI jar? – Gagravarr Jun 27 '18 at 16:00
  • Exception is throwing only in production. JavaFont = Rjb::import 'java.awt.Font' is working fine. – Rahul2692 Jun 29 '18 at 10:25

0 Answers0