Method: Axlsx::Package#serialize

Defined in:
lib/axlsx/package.rb

#serialize(output, confirm_valid = false) ⇒ Boolean

Note:

A tremendous amount of effort has gone into ensuring that you cannot create invalid xlsx documents. 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 stream
s = p.to_stream()
File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) }

Parameters:

  • output (String)

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

  • confirm_valid (Boolean) (defaults to: false)

    Validate the package prior to serialization.

Returns:

  • (Boolean)

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

See Also:



101
102
103
104
105
106
107
108
# File 'lib/axlsx/package.rb', line 101

def serialize(output, confirm_valid=false)
  return false unless !confirm_valid || self.validate.empty?
  Relationship.clear_cached_instances
  Zip::OutputStream.open(output) do |zip|
    write_parts(zip)
  end
  true
end