pub struct Workbook { /* private fields */ }
Expand description

The Workbook is the main object exposed by the libxlsxwriter library. It represents the entire spreadsheet as you see it in Excel and internally it represents the Excel file as it is written on disk.

use xlsxwriter::prelude::*;
fn main() -> Result<(), XlsxError> {
    let workbook = Workbook::new("test-workbook.xlsx")?;
    let mut worksheet = workbook.add_worksheet(None)?;
    worksheet.write_string(0, 0, "Hello Excel", None)?;
    workbook.close()
}

Implementations§

source§

impl Workbook

source

pub fn new(filename: &str) -> Result<Workbook, XlsxError>

This function is used to create a new Excel workbook with a given filename. When specifying a filename it is recommended that you use an .xlsx extension or Excel will generate a warning when opening the file.

source

pub fn new_with_options( filename: &str, constant_memory: bool, tmpdir: Option<&str>, use_zip64: bool ) -> Result<Workbook, XlsxError>

This function is the same as the Workbook::new() constructor but allows additional options to be set.

let workbook = Workbook::new_with_options("test-workbook_with_options.xlsx", true, Some("target"), true)?;
let mut worksheet = workbook.add_worksheet(None)?;
worksheet.write_string(0, 0, "Hello Excel", None)?;
workbook.close()

The options that can be set are:

  • constant_memory: This option reduces the amount of data stored in memory so that large files can be written efficiently. This option is off by default. See the note below for limitations when this mode is on.
  • tmpdir: libxlsxwriter stores workbook data in temporary files prior to assembling the final XLSX file. The temporary files are created in the system’s temp directory. If the default temporary directory isn’t accessible to your application, or doesn’t contain enough space, you can specify an alternative location using the tmpdir option.
  • use_zip64: Make the zip library use ZIP64 extensions when writing very large xlsx files to allow the zip container, or individual XML files within it, to be greater than 4 GB. See ZIP64 on Wikipedia for more information. This option is off by default.
Note

In constant_memory mode each row of in-memory data is written to disk and then freed when a new row is started via one of the Worksheet::write_*() functions. Therefore, once this option is active data should be written in sequential row by row order. For this reason Worksheet::merge_range() and some other row based functionality doesn’t work in this mode. See Constant Memory Mode for more details.

Also, in constant_memory mode the library uses temp file storage for worksheet data. This can lead to an issue on OSes that map the /tmp directory into memory since it is possible to consume the “system” memory even though the “process” memory remains constant. In these cases you should use an alternative temp file location by using the tmpdir option shown above. See Constant memory mode and the /tmp directory for more details.

source

pub fn add_worksheet<'a>( &'a self, sheet_name: Option<&str> ) -> Result<Worksheet<'a>, XlsxError>

source

pub fn get_worksheet<'a>( &'a self, sheet_name: &str ) -> Result<Option<Worksheet<'a>>, XlsxError>

This function returns a Worksheet object reference based on its name.

source

pub fn add_format(&self) -> Format

👎Deprecated since 0.6.0: Replaced with Format::new()

Create new format struct.

This function available only for compatibility. Please use Format::new to create new Format object.

source

pub fn add_chart(&self, chart_type: ChartType) -> Chart<'_>

Workbook::add_chart function creates a new chart object that can be added to a worksheet. Available chart types are defined in ChartType.

source

pub fn define_name(&self, name: &str, formula: &str) -> Result<(), XlsxError>

This function is used to defined a name that can be used to represent a value, a single cell or a range of cells in a workbook: These defined names can then be used in formulas:

let workbook = Workbook::new("test-workbook-define_name.xlsx")?;
let mut worksheet = workbook.add_worksheet(None)?;
workbook.define_name("Exchange_rate", "=0.95");
worksheet.write_formula(0, 0, "=Exchange_rate", None);
source

pub fn close(self) -> Result<(), XlsxError>

The Workbook::close function closes a Workbook object, writes the Excel file to disk, frees any memory allocated internally to the Workbook and frees the object itself.

Trait Implementations§

source§

impl Drop for Workbook

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.