From aaed724a0ac1415ea557a3320c8c8fa427130d2f Mon Sep 17 00:00:00 2001 From: RuoYi <yzz_ivy@163.com> Date: 星期五, 27 十月 2023 12:37:10 +0800 Subject: [PATCH] 优化数字金额大写转换精度丢失问题(I81IJA) --- src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java | 43 +++++++++++++++++++++++++++++++++---------- 1 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index 8e5b1fd..661d0db 100644 --- a/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -99,6 +99,11 @@ public static final String[] FORMULA_STR = { "=", "-", "+", "@" }; /** + * 鐢ㄤ簬dictType灞炴�ф暟鎹瓨鍌紝閬垮厤閲嶅鏌ョ紦瀛� + */ + public Map<String, String> sysDictMap = new HashMap<String, String>(); + + /** * Excel sheet鏈�澶ц鏁帮紝榛樿65536 */ public static final int sheetSize = 65536; @@ -283,9 +288,23 @@ * @param is 杈撳叆娴� * @return 杞崲鍚庨泦鍚� */ - public List<T> importExcel(InputStream is) throws Exception + public List<T> importExcel(InputStream is) { - return importExcel(is, 0); + List<T> list = null; + try + { + list = importExcel(is, 0); + } + catch (Exception e) + { + log.error("瀵煎叆Excel寮傚父{}", e.getMessage()); + throw new UtilException(e.getMessage()); + } + finally + { + IOUtils.closeQuietly(is); + } + return list; } /** @@ -331,7 +350,6 @@ } // 鑾峰彇鏈�鍚庝竴涓潪绌鸿鐨勮涓嬫爣锛屾瘮濡傛�昏鏁颁负n锛屽垯杩斿洖鐨勪负n-1 int rows = sheet.getLastRowNum(); - if (rows > 0) { // 瀹氫箟涓�涓猰ap鐢ㄤ簬瀛樻斁excel鍒楃殑搴忓彿鍜宖ield. @@ -446,7 +464,7 @@ { propertyName = field.getName() + "." + attr.targetAttr(); } - else if (StringUtils.isNotEmpty(attr.readConverterExp())) + if (StringUtils.isNotEmpty(attr.readConverterExp())) { val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator()); } @@ -456,7 +474,7 @@ } else if (!attr.handler().equals(ExcelHandlerAdapter.class)) { - val = dataFormatHandlerAdapter(val, attr); + val = dataFormatHandlerAdapter(val, attr, null); } else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures)) { @@ -1034,7 +1052,12 @@ } else if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotNull(value)) { - cell.setCellValue(convertDictByExp(Convert.toStr(value), dictType, separator)); + if (!sysDictMap.containsKey(dictType + value)) + { + String lable = convertDictByExp(Convert.toStr(value), dictType, separator); + sysDictMap.put(dictType + value, lable); + } + cell.setCellValue(sysDictMap.get(dictType + value)); } else if (value instanceof BigDecimal && -1 != attr.scale()) { @@ -1042,7 +1065,7 @@ } else if (!attr.handler().equals(ExcelHandlerAdapter.class)) { - cell.setCellValue(dataFormatHandlerAdapter(value, attr)); + cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell)); } else { @@ -1255,13 +1278,13 @@ * @param excel 鏁版嵁娉ㄨВ * @return */ - public String dataFormatHandlerAdapter(Object value, Excel excel) + public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell) { try { Object instance = excel.handler().newInstance(); - Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class }); - value = formatMethod.invoke(instance, value, excel.args()); + Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class, Cell.class, Workbook.class }); + value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb); } catch (Exception e) { -- Gitblit v1.9.3