C# 获取excel某列单元值的特殊数值处理方式(根据单元数据格式编码获取小位数)

当excel文件某列单元数值显示的值和实际的值不一致:
1.某列某单元显示:38,实际值是38.43,只取显示的38的值。
2.某列某单元显示:38.68,实际值是38.685,只取显示的38.68的值。
注释:如果没有格式并且不是默认的常规格式,是文本格式时,读取什么值则返回什么值。
以下是本人写的公共静态帮助类,可以直接使用:

using NPOI.SS.UserModel;
using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace ExcelAPPCase
{
    /// <summary>
    /// 获取excel单元值帮助类
    /// </summary>
    public static class ExcelExtendHelper
    {
        //获取单元值
        public static string GetCellValue(this ICell cell)
        {
            try
            {
                object val = null;
                switch (cell.CellType)
                {
                    case CellType.Blank:
                        val = "";
                        break;
                    case CellType.Numeric:
                        short format = cell.CellStyle.DataFormat;
                        if (DateUtil.IsCellDateFormatted(cell) || format == 14 || format == 31 || format == 57 || format == 58)
                        {
                            val = cell.DateCellValue;
                        }
                        else
                        {
                            val = cell.NumericCellValue;
                            if (val != null && !string.IsNullOrEmpty(val.ToString()))
                            {
                                ICellStyle cellStyle = cell.CellStyle;
                                // 解析数字格式字符串获取小数位数
                                string formatCode = cellStyle.GetDataFormatString();
                                int? _dotCount = GetformatVal(formatCode);
                                if (_dotCount!=null)
                                {
                                    decimal converVal=Convert.ToDecimal(val);
                                    val= Math.Round(converVal, (int)_dotCount,MidpointRounding.AwayFromZero);
                                }
                            }
                        }
                        break;
                    case CellType.String:
                        val = cell.StringCellValue;
                        break;
                }
                return val?.ToString().Trim();
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        //根据单元数据格式编码获取小位数
        public static int? GetformatVal(string formatCode)
        {
            int decimalPlaces = 0;
            string pattern = @"\d+(\.\d+)?";
            System.Text.RegularExpressions.Match match = Regex.Match(formatCode, pattern);
            if (match.Success)
            {
                string _number = match.Value;
                if (_number.Contains('.'))
                {
                    decimalPlaces = _number.Split('.')[1].Length;
                }
            }
            else
            {
                return null;
            }
            return decimalPlaces;
        }
    }
}

posted @   a博闻强识  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示