1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2025, John McNamara, jmcnamara@cpan.org
//! An example of writing a Polar Rust dataframe to an Excel file. This
//! demonstrates setting the format for the header row.
use polars::prelude::*;
use polars_excel_writer::PolarsExcelWriter;
use rust_xlsxwriter::Format;
fn main() -> PolarsResult<()> {
// Create a sample dataframe for the example.
let df: DataFrame = df!(
"East" => &[1, 1, 1, 1],
"West" => &[2, 2, 2, 2],
"North" => &[3, 3, 3, 3],
"South" => &[4, 4, 4, 4],
)?;
// Create a new excel writer.
let mut excel_writer = PolarsExcelWriter::new();
// Create and set the header format.
let header_format = Format::new()
.set_background_color("#C6EFCE")
.set_font_color("#006100")
.set_bold();
excel_writer.set_header_format(&header_format);
// Write the dataframe to Excel.
excel_writer.write_dataframe(&df)?;
// Save the file to disk.
excel_writer.save("dataframe.xlsx")?;
Ok(())
}