fix(xlsx): honor explicit cell type= directive

Explicit type=string now forces an inline-string cell even when the
value is numeric-looking (e.g. value=123), instead of silently storing
a number. Explicit type=number with a non-numeric value now throws
ArgumentException instead of silently storing as string.
This commit is contained in:
zmworm 2026-04-19 08:09:40 +08:00
parent dd2d010191
commit 74613df6b5

View file

@ -1436,6 +1436,25 @@ public partial class ExcelHandler
cell.CellValue = new CellValue(cellValue);
cell.DataType = new EnumValue<CellValues>(CellValues.String);
}
else if (explicitTypeIsString)
{
// R15-2: honor explicit type=string even for
// numeric-looking literals. Without this, Excel
// renders 123 as a number despite user intent.
cell.CellValue = new CellValue(cellValue);
cell.DataType = new EnumValue<CellValues>(CellValues.String);
}
else if (explicitTypeIsNumber)
{
// R15-2: honor explicit type=number — refuse
// non-numeric values rather than silently storing
// as string.
if (!double.TryParse(cellValue, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out _))
throw new ArgumentException(
$"Cannot store '{cellValue}' as number; use type=string or remove type=");
cell.CellValue = new CellValue(cellValue);
cell.DataType = null;
}
else
{
cell.CellValue = new CellValue(cellValue);