jmcnamara / libxlsxwriter

A C library for creating Excel XLSX files.
https://libxlsxwriter.github.io
Other
1.49k stars 332 forks source link

text Wrap issues on different platforms #356

Closed LLMIKU closed 2 years ago

LLMIKU commented 2 years ago

Using 1.1.3 now.

Here is the problem:

If you wish to control where the text is wrapped you can add newline characters to the string:


format = workbook_add_format(workbook);
format_set_text_wrap(format);

worksheet_write_string(worksheet, 0, 0, "It's\na bum\nwrap", format);



When I open this file with Numbers on Mac, it shows text is wrapped.
However, when I open it on Windows(Excel) or Android(Latest version WPS),it shows text is not wrapped, but when I edit it, I can see the \n effect.

Maybe something goes wrong in `lxw_escape_control_characters(string)` function, but I am not familiar with xlsx format. Is **"\_x%04X\_"** this a format control sign?
jmcnamara commented 2 years ago

However, when I open it on Windows(Excel) ... it shows text is not wrapped

It should work as expected in all versions of Excel. Here is an example program:

#include "xlsxwriter.h"

int main() {

    lxw_workbook  *workbook  = workbook_new("test.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

    lxw_format *format = workbook_add_format(workbook);
    format_set_text_wrap(format);

    worksheet_write_string(worksheet, 0, 0, "It's\na bum\nwrap", format);

    workbook_close(workbook);

    return 0;
}

Output:

screenshot

As you can see the test is wrapped as expected.

It may appear differently in other applications but that isn't usually an issue with libxlsxwriter since it follows Excel's format exactly.

One thing to note is that Excel automatically expands the height of the row when the file is opened. This isn't something that is controllable by the file format, it is something that happens at run time when Excel opens the file. So if you don't see that in other applications you may have to specify the row height explicitly.

Anyway, can you test the code above with Excel and confirm you get the same results, or otherwise.

LLMIKU commented 2 years ago

Thanks a lot, I haven't add that format_set_text_wrap(format);, after added, it works perfect!