Giter Site home page Giter Site logo

Comments (5)

jmcnamara avatar jmcnamara commented on June 10, 2024 1

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.

from rust_xlsxwriter.

jmcnamara avatar jmcnamara commented on June 10, 2024 1

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).

from rust_xlsxwriter.

jmcnamara avatar jmcnamara commented on June 10, 2024

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.

from rust_xlsxwriter.

Xydez avatar Xydez commented on June 10, 2024

Brilliant, massive thanks!

from rust_xlsxwriter.

jmcnamara avatar jmcnamara commented on June 10, 2024

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

from rust_xlsxwriter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.