Class: XLSX::Workbook

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsx/workbook.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ Workbook

Returns a new instance of Workbook.



29
30
31
32
# File 'lib/xlsx/workbook.rb', line 29

def initialize(filename=nil)
  @filename = filename
  @sheets = []
end

Instance Method Details

#create_worksheet(params) ⇒ Object



38
39
40
41
42
43
# File 'lib/xlsx/workbook.rb', line 38

def create_worksheet(params)
  params[:name] ||= "Sheet #{@sheets.size + 1}"
  ws = XLSX::Worksheet.new(self, params)
  @sheets << ws
  ws
end

#worksheet(sheet_no) ⇒ Object



34
35
36
# File 'lib/xlsx/workbook.rb', line 34

def worksheet(sheet_no)
  @sheets[sheet_no]
end

#write(filename = nil) ⇒ Object



45
46
47
48
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
# File 'lib/xlsx/workbook.rb', line 45

def write(filename=nil)
  @strings_count = 0
  @strings = []
  @strings_idx = {}
  
  filename ||= @filename
  File.unlink(filename) if File.exists?(filename)
  Zip::ZipFile.open(filename, Zip::ZipFile::CREATE) do |zipfile|
    zipfile.get_output_stream("[Content_Types].xml") { |f| write_content_types(f) }
    zipfile.get_output_stream("_rels/.rels") { |f| write_root_rels(f) }
    zipfile.get_output_stream("docProps/app.xml") { |f| write_app_properties(f) }
    zipfile.get_output_stream("docProps/core.xml") { |f| write_core_properties(f) }
    zipfile.get_output_stream("xl/_rels/workbook.xml.rels") { |f| write_workbook_rels(f) }
    zipfile.get_output_stream("xl/workbook.xml") { |f| write_workbook(f) }
    zipfile.get_output_stream("xl/styles.xml") { |f| write_styles(f) }

    0.upto(@sheets.size - 1) do |sheet_no|
      zipfile.get_output_stream("xl/worksheets/sheet#{sheet_no + 1}.xml") { |f| write_worksheet(@sheets[sheet_no], f) }
    end

    zipfile.get_output_stream("xl/sharedStrings.xml") { |f| write_shared_strings(f) }
  end
  
  @strings_count = nil
  @strings = nil
  @strings_idx = nil
  
  true
end