使用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;
            }
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用POI读取Excel,可以通过 HSSFClientAnchor 类来获取 Excel 中图片的位置和大小信息,并通过 PictureData 类来获取图片的二进制数据,然后可以将图片保存到本地或者上传到服务器。 以下是一个示例代码: ```java FileInputStream inputStream = new FileInputStream("test.xls"); Workbook workbook = new HSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); // 获取所有图片并处理 List<PictureData> pictures = new ArrayList<>(); Map<Integer, PictureData> picturesMap = new HashMap<>(); List<HSSFShape> shapes = ((HSSFSheet) sheet).getDrawingPatriarch().getChildren(); for (HSSFShape shape : shapes) { if (shape instanceof HSSFPicture) { HSSFPicture picture = (HSSFPicture) shape; PictureData pictureData = picture.getPictureData(); pictures.add(pictureData); picturesMap.put(picture.getPictureIndex(), pictureData); } } // 处理单元格中的图片 for (Row row : sheet) { for (Cell cell : row) { if (cell.getCellTypeEnum() == CellType.BLANK) { continue; } if (cell.getCellTypeEnum() == CellType.STRING && StringUtils.isNotBlank(cell.getStringCellValue())) { RichTextString richTextString = cell.getRichStringCellValue(); for (int i = 0; i < richTextString.numFormattingRuns(); i++) { int startIndex = richTextString.getIndexOfFormattingRun(i); int endIndex = startIndex + richTextString.getLengthOfFormattingRun(i); HSSFFont font = (HSSFFont) richTextString.getFontOfFormattingRun(i); if (font.getUnderline() == Font.U_SINGLE && font.getColor() == HSSFColor.AUTOMATIC.index) { for (int j = startIndex; j < endIndex; j++) { HSSFRichTextString hssfRichTextString = (HSSFRichTextString) richTextString; int pictureIndex = hssfRichTextString.getIndexOfFormattingRun(i) / 3; PictureData pictureData = picturesMap.get(pictureIndex); if (pictureData != null) { String fileName = UUID.randomUUID().toString() + "." + pictureData.suggestFileExtension(); byte[] data = pictureData.getData(); // 处理图片数据 } } } } } } } ``` 在上面的代码中,首先获取 Excel 中的所有图片,然后遍历单元格,通过 RichTextString 获取单元格中的所有文本和图片,并通过 HSSFRichTextString.getIndexOfFormattingRun() 方法获取图片在文本中的位置,最后可以通过 HSSFClientAnchor 类获取图片在 Excel 中的位置和大小信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值