简体   繁体   中英

Export to Excel failing in iOS using libxlsxwriter

Has anyone used libxlsxwriter to export data to Excel in iOS? I got the library up and running. Given below is the code that I have tried.

lxw_workbook  *workbook  = new_workbook("demo.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

int ret = worksheet_write_string(worksheet, 0, 0, "Hello", NULL);
ret = worksheet_write_number(worksheet, 1, 0, 123, NULL);

int ret1 = workbook_close(workbook);
if(LXW_CLOSE_ERROR_ZIP == ret1)
    NSLog(@"Failed");

It is consistently failing for me at workbook_close();

This is the error message I am getting

[ERROR][/path/packager.c:62]: Error opening zip file for xlsx
[ERROR][/path/workbook.c:1108]: Memory allocation failed.

Any idea why this is happening? Is there any other library that I can use?

Those errors are misleading. They are usually generated when the output file or directory isn't writeable.

For example:

cd libxlsxwriter
make examples
./examples/hello
# Creates hello_world.xlsx

chmod -w hello_world.xlsx
./examples/hello

[ERROR][packager.c:62]: Error opening zip file for xlsx
[ERROR][workbook.c:1108]: Memory allocation failed.

I'll see if I can add a fix to the library to make the actual source of the error clearer.

Update : In newer versions the error is clearer:

$ ./examples/hello
[ERROR] workbook_close(): Error creating 'hello_world.xlsx'. 
        Error = Permission denied

Something else to keep in mind is that you should call the libxlsxwriter functions with a string encoding that can be understood by C. I was getting this error until I converted all the strings in the calls to libxlsxwriter to UTF8 strings.

In your case, your calls to create the workbook and write the string would become:

lxw_workbook  *workbook  = new_workbook(("demo.xlsx" as NSString).UTF8String);
...
int ret = worksheet_write_string(worksheet, 0, 0, ("Hello" as NSString).UTF8String, NULL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM