北京大型网站建设,wordpress移动端导航菜单加文字,中国建设教育协会是什么网站,手机h5页面制作软件最近使用Aspose.cell将excel转pdf过程中excel中时间格式列的显示和excel表里的值显示不一样。 excel里日期格式 yyyy/MM/dd pdf里日期格式MM/dd/yyyy 主要原因#xff1a;linux和windows里内置的时间格式不一致#xff0c;当代码部署到linux服务器的时候转换格式就会发生不一… 最近使用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----14yyyy年m月d日---31yyyy年m月------57m月d日 --------58HH:mm---------20h时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);}}}}}}}