OpenXLSX: Adding "mergeCells" to program corrupts .xlsx file
Hello all,
I’m working on an accounting program that writes various data to excel files and one of the files has a header row in which ten cells get merged into pairs (B1:C1, D1:E1, etc.) but no matter how I implement the “mergeCell” functionality, when I open the resulting workbook I get a messagebox that reads “We found a problem with some content in ‘Worksheet.xlsx’. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.” Upon clicking next, I’m greeted by an empty workbook. I’ve tried several implementations so far and each one causes this messagebox. I’m writing data from a wxVector of wxVectors of wxStrings which is why I’m using “i” and “j” in the code below.
Here is the first implementation I tried:
// Merge current cell with one to its right:
OpenXLSX::XLCellReference start_ref((i + 1), (j + 1));
OpenXLSX::XLCellReference end_ref((i + 1), (j + 2));
std::string start = start_ref.address();
std::string end = end_ref.address();
std::string range = start + ":" + end;
sheet.mergeCells(range);
I also tried merging the cells explicitly after writing all the data:
// Merge header row cells:
sheet.mergeCells("B1:C1", OpenXLSX::XLEmptyHiddenCells);
sheet.mergeCells("D1:E1", OpenXLSX::XLEmptyHiddenCells);
sheet.mergeCells("F1:G1", OpenXLSX::XLEmptyHiddenCells);
sheet.mergeCells("H1:I1", OpenXLSX::XLEmptyHiddenCells);
sheet.mergeCells("J1:K1", OpenXLSX::XLEmptyHiddenCells);
Am I missing anything? Thank you in advance for your help!
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 44 (1 by maintainers)
Yeah, it just looked highly suspicious, so I just deleted them before anyone fell for it.
I can’t upload the before and after styles.xml files but here is the file after opening and saving in LO: Test.xlsx
I don’t know if this makes any difference but I wrote both the original program and the minimal testing program in MS Visual Studio 2022 running on Windows 11 Pro.
Update: Once again, I am able to open the .xlsx file in Excel after saving it using LibreOffice so somehow the reconfiguration executed by LibreOffice on save fixes whatever is rendering the file unopenable by Excel in the first place.