xlsxtream 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +77 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/xlsxtream/io/directory.rb +26 -0
- data/lib/xlsxtream/io/rubyzip.rb +64 -0
- data/lib/xlsxtream/io/stream.rb +24 -0
- data/lib/xlsxtream/row.rb +51 -0
- data/lib/xlsxtream/shared_string_table.rb +17 -0
- data/lib/xlsxtream/version.rb +3 -0
- data/lib/xlsxtream/workbook.rb +175 -0
- data/lib/xlsxtream/worksheet.rb +41 -0
- data/lib/xlsxtream/xml.rb +37 -0
- data/lib/xlsxtream.rb +8 -0
- data/xlsxtream.gemspec +28 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5e0567238c9913e1352c5b997232f15834684050
|
4
|
+
data.tar.gz: 2148c939d2ea8da99e490daf2065bf4ebbe5e266
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 07a53ebc39c5a16d108c970fd69f9037b2ee52305f9e097613791acad219fe44c6d9b59d666eea2d8e6d4b6320c03d7078c428d5a109abddf5122c080a74ddd6
|
7
|
+
data.tar.gz: d369b2bdd9aad37c3c48f7a9eed654016784b3f4774f2bfd18ae6cd9b43ced4fb82b8f45add2fdb339f27ec37b86d010215f86e262dc03c177b916b1a74715ea
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Felix Bünemann
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Xlsxtream
|
2
|
+
|
3
|
+
Xlsxtream is a streaming writer for XLSX spreadsheets. It supports multiple worksheets and optional string deduplication via a shared string table (SST). Its purpose is to replace CSV for large exports, because using CSV in Excel is very buggy and error prone. It's very efficient and can quickly write millions of rows with low memory usage.
|
4
|
+
|
5
|
+
Xlsxtream does not support formatting, charts, comments and a myriad of other [OOXML](https://en.wikipedia.org/wiki/Office_Open_XML) features. If you are looking for a fully featured solution take a look at [axslx](https://github.com/randym/axlsx).
|
6
|
+
|
7
|
+
Xlsxtream supports writing to files or IO-like objects, data is flushed as the ZIP compressor sees fit.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'xlsxtream'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install xlsxtream
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# Creates a new workbook and closes it at the end of the block.
|
29
|
+
Xlsxtream::Workbook.open("foo.xlsx") do |xlsx|
|
30
|
+
xlsx.write_sheet "Sheet1" do |sheet|
|
31
|
+
# Date, Time, DateTime, Fixnum & Float are properly mapped
|
32
|
+
sheet << [Date.today, "hello", "world", 42, 3.14159265359]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
io = StringIO.new('')
|
37
|
+
xlsx = Xlsxtream::Workbook.new(io)
|
38
|
+
xlsx.write_worksheet "Sheet1" do |sheet|
|
39
|
+
# Number of columns doesn't have to match
|
40
|
+
sheet << %w[first row]
|
41
|
+
sheet << %w[second row with more colums]
|
42
|
+
end
|
43
|
+
# Write multiple worksheets with custom names:
|
44
|
+
xlsx.write_worksheet "Foo & Bar" do |sheet|
|
45
|
+
sheet.add_row ["Timestamp", "Comment"]
|
46
|
+
sheet.add_row [Time.now, "Foo"]
|
47
|
+
sheet.add_row [Time.now, "Bar"]
|
48
|
+
end
|
49
|
+
# If you have highly repetitive data, you can enable Shared
|
50
|
+
# String Tables (SST) for the workbook or a single worksheet.
|
51
|
+
# The SST has to be kept in memory, so don't use it if you
|
52
|
+
# have a huge amount of rows or a little duplication of content
|
53
|
+
# accros cells. A single SST is used across the whole workbook.
|
54
|
+
xlsx.write_worksheet("SST", use_shared_strings: true) do |sheet|
|
55
|
+
sheet << %w[the same old story]
|
56
|
+
sheet << %w[the old same story]
|
57
|
+
sheet << %w[old, the same story]
|
58
|
+
end
|
59
|
+
# Writes metadata and ZIP archive central directory.
|
60
|
+
xlsx.close
|
61
|
+
```
|
62
|
+
|
63
|
+
## Development
|
64
|
+
|
65
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
66
|
+
|
67
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/felixbuenemann/xlsxtream.
|
72
|
+
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
77
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "xlsxtream"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "pathname"
|
2
|
+
|
3
|
+
module Xlsxtream
|
4
|
+
module IO
|
5
|
+
class Directory
|
6
|
+
def initialize(path)
|
7
|
+
@path = Pathname(path)
|
8
|
+
end
|
9
|
+
|
10
|
+
def <<(data)
|
11
|
+
@file << data
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_file(path)
|
15
|
+
close
|
16
|
+
file_path = @path + path
|
17
|
+
file_path.parent.mkpath
|
18
|
+
@file = file_path.open("wb")
|
19
|
+
end
|
20
|
+
|
21
|
+
def close
|
22
|
+
@file.close if @file.respond_to? :close
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "zip"
|
2
|
+
|
3
|
+
module Xlsxtream
|
4
|
+
module IO
|
5
|
+
class RubyZip
|
6
|
+
def initialize(path_or_io)
|
7
|
+
stream = path_or_io.respond_to? :reopen
|
8
|
+
@zos = UnbufferedZipOutputStream.new(path_or_io, stream)
|
9
|
+
end
|
10
|
+
|
11
|
+
def <<(data)
|
12
|
+
@zos << data
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_file(path)
|
16
|
+
@zos.put_next_entry path
|
17
|
+
end
|
18
|
+
|
19
|
+
def close
|
20
|
+
@zos.close
|
21
|
+
end
|
22
|
+
|
23
|
+
# Extend get_compressor to hook our custom deflater.
|
24
|
+
class UnbufferedZipOutputStream < ::Zip::OutputStream
|
25
|
+
private def get_compressor(entry, level)
|
26
|
+
case entry.compression_method
|
27
|
+
when ::Zip::Entry::DEFLATED then
|
28
|
+
StreamingDeflater.new(@output_stream, level, @encrypter)
|
29
|
+
else
|
30
|
+
super
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# RubyZip's Deflater buffers to a StringIO until finish is called.
|
36
|
+
# This StreamingDeflater writes out chunks during compression.
|
37
|
+
class StreamingDeflater < ::Zip::Compressor
|
38
|
+
def initialize(output_stream, level = Zip.default_compression, encrypter = NullEncrypter.new)
|
39
|
+
super()
|
40
|
+
@output_stream = output_stream
|
41
|
+
@zlib_deflater = ::Zlib::Deflate.new(level, -::Zlib::MAX_WBITS)
|
42
|
+
@size = 0
|
43
|
+
@crc = ::Zlib.crc32
|
44
|
+
unless encrypter.is_a? ::Zip::NullEncrypter
|
45
|
+
raise ::Zip::Error, 'StreamingDeflater does not support encryption'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def <<(data)
|
50
|
+
val = data.to_s
|
51
|
+
@crc = Zlib.crc32(val, @crc)
|
52
|
+
@size += val.bytesize
|
53
|
+
@output_stream << @zlib_deflater.deflate(data)
|
54
|
+
end
|
55
|
+
|
56
|
+
def finish
|
57
|
+
@output_stream << @zlib_deflater.finish until @zlib_deflater.finished?
|
58
|
+
end
|
59
|
+
|
60
|
+
attr_reader :size, :crc
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Xlsxtream
|
2
|
+
module IO
|
3
|
+
class Stream
|
4
|
+
def initialize(stream)
|
5
|
+
@stream = stream
|
6
|
+
end
|
7
|
+
|
8
|
+
def <<(data)
|
9
|
+
@stream << data
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_file(path)
|
13
|
+
close
|
14
|
+
@path = path
|
15
|
+
@stream << "#@path\n"
|
16
|
+
end
|
17
|
+
|
18
|
+
def close
|
19
|
+
@stream << "\n" if @path
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "xlsxtream/xml"
|
3
|
+
|
4
|
+
module Xlsxtream
|
5
|
+
class Row
|
6
|
+
def initialize(row, rownum, sst = nil)
|
7
|
+
@row = row
|
8
|
+
@rownum = rownum
|
9
|
+
@sst = sst
|
10
|
+
@encoding = Encoding.find("UTF-8")
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_xml
|
14
|
+
column = 'A'
|
15
|
+
@row.reduce(%'<row r="#@rownum">') do |xml, value|
|
16
|
+
cid = "#{column}#@rownum"
|
17
|
+
column.next!
|
18
|
+
xml << case value
|
19
|
+
when Fixnum, Float
|
20
|
+
%'<c r="#{cid}" t="n"><v>#{value}</v></c>'
|
21
|
+
when Date, Time, DateTime
|
22
|
+
style = value.is_a?(Date) ? 1 : 2
|
23
|
+
%'<c r="#{cid}" s="#{style}"><v>#{time_to_oa_date value}</v></c>'
|
24
|
+
else
|
25
|
+
value = value.to_s unless value.is_a? String
|
26
|
+
if value.empty?
|
27
|
+
''
|
28
|
+
else
|
29
|
+
value = value.encode(@encoding) if value.encoding != @encoding
|
30
|
+
if @sst
|
31
|
+
%'<c r="#{cid}" t="s"><v>#{@sst[value]}</v></c>'
|
32
|
+
else
|
33
|
+
%'<c r="#{cid}" t="inlineStr"><is><t>#{XML.escape_value value}</t></is></c>'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end << '</row>'
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# Converts Time objects to OLE Automation Date
|
43
|
+
def time_to_oa_date(time)
|
44
|
+
time = time.respond_to?(:to_time) ? time.to_time : time
|
45
|
+
# Local dates are stored as UTC by truncating the offset:
|
46
|
+
# 1970-01-01 00:00:00 +0200 => 1970-01-01 00:00:00 UTC
|
47
|
+
# This is done because SpreadsheetML is not timezone aware.
|
48
|
+
(time + time.utc_offset).utc.to_f / 24 / 3600 + 25569
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Xlsxtream
|
2
|
+
class SharedStringTable < Hash
|
3
|
+
def initialize
|
4
|
+
@references = 0
|
5
|
+
super { |hash, string| hash[string] = hash.size }
|
6
|
+
end
|
7
|
+
|
8
|
+
def [](string)
|
9
|
+
@references += 1
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def references
|
14
|
+
@references
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "stringio"
|
3
|
+
require "xlsxtream/xml"
|
4
|
+
require "xlsxtream/shared_string_table"
|
5
|
+
require "xlsxtream/workbook"
|
6
|
+
require "xlsxtream/io/rubyzip"
|
7
|
+
require "xlsxtream/io/directory"
|
8
|
+
require "xlsxtream/io/stream"
|
9
|
+
|
10
|
+
module Xlsxtream
|
11
|
+
class Workbook
|
12
|
+
|
13
|
+
class << self
|
14
|
+
|
15
|
+
def open(data = nil, options = {})
|
16
|
+
workbook = new(data, options)
|
17
|
+
if block_given?
|
18
|
+
begin
|
19
|
+
yield workbook
|
20
|
+
ensure
|
21
|
+
workbook.close
|
22
|
+
end
|
23
|
+
else
|
24
|
+
workbook
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(data = nil, options = {})
|
31
|
+
@options = options
|
32
|
+
io_wrapper = options[:io_wrapper] || IO::RubyZip
|
33
|
+
@io = io_wrapper.new(data || StringIO.new)
|
34
|
+
@sst = SharedStringTable.new
|
35
|
+
@worksheets = Hash.new { |hash, name| hash[name] = hash.size + 1 }
|
36
|
+
end
|
37
|
+
|
38
|
+
def write_worksheet(name = nil, options = {})
|
39
|
+
use_sst = options.fetch(:use_shared_strings, @options[:use_shared_strings])
|
40
|
+
name ||= "Sheet#{@worksheets.size + 1}"
|
41
|
+
sheet_id = @worksheets[name]
|
42
|
+
@io.add_file "xl/worksheets/sheet#{sheet_id}.xml"
|
43
|
+
worksheet = Worksheet.new(@io, use_sst ? @sst : nil)
|
44
|
+
yield worksheet if block_given?
|
45
|
+
worksheet.close
|
46
|
+
nil
|
47
|
+
end
|
48
|
+
alias_method :add_worksheet, :write_worksheet
|
49
|
+
|
50
|
+
def close
|
51
|
+
write_workbook
|
52
|
+
write_styles
|
53
|
+
write_sst unless @sst.empty?
|
54
|
+
write_workbook_rels
|
55
|
+
write_root_rels
|
56
|
+
write_content_types
|
57
|
+
@io.close
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def write_root_rels
|
64
|
+
@io.add_file "_rels/.rels"
|
65
|
+
@io << XML.header
|
66
|
+
@io << XML.strip(<<-XML)
|
67
|
+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
68
|
+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
|
69
|
+
</Relationships>
|
70
|
+
XML
|
71
|
+
end
|
72
|
+
|
73
|
+
def write_workbook
|
74
|
+
rid = "rId0"
|
75
|
+
@io.add_file "xl/workbook.xml"
|
76
|
+
@io << XML.header
|
77
|
+
@io << XML.strip(<<-XML)
|
78
|
+
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
|
79
|
+
<workbookPr date1904="false"/>
|
80
|
+
<sheets>
|
81
|
+
XML
|
82
|
+
@worksheets.each do |name, sheet_id|
|
83
|
+
@io << %'<sheet name="#{XML.escape_attr name}" sheetId="#{sheet_id}" r:id="#{rid.next!}"/>'
|
84
|
+
end
|
85
|
+
@io << XML.strip(<<-XML)
|
86
|
+
</sheets>
|
87
|
+
</workbook>
|
88
|
+
XML
|
89
|
+
end
|
90
|
+
|
91
|
+
def write_styles
|
92
|
+
@io.add_file "xl/styles.xml"
|
93
|
+
@io << XML.header
|
94
|
+
@io << XML.strip(<<-XML)
|
95
|
+
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
96
|
+
<numFmts count="2">
|
97
|
+
<numFmt numFmtId="164" formatCode="yyyy\\-mm\\-dd"/>
|
98
|
+
<numFmt numFmtId="165" formatCode="yyyy\\-mm\\-dd hh:mm:ss"/>
|
99
|
+
</numFmts>
|
100
|
+
<fonts count="1">
|
101
|
+
<font>
|
102
|
+
<sz val="12"/>
|
103
|
+
<name val="Calibri"/>
|
104
|
+
<family val="2"/>
|
105
|
+
</font>
|
106
|
+
</fonts>
|
107
|
+
<fills count="2">
|
108
|
+
<fill>
|
109
|
+
<patternFill patternType="none"/>
|
110
|
+
</fill>
|
111
|
+
<fill>
|
112
|
+
<patternFill patternType="gray125"/>
|
113
|
+
</fill>
|
114
|
+
</fills>
|
115
|
+
<borders count="1">
|
116
|
+
<border/>
|
117
|
+
</borders>
|
118
|
+
<cellStyleXfs count="1">
|
119
|
+
<xf numFmtId="0" fontId="0" fillId="0" borderId="0"/>
|
120
|
+
</cellStyleXfs>
|
121
|
+
<cellXfs count="3">
|
122
|
+
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
|
123
|
+
<xf numFmtId="164" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
|
124
|
+
<xf numFmtId="165" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
|
125
|
+
</cellXfs>
|
126
|
+
<cellStyles count="1">
|
127
|
+
<cellStyle name="Normal" xfId="0" builtinId="0"/>
|
128
|
+
</cellStyles>
|
129
|
+
<dxfs count="0"/>
|
130
|
+
<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/>
|
131
|
+
</styleSheet>
|
132
|
+
XML
|
133
|
+
end
|
134
|
+
|
135
|
+
def write_sst
|
136
|
+
@io.add_file "xl/sharedStrings.xml"
|
137
|
+
@io << XML.header
|
138
|
+
@io << %'<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="#{@sst.references}" uniqueCount="#{@sst.size}">'
|
139
|
+
@sst.each_key do |string|
|
140
|
+
@io << "<si><t>#{XML.escape_value string}</t></si>"
|
141
|
+
end
|
142
|
+
@io << '</sst>'
|
143
|
+
end
|
144
|
+
|
145
|
+
def write_workbook_rels
|
146
|
+
rid = "rId0"
|
147
|
+
@io.add_file "xl/_rels/workbook.xml.rels"
|
148
|
+
@io << XML.header
|
149
|
+
@io << '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
|
150
|
+
@worksheets.each do |name, sheet_id|
|
151
|
+
@io << %'<Relationship Id="#{rid.next!}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet#{sheet_id}.xml"/>'
|
152
|
+
end
|
153
|
+
@io << %'<Relationship Id="#{rid.next!}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>'
|
154
|
+
@io << %'<Relationship Id="#{rid.next!}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>' unless @sst.empty?
|
155
|
+
@io << '</Relationships>'
|
156
|
+
end
|
157
|
+
|
158
|
+
def write_content_types
|
159
|
+
@io.add_file "[Content_Types].xml"
|
160
|
+
@io << XML.header
|
161
|
+
@io << XML.strip(<<-XML)
|
162
|
+
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
|
163
|
+
<Default Extension="xml" ContentType="application/xml"/>
|
164
|
+
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
|
165
|
+
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
|
166
|
+
<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>
|
167
|
+
XML
|
168
|
+
@io << '<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>' unless @sst.empty?
|
169
|
+
@worksheets.each_value do |sheet_id|
|
170
|
+
@io << %'<Override PartName="/xl/worksheets/sheet#{sheet_id}.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>'
|
171
|
+
end
|
172
|
+
@io << '</Types>'
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "xlsxtream/xml"
|
3
|
+
require "xlsxtream/row"
|
4
|
+
|
5
|
+
module Xlsxtream
|
6
|
+
class Worksheet
|
7
|
+
def initialize(io, sst = nil)
|
8
|
+
@io = io
|
9
|
+
@rownum = 1
|
10
|
+
@sst = sst
|
11
|
+
write_header
|
12
|
+
end
|
13
|
+
|
14
|
+
def <<(row)
|
15
|
+
@io << Row.new(row, @rownum, @sst).to_xml
|
16
|
+
@rownum += 1
|
17
|
+
end
|
18
|
+
alias_method :add_row, :<<
|
19
|
+
|
20
|
+
def close
|
21
|
+
write_footer
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def write_header
|
27
|
+
@io << XML.header
|
28
|
+
@io << XML.strip(<<-XML)
|
29
|
+
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
30
|
+
<sheetData>
|
31
|
+
XML
|
32
|
+
end
|
33
|
+
|
34
|
+
def write_footer
|
35
|
+
@io << XML.strip(<<-XML)
|
36
|
+
</sheetData>
|
37
|
+
</worksheet>
|
38
|
+
XML
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Xlsxtream
|
2
|
+
module XML
|
3
|
+
XML_ESCAPES = {
|
4
|
+
'&' => '&',
|
5
|
+
'"' => '"',
|
6
|
+
'<' => '<',
|
7
|
+
'>' => '>',
|
8
|
+
}.freeze
|
9
|
+
|
10
|
+
XML_DECLARATION = %'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n'.freeze
|
11
|
+
|
12
|
+
WS_AROUND_TAGS = /(?<=>)\s+|\s+(?=<)/.freeze
|
13
|
+
|
14
|
+
UNSAFE_ATTR_CHARS = /[&"<>]/.freeze
|
15
|
+
UNSAFE_VALUE_CHARS = /[&<>]/.freeze
|
16
|
+
|
17
|
+
class << self
|
18
|
+
|
19
|
+
def header
|
20
|
+
XML_DECLARATION
|
21
|
+
end
|
22
|
+
|
23
|
+
def strip(xml)
|
24
|
+
xml.gsub(WS_AROUND_TAGS, ''.freeze)
|
25
|
+
end
|
26
|
+
|
27
|
+
def escape_attr(string)
|
28
|
+
string.gsub(UNSAFE_ATTR_CHARS, XML_ESCAPES)
|
29
|
+
end
|
30
|
+
|
31
|
+
def escape_value(string)
|
32
|
+
string.gsub(UNSAFE_VALUE_CHARS, XML_ESCAPES)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/xlsxtream.rb
ADDED
data/xlsxtream.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'xlsxtream/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "xlsxtream"
|
8
|
+
spec.version = Xlsxtream::VERSION
|
9
|
+
spec.authors = ["Felix Bünemann"]
|
10
|
+
spec.email = ["felix.buenemann@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Xlsxtream is a streaming XLSX spreadsheet writer}
|
13
|
+
spec.description = %q{This gem allows very efficient writing of CSV style data to XLSX with multiple worksheets.}
|
14
|
+
spec.homepage = "https://github.com/felixbuenemann/xlsxtream"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "rubyzip", ">= 1.0.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest"
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xlsxtream
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Felix Bünemann
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rubyzip
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: This gem allows very efficient writing of CSV style data to XLSX with
|
84
|
+
multiple worksheets.
|
85
|
+
email:
|
86
|
+
- felix.buenemann@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/xlsxtream.rb
|
100
|
+
- lib/xlsxtream/io/directory.rb
|
101
|
+
- lib/xlsxtream/io/rubyzip.rb
|
102
|
+
- lib/xlsxtream/io/stream.rb
|
103
|
+
- lib/xlsxtream/row.rb
|
104
|
+
- lib/xlsxtream/shared_string_table.rb
|
105
|
+
- lib/xlsxtream/version.rb
|
106
|
+
- lib/xlsxtream/workbook.rb
|
107
|
+
- lib/xlsxtream/worksheet.rb
|
108
|
+
- lib/xlsxtream/xml.rb
|
109
|
+
- xlsxtream.gemspec
|
110
|
+
homepage: https://github.com/felixbuenemann/xlsxtream
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.5.1
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Xlsxtream is a streaming XLSX spreadsheet writer
|
134
|
+
test_files: []
|