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
Change to the new hello-xlsx
directory and add the rust_xlsxwriter
dependency:
$ cd hello-xlsx
$ cargo add rust_xlsxwriter
Modify the src/main.rs
file so it looks like this:
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2024, 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(0, 0, "Hello")?;
// Write a number to cell (1, 0) = A2.
worksheet.write(1, 0, 12345)?;
// Save the file to disk.
workbook.save("hello.xlsx")?;
Ok(())
}
Then run the application as follows:
$ cargo run
This will create an output file called hello.xlsx
which should look
something like this: