Method: Axlsx::Package#serialize

Defined in:
lib/axlsx/package.rb

#serialize(output, options = {}, secondary_options = nil) ⇒ Boolean

Note:

A tremendous amount of effort has gone into ensuring that you cannot create invalid xlsx documents. options[:confirm_valid] should be used in the rare case that you cannot open the serialized file.

Serialize your workbook to disk as an xlsx document.

Examples:

# This is how easy it is to create a valid xlsx file. Of course you might want to add a sheet or two, and maybe some data, styles and charts.
# Take a look at the README for an example of how to do it!

#serialize to a file
p = Axlsx::Package.new
# ......add cool stuff to your workbook......
p.serialize("example.xlsx")

# Serialize to a file, using a system zip binary
p.serialize("example.xlsx", zip_command: "zip", confirm_valid: false)
p.serialize("example.xlsx", zip_command: "/path/to/zip")
p.serialize("example.xlsx", zip_command: "zip -1")

# Serialize to a stream
s = p.to_stream()
File.open('example_streamed.xlsx', 'wb') { |f| f.write(s.read) }

Parameters:

  • output (String)

    The name of the file you want to serialize your package to

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :confirm_valid (Boolean)

    Validate the package prior to serialization.

  • :zip_command (String)

    When nil, #serialize with RubyZip to zip the XLSX file contents. When a String, the provided zip command (e.g., "zip") is used to zip the file contents (may be faster for large files)

  • :password (String)

    When specified, the serialized packaged will be encrypted with the password. Requires ooxml_crypt gem.

Returns:

  • (Boolean)

    False if confirm_valid and validation errors exist. True if the package was serialized

See Also:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/axlsx/package.rb', line 108

def serialize(output, options = {}, secondary_options = nil)
  unless workbook.styles_applied
    workbook.apply_styles
  end

  confirm_valid, zip_command, password = parse_serialize_options(options, secondary_options)
  return false unless !confirm_valid || validate.empty?

  zip_provider = if zip_command
                   ZipCommand.new(zip_command)
                 else
                   BufferedZipOutputStream
                 end
  Relationship.initialize_ids_cache
  zip_provider.open(output) do |zip|
    write_parts(zip)
  end

  if password && !password.empty?
    require_ooxml_crypt!
    OoxmlCrypt.encrypt_file(output, password, output)
  end

  true
ensure
  Relationship.clear_ids_cache
end