tfussell / xlnt

:bar_chart: Cross-platform user-friendly xlsx library for C++11+
Other
1.5k stars 423 forks source link

Do Excel files not support Chinese path? #724

Open zhjr2019 opened 1 year ago

zhjr2019 commented 1 year ago
#include <xlnt/xlnt.hpp>
int main()
{
    xlnt::workbook fault_workbook;
    std::string fault_excel_file = "F:\\中国\\中国20220823224554.xlsx";
    fault_workbook.load(fault_excel_file);
    return 0;
}

error:bad conversion

Do Excel files not support Chinese path?

doomlaur commented 11 months ago

I'm not sure which version of xlnt you are using, but if you're using version 1.5.0, you should maybe use the latest commit from the master branch instead. The issue you describe has been fixed by pull request #607 by using std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>. As explained on cppreference, the previous code converted UTF-8 to UCS-2 (the predecessor of UTF-16) on Windows, causing Unicode code points that need 4 bytes (like emojis) to fail. Unfortunately, the fix has not been released in a stable version of XLNT yet, but at least in my experience, the master branch seems to be even more stable than version 1.5.0, as it contains many bugfixes - so you should definitely give it a go.

szfbit commented 1 month ago

But when the file name contains Chinese, calling the workbook.load() function does indeed result in an error: std::range_error。 Upon checking the source code, it was found that the error occurred at the following location:xlocbuf _Throw(range_error("bad conversion")). If possible, please fix it!Thank you.

doomlaur commented 1 month ago

As discussed in issue https://github.com/tfussell/xlnt/issues/748, this repo has been unmaintained for several years.

In order to continue this great project started by tfussell, we have created a new repo to support further development by the community.

Furthermore, specifically regarding this issue, I have changed the way Unicode conversions are done in Pull Request https://github.com/xlnt-community/xlnt/pull/29. So @szfbit could you please clone our new fork, build XLNT using the newest commit of our master branch, and tell me if it solved your problem? If not, feel free to open an issue in our new repo with a detailed example that causes the issue you described. Thank you!

szfbit commented 1 month ago

Hello, thank you for your reply! My problem has been solved now, just change the encoding format of the source code from GBK to UTF8. I will continue to monitor the new code repository, keep up the good ! @doomlaur

doomlaur commented 1 month ago

@szfbit Perfect, I'm glad to hear that! :smile: Using UTF-8 is always a great idea :wink: However, if you think that the conversion error is also an issue with XLNT, feel free to open an issue in our new repo so that we can take a closer look at it!