Closed Xydez closed 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.
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:
It isn't 100% the same as Excel or the final solution but it will give you a workaround.
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).
Brilliant, massive thanks!
This feature is now available in rust_xlsxwriter
v0.43.0. Thanks for the prompt.
Feature Request
Using xlsxwriter, it's possible to use the following field to set header formatting:
It would be nice to be able to do something similar with this library, e.g.:
Is it already possible to accomplish this in another way or is this something that will be implemented?
Best regards