我使用 EPPlus 生成的文件在不同的 PC 上的 excel 中得到不同的数字

I get different numbers in excel on different PCs using EPPlus generated file

我的 C# 应用程序中有这段代码可以使用 EPPlus 生成 Excel 文件,一切正常,我将应用程序发送给客户,他说它显示了错误的数字。在他发给我的 Excel 文件中,数字真的不一样。不知道为什么。我怀疑 Excel 是罪魁祸首,由于区域设置或类似原因,对数字的解释不同。我怎样才能解决这个问题? 例如,20 在另一台 PC 上变为 2000:

Decimal.TryParse(dataGridView1.Rows[e.RowIndex].Cells[15].Value.ToString().Replace(".", ","), out number); //cell value 20.00
ws.Cells["I33"].Value = number;
ws.Cells["I33"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
ws.Cells["I33"].Style.Numberformat.Format = "0.00"; //becomes 20,00

结果:

这是一个小例子:

string decimalStr1 = "123,34";
string decimalStr2 = "123.34";

decimal number1, number2 = 0m;

decimal.TryParse(decimalStr1.Replace(",", "."), NumberStyles.AllowDecimalPoint, 
                 CultureInfo.InvariantCulture, out number1);
decimal.TryParse(decimalStr2.Replace(",", "."), NumberStyles.AllowDecimalPoint, 
                 CultureInfo.InvariantCulture, out number2);

// number1 = number2 = 123.34 

重要的是使用“.”作为分隔符和 CultureInfo.InvariantCulture。 Ant 然后在解析后(你确定值是正确的)你可以将分隔符改回 ',':

ws.Cells["I33"].Value = number1.ToString().Replace('.', ',');