I'm trying to create an Excel worksheet in C# in OpenXML, but end up with Excel having to "repair" the Workbook because of doubles have been stored with commas as decimal separators in the Excel /xl/worksheets/sheet1.xml file.
My Excel and Windows globalisation is Swedish, which sets decimal separators to comma whenever a double is converted to a string.
When I do a
double d = 355d/113d;
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new Cell();
cell.CellValue = new CellValue(d);
cell.DataType = CellValues.Number;
what is actually stored in the Excel's /xl/worksheets/sheet1.xml is
<x:c s="4" t="n">
<x:v>3,14159292035398</x:v>
</x:c>
with comma as decimal separator. And that makes my Swedish Excel force a "repair" and converts the cells to text/string instead of the numeric values. I can manually convert to numeric values with minimal effort inside Excel, but that is not acceptable in this application.
Looking at the /xl/worksheets/sheet1.xml AFTER "repair" and manually converting the strings to decimal values, they are stored as
<v>3.1415929203539825</v>
WITH a dot as decimal separator. The obvious solution - so I thought - was to force store my decimal value with a dot instead with something like
double d = 355d/113d;
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new Cell();
cell.CellValue = new CellValue(d.ToString().Replace(",", ".").Replace(" ", ""));
cell.DataType = CellValues.Number;
and a few (.) more varieties to make sure that cell.CellValue contains a dot instead of a comma, But then my Swedish Excel goes bananas and refuses to interpret the value as a decimal. and makes it an int instead.
So I'm stuck. Does anyone have a clue on how to store a decimal value in the DocumentFormat.OpenXml.Spreadsheet.Cell that Excel will interpret correctly, without repairs, in a system that uses comma as decimal separator by default?
Haven't tried this but what about something like:
var nfi = System.Globalization.CultureInfo.InvariantCulture.NumberFormat;
...
cell.CellValue = new CellValue(d.ToString(nfi));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.