nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.74k stars 1.44k forks source link

The backport of POI Bug 60370 introduces a bug with PromptBox and ErrorBox text encoding #1386

Closed yzhoholiev closed 4 months ago

yzhoholiev commented 4 months ago

NPOI Version

2.7.1

File Type

Reproduce Steps

Create a PromptBox or ErrorBox with special characters (new line, for example).

Issue Description

The issue was introduced as part of the backporting of the fix for POI Bug 60370 - XSSFDataValidation, promptBox, \n ignored (no multiline).

image image

The bug was introduced as part of the translation of the Java implementation of the encodeUtf to C#: Original code: https://github.com/apache/poi/blob/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDataValidation.java#L151 C# code: https://github.com/nissl-lab/npoi/blob/2.7.1-rc1/ooxml/XSSF/UserModel/XSSFDataValidation.cs#L163

The Java implementation of the Integer.toHexString method does not prepend the string with the leading zeros (the spec requires it to be 4 characters long), for that reason they are manually prepended with zeros to get the required length.

The proper implementation in C# will look like:

builder.Append("_x").Append($"{(byte) c:X4}").Append("_");

In case you want to use the HexDump helper it should be something like this:

builder.Append("_x").Append(HexDump.ToHex((short) c)).Append("_");
tonyqus commented 4 months ago

bug introduced by #1354