Struct xlsxwriter::workbook::Workbook
source · 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
impl Workbook
sourcepub fn new(filename: &str) -> Result<Workbook, XlsxError>
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.
sourcepub fn new_with_options(
filename: &str,
constant_memory: bool,
tmpdir: Option<&str>,
use_zip64: bool
) -> Result<Workbook, XlsxError>
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.
pub fn add_worksheet<'a>( &'a self, sheet_name: Option<&str> ) -> Result<Worksheet<'a>, XlsxError>
sourcepub fn get_worksheet<'a>(
&'a self,
sheet_name: &str
) -> Result<Option<Worksheet<'a>>, XlsxError>
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.
sourcepub fn add_format(&self) -> Format
👎Deprecated since 0.6.0: Replaced with Format::new()
pub fn add_format(&self) -> Format
Create new format struct.
This function available only for compatibility. Please use Format::new
to create new Format object.
sourcepub fn add_chart(&self, chart_type: ChartType) -> Chart<'_>
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
.
sourcepub fn define_name(&self, name: &str, formula: &str) -> Result<(), XlsxError>
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);