Class: SimpleXlsx::Serializer
- Inherits:
-
Object
- Object
- SimpleXlsx::Serializer
- Defined in:
- lib/simple_xlsx/serializer.rb
Instance Method Summary collapse
- #add_content_types ⇒ Object
- #add_doc_props ⇒ Object
- #add_relationship_part ⇒ Object
- #add_styles ⇒ Object
- #add_workbook_part ⇒ Object
- #add_workbook_relationship_part ⇒ Object
- #add_worksheets_directory ⇒ Object
-
#initialize(to) ⇒ Serializer
constructor
A new instance of Serializer.
- #open_stream_for_sheet(ndx) ⇒ Object
Constructor Details
#initialize(to) ⇒ Serializer
Returns a new instance of Serializer.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/simple_xlsx/serializer.rb', line 5 def initialize to @to = to Zip::ZipFile.open(to, Zip::ZipFile::CREATE) do |zip| @zip = zip add_doc_props add_worksheets_directory add_relationship_part add_styles @doc = Document.new(self) yield @doc add_workbook_relationship_part add_content_types add_workbook_part end end |
Instance Method Details
#add_content_types ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/simple_xlsx/serializer.rb', line 47 def add_content_types @zip.get_output_stream "[Content_Types].xml" do |f| f.puts '<?xml version="1.0" encoding="UTF-8"?>' f.puts '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' f.puts <<-ends <Override PartName="/_rels/.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/> <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/> <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/> <Override PartName="/xl/_rels/workbook.xml.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> ends if @doc.has_shared_strings? f.puts '<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>' end @doc.sheets.each_with_index do |sheet, ndx| f.puts "<Override PartName=\"/xl/worksheets/sheet#{ndx+1}.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml\"/>" end f.puts '<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>' f.puts "</Types>" end end |
#add_doc_props ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/simple_xlsx/serializer.rb', line 103 def add_doc_props @zip.mkdir "docProps" @zip.get_output_stream "docProps/core.xml" do |f| f.puts <<-ends <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <dcterms:created xsi:type="dcterms:W3CDTF">2010-07-20T14:30:58.00Z</dcterms:created> <cp:revision>0</cp:revision> </cp:coreProperties> ends end @zip.get_output_stream "docProps/app.xml" do |f| f.puts <<-ends <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"> <TotalTime>0</TotalTime> </Properties> ends end end |
#add_relationship_part ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/simple_xlsx/serializer.rb', line 89 def add_relationship_part @zip.mkdir "_rels" @zip.get_output_stream "_rels/.rels" do |f| f.puts <<-ends <?xml version="1.0" encoding="UTF-8"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/> <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/> <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/> ends f.puts "</Relationships>" end end |
#add_styles ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/simple_xlsx/serializer.rb', line 124 def add_styles @zip.get_output_stream "xl/styles.xml" do |f| f.puts <<-ends <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <numFmts count="7"> <numFmt formatCode="GENERAL" numFmtId="164"/> <numFmt formatCode=""TRUE";"TRUE";"FALSE"" numFmtId="170"/> </numFmts> <fonts count="5"> <font><name val="Mangal"/><family val="2"/><sz val="10"/></font> <font><name val="Arial"/><family val="0"/><sz val="10"/></font> <font><name val="Arial"/><family val="0"/><sz val="10"/></font> <font><name val="Arial"/><family val="0"/><sz val="10"/></font> <font><name val="Arial"/><family val="2"/><sz val="10"/></font> </fonts> <fills count="2"> <fill><patternFill patternType="none"/></fill> <fill><patternFill patternType="gray125"/></fill> </fills> <borders count="1"> <border diagonalDown="false" diagonalUp="false"><left/><right/><top/><bottom/><diagonal/></border> </borders> <cellStyleXfs count="20"> <xf applyAlignment="true" applyBorder="true" applyFont="true" applyProtection="true" borderId="0" fillId="0" fontId="0" numFmtId="164"> <alignment horizontal="general" indent="0" shrinkToFit="false" textRotation="0" vertical="bottom" wrapText="false"/> <protection hidden="false" locked="true"/> </xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="2" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="2" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="43"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="41"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="44"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="42"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="9"></xf> </cellStyleXfs> <cellXfs count="7"> <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="164" xfId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="22" xfId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="15" xfId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="1" xfId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="2" xfId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="49" xfId="0"></xf> <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="170" xfId="0"></xf> </cellXfs> <cellStyles count="6"><cellStyle builtinId="0" customBuiltin="false" name="Normal" xfId="0"/> <cellStyle builtinId="3" customBuiltin="false" name="Comma" xfId="15"/> <cellStyle builtinId="6" customBuiltin="false" name="Comma [0]" xfId="16"/> <cellStyle builtinId="4" customBuiltin="false" name="Currency" xfId="17"/> <cellStyle builtinId="7" customBuiltin="false" name="Currency [0]" xfId="18"/> <cellStyle builtinId="5" customBuiltin="false" name="Percent" xfId="19"/> </cellStyles> </styleSheet> ends end end |
#add_workbook_part ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/simple_xlsx/serializer.rb', line 21 def add_workbook_part @zip.get_output_stream "xl/workbook.xml" do |f| f.puts <<-ends <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> <workbookPr date1904="0" /> <sheets> ends @doc.sheets.each_with_index do |sheet, ndx| f.puts "<sheet name=\"#{sheet.name}\" sheetId=\"#{ndx + 1}\" r:id=\"#{sheet.rid}\"/>" end f.puts "</sheets></workbook>" end end |
#add_workbook_relationship_part ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/simple_xlsx/serializer.rb', line 69 def add_workbook_relationship_part @zip.mkdir "xl/_rels" @zip.get_output_stream "xl/_rels/workbook.xml.rels" do |f| f.puts <<-ends <?xml version="1.0" encoding="UTF-8"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> ends cnt = 0 f.puts "<Relationship Id=\"rId#{cnt += 1}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\" Target=\"styles.xml\"/>" @doc.sheets.each_with_index do |sheet, ndx| sheet.rid = "rId#{cnt += 1}" f.puts "<Relationship Id=\"#{sheet.rid}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet\" Target=\"worksheets/sheet#{ndx + 1}.xml\"/>" end if @doc.has_shared_strings? f.puts '<Relationship Id="rId#{cnt += 1}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="xl/sharedStrings.xml"/>' end f.puts "</Relationships>" end end |
#add_worksheets_directory ⇒ Object
36 37 38 39 |
# File 'lib/simple_xlsx/serializer.rb', line 36 def add_worksheets_directory @zip.mkdir "xl" @zip.mkdir "xl/worksheets" end |
#open_stream_for_sheet(ndx) ⇒ Object
41 42 43 44 45 |
# File 'lib/simple_xlsx/serializer.rb', line 41 def open_stream_for_sheet ndx @zip.get_output_stream "xl/worksheets/sheet#{ndx + 1}.xml" do |f| yield f end end |