[GH-ISSUE #255] Corrupted EXCEL file #202

Closed
opened 2026-05-05 11:54:48 -06:00 by gitea-mirror · 7 comments
Owner

Originally created by @hadrizi on GitHub (Dec 12, 2019).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/255

Originally assigned to: @jmcnamara on GitHub.

I am using libxlsxwriter to make DOS txt parser.
But when i tried to build an application for Windows it compiled with no errors, and when I opened my excel file I got this message

Excel could not open file.xlsx because some content is unreadable. Do you want to open and repair this workbook.

so most of my data was missed.
So i tried to write small example code and chek if problem appears again and it does.
I am using Visual Studio 2017, libxlsxwriter was built according to this guide, my OS is Windows 10
Here is small code i tried to compile:

#include "xlsxwriter.h"

struct header
{
	char name[256];
};

struct header hd = { "hey" };

int main() {
	lxw_workbook  *workbook = workbook_new("hello_world.xlsx");
	lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

	lxw_row_t r = 0;
	lxw_col_t c = 0;
	
	worksheet_write_string(worksheet, r, c, hd.name, NULL);
	
	return workbook_close(workbook);;
}

P.S here is the code I compiled on Ubuntu 18.04. Sorry it is in Russian, but there is only one code file in src folder.

Originally created by @hadrizi on GitHub (Dec 12, 2019). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/255 Originally assigned to: @jmcnamara on GitHub. I am using libxlsxwriter to make DOS txt parser. But when i tried to build an application for Windows it compiled with no errors, and when I opened my excel file I got this message ``` Excel could not open file.xlsx because some content is unreadable. Do you want to open and repair this workbook. ``` so most of my data was missed. So i tried to write small example code and chek if problem appears again and it does. I am using Visual Studio 2017, libxlsxwriter was built according to [this guide](https://libxlsxwriter.github.io/getting_started.html#gsg_vcpkg), my OS is Windows 10 Here is small code i tried to compile: ``` #include "xlsxwriter.h" struct header { char name[256]; }; struct header hd = { "hey" }; int main() { lxw_workbook *workbook = workbook_new("hello_world.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_row_t r = 0; lxw_col_t c = 0; worksheet_write_string(worksheet, r, c, hd.name, NULL); return workbook_close(workbook);; } ``` P.S [here](https://github.com/hadrizi/tArass) is the code I compiled on Ubuntu 18.04. Sorry it is in Russian, but there is only one code file in src folder.
gitea-mirror 2026-05-05 11:54:48 -06:00
Author
Owner

@jmcnamara commented on GitHub (Dec 12, 2019):

So i tried to write small example code and chek if problem appears again and it does.

That source file doesn't have any issues:

screenshot

I also ran the tArass file with the Readme.md file as input and that worked fine as well:

screenshot

The issue is probably that you are processing a file that isn't UTF-8 encoded. All strings passed to libxlsxwriter (and Excel) must be UTF8 encoded. Do you think that may be your issue?

<!-- gh-comment-id:565179915 --> @jmcnamara commented on GitHub (Dec 12, 2019): > So i tried to write small example code and chek if problem appears again and it does. That source file doesn't have any issues: ![screenshot](https://user-images.githubusercontent.com/94267/70747474-fdf36580-1d1f-11ea-92fa-5cddbc9fbb3a.png) I also ran the tArass file with the Readme.md file as input and that worked fine as well: ![screenshot](https://user-images.githubusercontent.com/94267/70747689-68a4a100-1d20-11ea-9202-02a80192f6a4.png) The issue is probably that you are processing a file that isn't UTF-8 encoded. All strings passed to libxlsxwriter (and Excel) must be UTF8 encoded. Do you think that may be your issue?
Author
Owner

@hadrizi commented on GitHub (Dec 12, 2019):

@jmcnamara hm, this is strange I reloaded my PC and smaller code started to work. But tArass still doesn't work.
Here is file I'm parsing.
tarass bug
and it seems that file is UTF-8 encoded
utf8

<!-- gh-comment-id:565209896 --> @hadrizi commented on GitHub (Dec 12, 2019): @jmcnamara hm, this is strange I reloaded my PC and smaller code started to work. But tArass still doesn't work. Here is [file](https://drive.google.com/open?id=1-HFXBmlTOalRvlMrf4jsfTbcD9Ir98a_) I'm parsing. ![tarass bug](https://raw.githubusercontent.com/hadrizi/tArass/master/assets/bug.gif "tarass bug") and it seems that file is UTF-8 encoded ![utf8](https://raw.githubusercontent.com/hadrizi/tArass/master/assets/utf8.PNG "utf8")
Author
Owner

@RaFaeL-NN commented on GitHub (Dec 12, 2019):

А строки ""Выполняемая команда" и т.п. в worksheet_merge_range ты точно в UTF-8 передаешь? Файл xlsx на выходе прикрепи, его можно распаковать и посмотреть, что там не нравится Excel

<!-- gh-comment-id:565212096 --> @RaFaeL-NN commented on GitHub (Dec 12, 2019): А строки ""Выполняемая команда" и т.п. в worksheet_merge_range ты точно в UTF-8 передаешь? Файл xlsx на выходе прикрепи, его можно распаковать и посмотреть, что там не нравится Excel
Author
Owner

@hadrizi commented on GitHub (Dec 12, 2019):

А строки ""Выполняемая команда" и т.п. в worksheet_merge_range ты точно в UTF-8 передаешь?

Вот так я их передаю
code_piece

Файл xlsx на выходе прикрепи, его можно распаковать и посмотреть, что там не нравится Excel

а вот сам xlsx файл

<!-- gh-comment-id:565216250 --> @hadrizi commented on GitHub (Dec 12, 2019): > А строки ""Выполняемая команда" и т.п. в worksheet_merge_range ты точно в UTF-8 передаешь? Вот так я их передаю ![code_piece](https://raw.githubusercontent.com/hadrizi/tArass/master/assets/code_piece.PNG "code_piece") > Файл xlsx на выходе прикрепи, его можно распаковать и посмотреть, что там не нравится Excel а вот сам xlsx [файл](https://drive.google.com/open?id=1sgndnqIlderrQzlZ4c7GDzFgzhcbjga8)
Author
Owner

@RaFaeL-NN commented on GitHub (Dec 12, 2019):

Строки ты передаешь в Win-1251, поэтому файл на выходе битый. Надо передавать в UTF-8. Я не знаю как это сделать в VS (не программист на С), возможно какой-то параметр сборки проекта. Либо использовать функции перекодирования MultiByteToWideChar/WideCharToMultiByte. Замени временно все русские строки на пустые либо транслитерируй их и увидишь, что дело в этом

<!-- gh-comment-id:565221585 --> @RaFaeL-NN commented on GitHub (Dec 12, 2019): Строки ты передаешь в Win-1251, поэтому файл на выходе битый. Надо передавать в UTF-8. Я не знаю как это сделать в VS (не программист на С), возможно какой-то параметр сборки проекта. Либо использовать функции перекодирования MultiByteToWideChar/WideCharToMultiByte. Замени временно все русские строки на пустые либо транслитерируй их и увидишь, что дело в этом
Author
Owner

@hadrizi commented on GitHub (Dec 13, 2019):

Строки ты передаешь в Win-1251, поэтому файл на выходе битый.

Да, так оно и есть. Я просто поменял кодировку исходника, собрал и все сработало.

<!-- gh-comment-id:565247354 --> @hadrizi commented on GitHub (Dec 13, 2019): > Строки ты передаешь в Win-1251, поэтому файл на выходе битый. Да, так оно и есть. Я просто поменял кодировку исходника, собрал и все сработало.
Author
Owner

@jmcnamara commented on GitHub (Dec 13, 2019):

@RaFaeL-NN Спасибо за помощь

<!-- gh-comment-id:565346696 --> @jmcnamara commented on GitHub (Dec 13, 2019): @RaFaeL-NN Спасибо за помощь
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#202
No description provided.