Aspose.cell excel转pdf日期格式不正确yyyy/MM/dd变成MM/dd/yyyy

最近使用Aspose.cell将excel转pdf过程中excel中时间格式列的显示和excel表里的值显示不一样。
excel里日期格式 yyyy/MM/dd
pdf里日期格式MM/dd/yyyy

主要原因:linux和windows里内置的时间格式不一致,当代码部署到linux服务器的时候转换格式就会发生不一致的问题。
解决方法:使用apache poi获取aspose遍历所有的CELL判断其类型,若是日期格式则手动格式化日期格式并将单元格设置成String类型。

接下来详细讲解怎么操作。

例:这边的的B列设置了时间格式且显示是yyyy/MM/dd
在这里插入图片描述
但转成pdf过程中却格式变样了变成MM/dd/yyyy

在这里插入图片描述
在这里插入图片描述

/**
   对excel里所有单元格进行遍历,判断单元格类型是Numeric且是时间类型,
   类型==14)则进行日期格式化且将cell类型设置成String
   单元格为自定义类型的时候,cell.getCellStyle().getDataFormat()值:
  yyyy-MM-dd---->14
  yyyy年m月d日--->31
  yyyy年m月------>57
  m月d日  -------->58
  HH:mm--------->20
  h时mm分  ------>32
 */
public void formatterAllDateCellStyle(POIUtils poiUtils) {
        Sheet sheet = poiUtils.getSheet();
        // 遍历行Row
        // 获取sheet中的总行数
        int rowTotalCount = sheet.getLastRowNum();
        for (int i = 0; i <= rowTotalCount; i++) {
            // 获取第i列的row对象
            Row row = sheet.getRow(i);
            //解决空白行问题
            if (row == null) {
                continue;
            }
            //获取总列数
            int columnCount = row.getLastCellNum();
            for (int j = 0; j < columnCount; j++) {
                Cell cell = row.getCell(j);
                //如果未null则跳过
                if (row.getCell(j) == null) {
                    continue;
                } else {
                    if (cell.getCellType().equals(CellType.NUMERIC)) {
                        //日期格式
                        short format = cell.getCellStyle().getDataFormat();
                        if (DateUtil.isCellDateFormatted(cell)) {
                            if (format == 14) {
                                SimpleDateFormat sdf = null;
                                sdf = new SimpleDateFormat("yyyy/M/d");
                                double valueDouble = cell.getNumericCellValue();
                                Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(valueDouble);
                                String formatStr = sdf.format(date);
                                cell.setCellType(CellType.STRING);
                                cell.setCellValue(formatStr);
                            }

                        }
                    }
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值