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

在这里插入图片描述

java 复制代码
/**
   对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);
                            }

                        }
                    }
                }
            }
        }
    }
相关推荐
FC_nian5 分钟前
IDEA配置(Maven)
java·maven·intellij-idea
汤姆yu29 分钟前
基于springboot的快递分拣管理系统
java·spring boot·后端
xuanjiong38 分钟前
Excel数据转化为Xmind思维导图全流程(含Word转化格式),实用
excel·xmind
lang2015092839 分钟前
Apache Ignite Data Streaming 案例 QueryWords
apache·ignite
衍生星球1 小时前
JSP 程序设计之 JSP 基础知识
java·web·jsp
m0_749317521 小时前
力扣-字母异位词
java·算法·leetcode·职场和发展
黑暗也有阳光1 小时前
java中为什么hashmap的大小必须是2倍数
java·后端
fatsheep洋1 小时前
XSS-DOM-1
java·前端·xss
七七软件开发2 小时前
一对一交友小程序 / APP 系统架构分析
java·python·小程序·系统架构·php
TDengine (老段)2 小时前
TDengine 中 TDgpt 异常检测的数据密度算法
java·大数据·算法·时序数据库·iot·tdengine·涛思数据