Class: JExcelFile

Inherits:
Object show all
Defined in:
lib/engine/jruby/jexcel_file.rb

Constant Summary collapse

MAX_COLUMNS =
256.freeze
MAX_ROWS =
65536.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJExcelFile

The HSSFWorkbook uses 0 based indexes



45
46
47
# File 'lib/engine/jruby/jexcel_file.rb', line 45

def initialize()
  @book = nil
end

Instance Attribute Details

#bookObject

Returns the value of attribute book.



36
37
38
# File 'lib/engine/jruby/jexcel_file.rb', line 36

def book
  @book
end

#current_sheetObject

Returns the value of attribute current_sheet.



36
37
38
# File 'lib/engine/jruby/jexcel_file.rb', line 36

def current_sheet
  @current_sheet
end

#rowObject

Returns the value of attribute row.



36
37
38
# File 'lib/engine/jruby/jexcel_file.rb', line 36

def row
  @row
end

#sheet(i = nil) ⇒ Object (readonly)

Return the current or specified HSSFSheet



65
66
67
# File 'lib/engine/jruby/jexcel_file.rb', line 65

def sheet
  @sheet
end

Instance Method Details

#cell_value(cell) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/engine/jruby/jexcel_file.rb', line 99

def cell_value(cell)
  return nil unless cell
  #puts "DEBUG CELL TYPE : #{cell} => #{cell.getCellType().inspect}"
  case (cell.getCellType())
    when HSSFCell::CELL_TYPE_FORMULA  then return cell.getCellFormula()
    when HSSFCell::CELL_TYPE_NUMERIC  then return cell.getNumericCellValue()
    when HSSFCell::CELL_TYPE_STRING   then return cell.getStringCellValue()
    when HSSFCell::CELL_TYPE_BOOLEAN  then return cell.getBooleanCellValue()
    when HSSFCell::CELL_TYPE_BLANK    then return ""
  end
end

#create(sheet_name) ⇒ Object



57
58
59
60
61
62
# File 'lib/engine/jruby/jexcel_file.rb', line 57

def create(sheet_name)
  @book = HSSFWorkbook.new()
  @sheet = @book.createSheet(sheet_name.gsub(" ", ''))
  date_style = @book.createCellStyle()
  date_style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"))
end

#create_row(index) ⇒ Object

Create new row, bring index in line with POI usage (our 1 is their 0)



83
84
85
86
# File 'lib/engine/jruby/jexcel_file.rb', line 83

def create_row(index)
  @row = @sheet.createRow(index)
  @row
end

#each_rowObject

Process each row. (type is org.apache.poi.hssf.usermodel.HSSFRow)



77
78
79
# File 'lib/engine/jruby/jexcel_file.rb', line 77

def each_row
  @sheet.rowIterator.each { |row| yield row }
end

#num_rowsObject



71
72
73
# File 'lib/engine/jruby/jexcel_file.rb', line 71

def num_rows
  @sheet.getPhysicalNumberOfRows
end

#open(filename) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/engine/jruby/jexcel_file.rb', line 49

def open(filename)
  inp = FileInputStream.new(filename)

  @book = HSSFWorkbook.new(inp)

  sheet(0)  # also sets @current_sheet
end

#save(filename) ⇒ Object



111
112
113
# File 'lib/engine/jruby/jexcel_file.rb', line 111

def save( filename )
  File.open( filename, 'w') {|f| f.write(to_s) }
end

#set_cell(row, column, data) ⇒ Object



88
89
90
91
# File 'lib/engine/jruby/jexcel_file.rb', line 88

def set_cell(row, column, data)
  @row = @sheet.getRow(row) || create_row(row)
  @row.createCell(column).setCellValue(data)
end

#to_sObject

The internal representation of a Excel File



118
119
120
121
122
123
# File 'lib/engine/jruby/jexcel_file.rb', line 118

def to_s
  outs = ByteArrayOutputStream.new
  @book.write(outs);
  outs.close();
  String.from_java_bytes(outs.toByteArray)
end

#value(row, column) ⇒ Object

Raises:

  • (TypeError)


93
94
95
96
97
# File 'lib/engine/jruby/jexcel_file.rb', line 93

def value(row, column)
  raise TypeError, "Expect row argument of type HSSFRow" unless row.is_a?(Java::OrgApachePoiHssfUsermodel::HSSFRow)
  #puts "DEBUG - CELL VALUE : #{column} => #{ cell_value( row.getCell(column) ).inspect}"
  cell_value( row.getCell(column) )
end