jmcnamara / rust_xlsxwriter

A Rust library for creating Excel XLSX files.
https://crates.io/crates/rust_xlsxwriter
Apache License 2.0
316 stars 25 forks source link

Format a table column header #48

Closed Xydez closed 1 year ago

Xydez commented 1 year ago

Feature Request

Using xlsxwriter, it's possible to use the following field to set header formatting:

// xlsxwriter
TableColumn {
    header_format: Option<xlsxwriter::Format>,
    // ...
}

It would be nice to be able to do something similar with this library, e.g.:

// rust_xlsxwriter (hypothetical)
TableColumn::new()
    .set_header_format(rust_xlsxwriter::Format)

Is it already possible to accomplish this in another way or is this something that will be implemented?

Best regards

jmcnamara commented 1 year ago

I'll definitely add that. Probably in the next release. I've already implemented the column format option and will add the header format soon.

jmcnamara commented 1 year ago

Is it already possible to accomplish this in another way

You can workaround it by overwriting the column headers with the same text but different formatting. Like this:

use rust_xlsxwriter::{Color, Format, Table, TableColumn, Workbook, XlsxError};

fn main() -> Result<(), XlsxError> {
    // Create a new Excel file object.
    let mut workbook = Workbook::new();

    // Some sample data for the table.
    let items = ["Apples", "Pears", "Bananas", "Oranges"];
    let data = [
        [10000, 5000, 8000, 6000],
        [2000, 3000, 4000, 5000],
        [6000, 6000, 6500, 6000],
        [500, 300, 200, 700],
    ];

    // Add a worksheet to the workbook.
    let worksheet = workbook.add_worksheet();

    // Set the columns widths for clarity.
    for col_num in 1..=6u16 {
        worksheet.set_column_width(col_num, 12)?;
    }

    // Write the table data.
    worksheet.write_column(3, 1, items)?;
    worksheet.write_row_matrix(3, 2, data)?;

    // Create and configure a new table.
    let mut table = Table::new();
    let columns = vec![
        TableColumn::new().set_header("Product"),
        TableColumn::new().set_header("Quarter 1"),
        TableColumn::new().set_header("Quarter 2"),
        TableColumn::new().set_header("Quarter 3"),
        TableColumn::new().set_header("Quarter 4"),
    ];
    table.set_columns(&columns);

    // Add the table to the worksheet.
    worksheet.add_table(2, 1, 6, 5, &table)?;

    // Overwrite the Header with formatted text.
    let header = Format::new()
        .set_bold()
        .set_italic()
        .set_font_color(Color::Yellow);

    worksheet.write_with_format(2, 1, "Product", &header)?;
    worksheet.write_with_format(2, 2, "Quarter 1", &header)?;
    worksheet.write_with_format(2, 3, "Quarter 2", &header)?;
    worksheet.write_with_format(2, 4, "Quarter 3", &header)?;
    worksheet.write_with_format(2, 5, "Quarter 4", &header)?;

    // Save the file to disk.
    workbook.save("tables.xlsx")?;

    Ok(())
}

Output:

screenshot 1

It isn't 100% the same as Excel or the final solution but it will give you a workaround.

jmcnamara commented 1 year ago

This feature is now available on main and should be available in v0.43.0 in the next 1-2 days.

Example:


use rust_xlsxwriter::{Color, Format, Table, TableColumn, Workbook, XlsxError};

fn main() -> Result<(), XlsxError> {
    // Create a new Excel file object.
    let mut workbook = Workbook::new();

    // Some sample data for the table.
    let items = ["Apples", "Pears", "Bananas", "Oranges"];
    let data = [
        [10000, 5000, 8000, 6000],
        [2000, 3000, 4000, 5000],
        [6000, 6000, 6500, 6000],
        [500, 300, 200, 700],
    ];

    // Add a worksheet to the workbook.
    let worksheet = workbook.add_worksheet();

    // Set the columns widths for clarity.
    for col_num in 1..=6u16 {
        worksheet.set_column_width(col_num, 12)?;
    }

    // Write the table data.
    worksheet.write_column(3, 1, items)?;
    worksheet.write_row_matrix(3, 2, data)?;

    // Create a header format.
    let header = Format::new()
        .set_bold()
        .set_italic()
        .set_font_color(Color::Yellow);

    // Create and configure a new table.
    let mut table = Table::new();
    let columns = vec![
        TableColumn::new()
            .set_header("Product")
            .set_header_format(&header),
        TableColumn::new()
            .set_header("Quarter 1")
            .set_header_format(&header),
        TableColumn::new()
            .set_header("Quarter 2")
            .set_header_format(&header),
        TableColumn::new()
            .set_header("Quarter 3")
            .set_header_format(&header),
        TableColumn::new()
            .set_header("Quarter 4")
            .set_header_format(&header),
    ];
    table.set_columns(&columns);

    // Add the table to the worksheet.
    worksheet.add_table(2, 1, 6, 5, &table)?;

    // Save the file to disk.
    workbook.save("tables.xlsx")?;

    Ok(())
}

This produces the same output file as the version above (with the addition of some internal format references that make it the same as Excel's output).

Xydez commented 1 year ago

Brilliant, massive thanks!

jmcnamara commented 1 year ago

This feature is now available in rust_xlsxwriter v0.43.0. Thanks for the prompt.