Getting started
Rust_xlsxwriter is a library and doesn't need to be installed. All that is
required is to add it the Cargo.toml
file for your project. To demonstrate we
will start with a small sample application.
Create a sample application
Create a new rust command-line application as follows:
cargo new hello-xlsx
This will create a directory like the following:
hello-xlsx/
├── Cargo.toml
└── src
└── main.rs
Edit the Cargo.toml file and add the following rust_xlsxwriter
dependency
so the file looks like below. Note, rust_xlsxwriter
adds new features
regularly do make sure you use the latest version.
[package]
name = "hello-xlsx"
version = "0.1.0"
edition = "2021"
[dependencies]
rust_xlsxwriter = "0.26.0"
Modify the main.rs file so it looks like this:
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2023, John McNamara, jmcnamara@cpan.org
//! Create a simple Hello World style Excel spreadsheet using the
//! rust_xlsxwriter library.
use rust_xlsxwriter::{Workbook, XlsxError};
fn main() -> Result<(), XlsxError> {
// Create a new Excel file object.
let mut workbook = Workbook::new();
// Add a worksheet to the workbook.
let worksheet = workbook.add_worksheet();
// Write a string to cell (0, 0) = A1.
worksheet.write_string(0, 0, "Hello")?;
// Write a number to cell (1, 0) = A2.
worksheet.write_number(1, 0, 12345)?;
// Save the file to disk.
workbook.save("hello.xlsx")?;
Ok(())
}
The run the application as follows:
cargo run
This will create an output file called hello.xlsx
which should look
something like this: