Class: Knj::Table_writer
Instance Method Summary collapse
- #close ⇒ Object
- #destroy ⇒ Object
- #ext ⇒ Object
- #ftype ⇒ Object
-
#initialize(args = {}) ⇒ Table_writer
constructor
A new instance of Table_writer.
- #write_row(arr) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Table_writer
Returns a new instance of Table_writer.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/knj/table_writer.rb', line 2 def initialize(args = {}) @args = args if !@args["filepath"] raise "No filepath was given." end if @args["format"] == "csv" @fp = File.open(@args["filepath"], "w") elsif @args["format"] == "excel5" require "spreadsheet" @wb = Spreadsheet::Workbook.new @ws = @wb.create_worksheet @row = 0 elsif @args["format"] == "excel2007" require "php_process" @php = Php_process.new @php.func("require_once", "PHPExcel.php") if @args["date_format"] @date_format_excel = args["date_format"].gsub("d", "dd").gsub("m", "mm").gsub("y", "yy").gsub("Y", "yyyy").gsub("-", '\\-') end #Array used for identifiyng Excel-columns. @colarr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] #This greatly speeds up the process, since it minimizes the call to PHP by ~25%. @date_cache = {} #Set cache-mode to cache-in-memory-gzip. cache_gzip_const = @php.constant_val("PHPExcel_CachedObjectStorageFactory::cache_to_discISAM") cosf = @php.static("PHPExcel_CachedObjectStorageFactory", "initialize", cache_gzip_const) @php.static("PHPExcel_Settings", "setCacheStorageMethod", cosf) #Create PHPExcel-objects. @pe = @php.new("PHPExcel") @pe.getProperties.setCreator(args["creator"]) if args["creator"] @pe.getProperties.setLastModifiedBy(args["last_modified_by"]) if args["last_modified_by"] @pe.getProperties.setTitle(args["title"]) if args["title"] @pe.getProperties.setSubject(args["subject"]) if args["subject"] @pe.getProperties.setDescription(args["descr"]) if args["descr"] @pe.setActiveSheetIndex(0) @sheet = @pe.getActiveSheet @linec = 1 else raise "Unsupported format: '#{@args["format"]}'." end end |
Instance Method Details
#close ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/knj/table_writer.rb', line 119 def close if @args["format"] == "csv" @fp.close elsif @args["format"] == "excel5" dirname = File.dirname(@args["filepath"]) basename = File.basename(@args["filepath"], File.extname(@args["filepath"])) temp_path = "#{dirname}/#{basename}.xls" @wb.write(temp_path) @wb = nil @ws = nil FileUtils.mv(temp_path, @args["filepath"]) elsif @args["format"] == "excel2007" writer = @php.new("PHPExcel_Writer_Excel2007", @pe) writer.save(@args["filepath"]) self.destroy else raise "Unsupported format: '#{@args["format"]}'." end return nil end |
#destroy ⇒ Object
113 114 115 116 117 |
# File 'lib/knj/table_writer.rb', line 113 def destroy @sheet = nil @php.destroy if @php @php = nil end |
#ext ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/knj/table_writer.rb', line 144 def ext if @args["format"] == "csv" return "csv" elsif @args["format"] == "excel5" return "xls" elsif @args["format"] == "excel2007" return "xlsx" else raise "Unsupported format: '#{@args["format"]}'." end end |
#ftype ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/knj/table_writer.rb', line 156 def ftype if @args["format"] == "csv" return "text/csv" elsif @args["format"] == "excel5" or @args["format"] == "excel2007" return "application/ms-excel" else raise "Unsupported format: '#{@args["format"]}'." end end |
#write_row(arr) ⇒ Object
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/knj/table_writer.rb', line 53 def write_row(arr) if @args["format"] == "csv" arr.each_index do |key| val = arr[key] if val.is_a?(Hash) and val["type"] == "decimal" arr[key] = Knj::Php.number_format(val["value"], @args["amount_decimals"], @args["amount_dsep"], @args["amount_tsep"]) elsif val.is_a?(Hash) and val["type"] == "date" arr[key] = Knj::Php.date(@args["date_format"], val["value"]) end end line_str = Knj::Csv.arr_to_csv(arr, @args["expl"], @args["surr"]) @fp.write(line_str) elsif @args["format"] == "excel5" col_count = 0 arr.each do |val| if val.is_a?(Hash) and val["type"] == "decimal" @ws[@row, col_count] = Knj::Php.number_format(val["value"], @args["amount_decimals"], @args["amount_dsep"], @args["amount_tsep"]) elsif val.is_a?(Hash) and val["type"] == "date" @ws[@row, col_count] = Knj::Php.date(@args["date_format"], val["value"]) else @ws[@row, col_count] = val end col_count += 1 end @row += 1 elsif @args["format"] == "excel2007" col_count = 0 arr.each do |val| colval = "#{@colarr[col_count]}#{@linec}" if val.is_a?(Hash) and val["type"] == "decimal" @sheet.setCellValue(colval, val["value"]) @sheet.getStyle(colval).getNumberFormat.setFormatCode("#,##0.00") elsif val.is_a?(Hash) and val["type"] == "date" datet = Knj::Datet.in(val["value"]) datet.days + 1 date_val = @php.static("PHPExcel_Shared_Date", "PHPToExcel", datet.to_i) @sheet.setCellValue(colval, date_val) @sheet.getStyle(colval).getNumberFormat.setFormatCode(@date_format_excel) else @sheet.setCellValue(colval, val) end col_count += 1 end @linec += 1 else raise "Unsupported format: '#{@args["format"]}'." end return nil end |