Comments (5)
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.
from rust_xlsxwriter.
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.
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.
Brilliant, massive thanks!
from rust_xlsxwriter.
This feature is now available in rust_xlsxwriter
v0.43.0. Thanks for the prompt.
from rust_xlsxwriter.
Related Issues (20)
- Bug: clone HOT 8
- feature request: rename `rust_xlsxwriter` into `excelwriter` HOT 10
- Bug: issue with serde `skip_serializing_if` HOT 3
- Is it possible to add a custom XlsxError HOT 4
- Bug: `Result<T, E>` serialization not working HOT 1
- Bug: `value_format` annotation not working with `chrono::NaiveDate` HOT 9
- Bug: serialized fields are not skipped correctly
- question: Use formatting without workbook HOT 1
- Is it possible for us to add comments to cells? HOT 3
- feature request: <Prevent cell from overflowing into next (empty) cell> HOT 1
- Feature request: set a cell 's background color HOT 2
- Feature request: utility::validate_sheetname visibility HOT 5
- feature request: <Update polars to version 0.37> HOT 6
- Bug: `IntoExcelData::write_with_format` should have a free lifetime for `format` argument HOT 3
- feature request: Add the ability to return a Workbook by reading an xlsx file HOT 1
- Bug: Line breaks "\n" behaves strangely HOT 3
- question: Is it normal that "23/07/2023" renders "'23/07/2023"? HOT 8
- Bug: header_format and table_default are incompatible HOT 6
- feature request: <Implement the URLs in images feature>
- Bug: Incorrect adjustment of column widths in autofit method with autofilter headers HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust_xlsxwriter.