informationsea / xlsxwriter-rs

Excel file writer for Rust
https://crates.io/crates/xlsxwriter
Apache License 2.0
265 stars 44 forks source link

`freeze_panes` does not appear to work #29

Closed ryanmcgrath closed 1 year ago

ryanmcgrath commented 2 years ago

...or, if it does, I'm simply completely misunderstanding it.

I need to freeze the header (row 0) and the first two columns (columns 0 and 1). No matter where I place the call to freeze_panes, the rows/columns do not actually freeze.

(Grepping around the internet for logic regarding the inner workings of Excel is 100% not my strength, so if I'm missing something please let me know)

Christoph-AK commented 2 years ago

I do it for every sheet directly before saving the file and it works fine.

Pass the amount of rows and columns you want to lock!

So in your case (1,2)

    sheet1.freeze_panes(1, 1);
    sheet2.freeze_panes(1, 1);
    sheet3.freeze_panes(1, 1);

    if excel.close().is_ok() {...}
ryanmcgrath commented 2 years ago

Hmmmm... well, I'm certainly open to being wrong. Suppose I'll tinker a bit more, there's probably something I've missed then. Thanks for the sanity check!

Christoph-AK commented 2 years ago

If you post some code that refuses to work I might be able to help out more.

jmcnamara commented 1 year ago

It should work as expected. The only thing to look out for is to make sure that splitting cell is after the split, like in Excel.

I need to freeze the header (row 0) and the first two columns (columns 0 and 1).

In that case you would need to split at row = 1 and column = 2. This is still zero indexing but the split needs to be specified in the bottom right quadrant instead of the top left. Here is an example.

So Here is an example:

use xlsxwriter::Workbook;

fn main() -> Result<(), xlsxwriter::XlsxError> {
    let workbook = Workbook::new("freeze.xlsx");
    let mut worksheet = workbook.add_worksheet(None)?;

    for row in 0_u32..10_u32 {
        for col in 0_u16..8_u16 {
            worksheet.write_string(row, col, "Text", None)?;
        }
    }

    worksheet.freeze_panes(1, 2);

    workbook.close()
}

Output:

screenshot

I'd suggest closing this issue.

ryanmcgrath commented 1 year ago

Thanks!