|
| 1 | +module SimpleXlsx |
| 2 | + |
| 3 | +class Serializer |
| 4 | + |
| 5 | + def initialize to |
| 6 | + @to = to |
| 7 | + Zip::ZipFile.open(to, Zip::ZipFile::CREATE) do |zip| |
| 8 | + @zip = zip |
| 9 | + add_doc_props |
| 10 | + add_relationship_part |
| 11 | + add_worksheets_directory |
| 12 | + @doc = Document.new(self) |
| 13 | + yield @doc |
| 14 | + add_content_types |
| 15 | + add_workbook_part |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + def add_workbook_part |
| 20 | + @zip.get_output_stream "xl/workbook.xml" do |f| |
| 21 | + f.puts <<-ends |
| 22 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 23 | +<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> |
| 24 | +<sheets> |
| 25 | +ends |
| 26 | + @doc.sheets.each_with_index do |sheet, ndx| |
| 27 | + f.puts "<sheet name=\"#{sheet.name}\" sheetId=\"#{ndx + 1}\" r:id=\"rId#{ndx + 2}\"/>" |
| 28 | + end |
| 29 | + f.puts "</sheets></workbook>" |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + def add_worksheets_directory |
| 34 | + @zip.mkdir "xl" |
| 35 | + @zip.mkdir "xl/worksheets" |
| 36 | + end |
| 37 | + |
| 38 | + def open_stream_for_sheet sheet_name |
| 39 | + @zip.get_output_stream "xl/worksheets/#{sheet_name}.xml" do |f| |
| 40 | + yield f |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + def add_content_types |
| 45 | + @zip.get_output_stream "[Content_Types].xml" do |f| |
| 46 | + f.puts '<?xml version="1.0" encoding="UTF-8"?>' |
| 47 | + f.puts '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' |
| 48 | + f.puts <<-ends |
| 49 | + <Override PartName="/_rels/.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> |
| 50 | + <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/> |
| 51 | + <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/> |
| 52 | + <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/> |
| 53 | +ends |
| 54 | + if @doc.has_shared_strings? |
| 55 | + f.puts '<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>' |
| 56 | + end |
| 57 | + @doc.sheets.each do |sheet| |
| 58 | + f.puts "<Override PartName=\"/xl/worksheets/#{sheet.name}.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml\"/>" |
| 59 | + end |
| 60 | + if @doc.has_styles? |
| 61 | + f.puts '<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>' |
| 62 | + end |
| 63 | + f.puts "</Types>" |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def add_relationship_part |
| 68 | + @zip.mkdir "_rels" |
| 69 | + @zip.get_output_stream "_rels/.rels" do |f| |
| 70 | + f.puts <<-ends |
| 71 | +<?xml version="1.0" encoding="UTF-8"?> |
| 72 | +<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> |
| 73 | + <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/> |
| 74 | + <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/> |
| 75 | + <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/> |
| 76 | +</Relationships> |
| 77 | +ends |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + def add_doc_props |
| 82 | + @zip.mkdir "docProps" |
| 83 | + @zip.get_output_stream "docProps/core.xml" do |f| |
| 84 | + f.puts <<-ends |
| 85 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 86 | +<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"> |
| 87 | + <dcterms:created xsi:type="dcterms:W3CDTF">2010-07-20T14:30:58.00Z</dcterms:created> |
| 88 | + <cp:revision>0</cp:revision> |
| 89 | +</cp:coreProperties> |
| 90 | +ends |
| 91 | + end |
| 92 | + @zip.get_output_stream "docProps/app.xml" do |f| |
| 93 | + f.puts <<-ends |
| 94 | +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 95 | +<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"> |
| 96 | + <TotalTime>0</TotalTime> |
| 97 | +</Properties> |
| 98 | +ends |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | +end |
| 103 | + |
| 104 | +end |
| 105 | + |
| 106 | + |
0 commit comments