From c4cd477002ae3200be563d2cdf24f90173992089 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Mon, 12 Aug 2024 11:14:46 +0200 Subject: [PATCH] Allow trailing characters (m, cbm etc.) when importing numbers in Excel --- ENI2/Excel/ExcelBase.cs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/ENI2/Excel/ExcelBase.cs b/ENI2/Excel/ExcelBase.cs index 31eb1cd5..3573d4cf 100644 --- a/ENI2/Excel/ExcelBase.cs +++ b/ENI2/Excel/ExcelBase.cs @@ -7,6 +7,7 @@ using Microsoft.Office.Interop.Excel; using System.Runtime.InteropServices; using log4net; using System.Globalization; +using System.Text.RegularExpressions; namespace ENI2.Excel { @@ -105,6 +106,20 @@ namespace ENI2.Excel } } + private static double? ParseAnyDouble(string val) + { + double? result = null; + if (double.TryParse(val, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, + CultureInfo.InvariantCulture, out double tmpDouble)) + result = tmpDouble; + if (result == null) + { + if (double.TryParse(val, out tmpDouble)) // current language style (==GER, mit , statt .) + result = tmpDouble; + } + return result; + } + internal double? ReadNumber(string lookup) { double? result = null; @@ -114,15 +129,17 @@ namespace ENI2.Excel { var val = _nameDict[lookup].RefersToRange.Value; if (val is double) result = val; - if (val is string) + if ((val is string) && (val.Length > 0)) { - if (double.TryParse(val, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, - CultureInfo.InvariantCulture, out double tmpDouble)) - result = tmpDouble; - if (result == null) - { - if (double.TryParse(val, out tmpDouble)) // current language style (==GER, mit , statt .) - result = tmpDouble; + result = ParseAnyDouble(val); + + if(result == null) + { + Match m = Regex.Match(val, "([0-9\\.\\,]+)([a-zA-Z]*)"); + if (m.Success) + { + result = ParseAnyDouble(m.Groups[1].Value); + } } }