使用POI导入excel文件时读取格式相关问题

在excel文档中,数字、日期类型数据在编辑后会变成numeric格式
右对齐为编辑后的,格式已经改变
导致在读取过程中,如果按照原来格式读取会遇到问题

故添加以下函数进行判断

        /// <summary>
        /// 获取字符串格式的值
        /// </summary>
        /// <param name="cell"></param>
        /// <returns></returns>
        private string GetCellStringValue(ICell cell)
        {
            try
            {
                if (cell == null || cell.CellType == CellType.Blank)
                {
                    return null;
                }
                string result;
                switch (cell.CellType)
                {
                    case CellType.Numeric:
                        result = cell.NumericCellValue.ToString(CultureInfo.InvariantCulture);
                        break;
                    default:
                        result = cell.StringCellValue?.Trim();
                        break;
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 获取日期格式的值
        /// </summary>
        /// <param name="cell"></param>
        /// <returns></returns>
        private DateTime? GetCellDateTimeValue(ICell cell)
        {
            try
            {
                if (cell == null || cell.CellType == CellType.Blank)
                {
                    return null;
                }
                DateTime? result;
                switch (cell.CellType)
                {
                    case CellType.Numeric:
                        result = cell.DateCellValue;
                        break;
                    case CellType.String:
                        result = DateTime.Parse(cell.StringCellValue);
                        break;
                    default:
                        result = null;
                        break;
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 获取数字格式的值
        /// </summary>
        /// <param name="cell"></param>
        /// <returns></returns>
        private double? GetCellNumericValue(ICell cell)
        {
            try
            {
                if (cell == null || cell.CellType == CellType.Blank)
                {
                    return null;
                }
                double? result;
                switch (cell.CellType)
                {
                    case CellType.Numeric:
                        result = cell.NumericCellValue;
                        break;
                    case CellType.String:
                        result = double.Parse(cell.StringCellValue);
                        break;
                    default:
                        result = null;
                        break;
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值