Module: POIPond

Includes:
Style
Defined in:
lib/poi_pond.rb

Instance Method Summary collapse

Methods included from Style

#create_cell_style, #excel_cell_style, #hssf_cell_style, #poi_color

Instance Method Details

#add_photo_to_sheet(workbook, sheet, row, column, image) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/poi_pond.rb', line 40

def add_photo_to_sheet(workbook, sheet, row, column, image)
  picture_index = workbook.addPicture image, workbook.PICTURE_TYPE_JPEG
  drawing = workbook.getSheet(sheet).createDrawingPatriarch
  anchor = workbook.getCreationHelper.createClientAnchor
  anchor.setCol1 column
  anchor.setRow1 row
  drawing.createPicture(anchor, picture_index).resize
end

#create_excel_cell_range_addressObject



20
21
22
# File 'lib/poi_pond.rb', line 20

def create_excel_cell_range_address
  Rjb::import('org.apache.poi.ss.util.CellRangeAddress')
end

#create_excel_workbook(file = nil) ⇒ Object



16
17
18
# File 'lib/poi_pond.rb', line 16

def create_excel_workbook(file = nil)
  file ? Rjb::import('org.apache.poi.hssf.usermodel.HSSFWorkbook').new(file) : Rjb::import('org.apache.poi.hssf.usermodel.HSSFWorkbook').new
end

#create_spreadsheet(options, passed_styles = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/poi_pond.rb', line 49

def create_spreadsheet(options, passed_styles = nil)
  workbook = create_excel_workbook
  if passed_styles
    styles = {}
    passed_styles.each do |style|
      styles[style.first] = create_cell_style workbook, style.last
    end
  end
  options.each do |sheet_hash|
    sheet = workbook.createSheet sheet_hash[:sheet][:name]
    sheet.setPrintGridlines(!!sheet_hash[:sheet][:print_grid_lines]) 
    sheet.setDisplayGridlines(!!sheet_hash[:sheet][:display_grid_lines])
    if sheet_hash[:sheet][:merged_regions]
      sheet_hash[:sheet][:merged_regions].each do |merged_region|
        sheet.addMergedRegion create_excel_cell_range_address.new(merged_region[:start_row], merged_region[:end_row],
                                                                  merged_region[:start_column], merged_region[:end_column])
      end
    end
    if sheet_hash[:sheet][:photos]
      sheet_hash[:sheet][:photos].each do |photo|
        add_photo_to_sheet workbook, sheet_hash[:sheet][:name], photo[:row], photo[:column], photo[:photo]
      end
    end
    if sheet_hash[:sheet][:column_widths]
      sheet_hash[:sheet][:column_widths].each do |column_width|
        sheet.setColumnWidth column_width.first, column_width.last
      end
    end
    if sheet_hash[:sheet][:row] 
      sheet_hash[:sheet][:row].each do |row_hash|
        row = sheet.createRow row_hash[:row_index]
        row_hash[:row_height] ? row.setHeight(row_hash[:row_height]) : nil
        if row_hash[:cell]
          row_hash[:cell].each do |cell_hash|
            cell = row.createCell cell_hash[:cell_index]
            cell_hash[:value] ? cell.setCellValue(cell_hash[:value].to_s) : nil
            cell_hash[:style] ? cell.setCellStyle(styles[cell_hash[:style]]) : nil
          end
        end
      end
    end 
  end
  workbook
end

#hssf_data_formatObject



24
25
26
# File 'lib/poi_pond.rb', line 24

def hssf_data_format
  Rjb::import('org.apache.poi.hssf.usermodel.HSSFDataFormat')
end

#initialize_poiObject



6
7
8
9
10
11
12
13
14
# File 'lib/poi_pond.rb', line 6

def initialize_poi 
  dir = File.join(File.dirname(File.dirname(__FILE__)), 'javalibs')
  if File.exist?(dir)
    jardir = File.join(File.dirname(File.dirname(__FILE__)), 'javalibs', '**', '*.jar')
  else
    jardir = File.join('.','javalibs', '**', '*.jar')
  end
  Rjb::load(classpath = Dir.glob(jardir).join(':'), jvmargs=['-Djava.awt.headless=true'])
end

#poi_byte_array_output_streamObject



32
33
34
# File 'lib/poi_pond.rb', line 32

def poi_byte_array_output_stream
  Rjb::import('java.io.ByteArrayOutputStream').new
end

#poi_input_file(file) ⇒ Object



36
37
38
# File 'lib/poi_pond.rb', line 36

def poi_input_file(file)
  Rjb::import('java.io.FileInputStream').new(file)
end

#poi_output_file(file) ⇒ Object



28
29
30
# File 'lib/poi_pond.rb', line 28

def poi_output_file(file)
  Rjb::import('java.io.FileOutputStream').new(file)
end