My c code successfully compiles both on Linux (Ubuntu) and Windows (MSYS2 / mingw toolchain). The .xlsx file compiled on Linux is correct (file opens in Windows without issue), the same code creates corrupt xlsx file in Windows: "We found a problem with some content in test.xlsx. Do you want us to try to recover as much as we can?..." And MS Excel 2013 can recover the file perfectly, but the message is annoying.
Does someone know what the problem is?
The code is just to test the functionality, which is OK - the output file (write.xlsx) is different in Windows and Linux.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "xlsxio_write.h"
int main(int argc, char *argv[])
{
const char *xfile = "write.xlsx";
const char *sheetname = "Sheet1";
//open .xlsx file for writing (will overwrite if it already exists)
xlsxiowriter handle;
if ((handle = xlsxiowrite_open(xfile, sheetname)) == NULL)
{
fprintf(stderr, "Error creating .xlsx file\n");
return 1;
}
//set row height
xlsxiowrite_set_row_height(handle, 1);
//how many rows to buffer to detect column widths
xlsxiowrite_set_detection_rows(handle, 10);
//write column names
xlsxiowrite_add_column(handle, "Col1", 0);
xlsxiowrite_add_column(handle, "Col2", 21);
xlsxiowrite_add_column(handle, "Col3", 0);
xlsxiowrite_add_column(handle, "Col4", 2);
xlsxiowrite_add_column(handle, "Col5", 0);
xlsxiowrite_add_column(handle, "Col6", 0);
xlsxiowrite_add_column(handle, "Col7", 0);
xlsxiowrite_next_row(handle);
//write data
for (int i = 0; i < 100; i )
{
xlsxiowrite_add_cell_string(handle, "01232021");
xlsxiowrite_add_cell_string(handle, "A b c d e f\nnew line");
xlsxiowrite_add_cell_string(handle, "&% <test> \"'");
xlsxiowrite_add_cell_string(handle, NULL);
xlsxiowrite_add_cell_int(handle, i);
xlsxiowrite_add_cell_datetime(handle, time(NULL));
xlsxiowrite_add_cell_float(handle, 3.1415926);
xlsxiowrite_next_row(handle);
}
//close .xlsx file
xlsxiowrite_close(handle);
return 0;
}
Thanks!
CodePudding user response:
As the comments are saying, you need to debug and get more smaller problem then return back to ask. One possible way to make your debugging easier is to begin to create a simple excel sheet with even a single cell, then add your code piece by piece and builds upon this example until you got the corrupted excel sheet. This is one way to start grasping the problem and you can then return back here to ask a more precise question.
CodePudding user response:
There's something wrong with mingw-w64-x86_64-xlsxio package (that I installed and used in the first place). If I manually compile xlsxio for MSYS2, the code works fine and generate correct xlsx format in Windows too.