soulCoke
2020-07-23 8dd3ca5bb6b48e73f748ff4770f223b63cc09548
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
@@ -8,7 +8,6 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
@@ -18,6 +17,7 @@
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
@@ -41,6 +41,7 @@
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.annotation.Excel.ColumnType;
import com.ruoyi.common.annotation.Excel.Type;
@@ -50,6 +51,7 @@
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.reflect.ReflectUtils;
@@ -199,7 +201,9 @@
                    // 设置类的私有字段属性可访问.
                    field.setAccessible(true);
                    Integer column = cellMap.get(attr.name());
                    fieldsMap.put(column, field);
                    if(column !=null ) { // 字段在excel 中没有,那么就不需要设置值
                       fieldsMap.put(column, field);
                    }
                }
            }
            for (int i = 1; i < rows; i++)
@@ -270,7 +274,11 @@
                        }
                        else if (StringUtils.isNotEmpty(attr.readConverterExp()))
                        {
                            val = reverseByExp(String.valueOf(val), attr.readConverterExp());
                            val = reverseByExp(Convert.toStr(val), attr.readConverterExp());
                        }
                        else if (StringUtils.isNotEmpty(attr.dictType()))
                        {
                            val = reverseDictByExp(attr.dictType(), Convert.toStr(val));
                        }
                        ReflectUtils.invokeSetter(entity, propertyName, val);
                    }
@@ -529,13 +537,18 @@
                Object value = getTargetValue(vo, field, attr);
                String dateFormat = attr.dateFormat();
                String readConverterExp = attr.readConverterExp();
                String dictType = attr.dictType();
                if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
                {
                    cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
                }
                else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
                {
                    cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp));
                    cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp));
                }
                else if (StringUtils.isNotEmpty(dictType))
                {
                    cell.setCellValue(convertDictByExp(dictType, Convert.toStr(value)));
                }
                else
                {
@@ -664,6 +677,30 @@
            throw e;
        }
        return propertyValue;
    }
    /**
     * 解析字典值
     *
     * @param dictType 字典类型
     * @param dictValue 字典值
     * @return 字典标签
     */
    public static String convertDictByExp(String dictType, String dictValue) throws Exception
    {
        return DictUtils.getDictLabel(dictType, dictValue);
    }
    /**
     * 反向解析值字典值
     *
     * @param dictType 字典类型
     * @param dictValue 字典标签
     * @return 字典值
     */
    public static String reverseDictByExp(String dictType, String dictLabel) throws Exception
    {
        return DictUtils.getDictValue(dictType, dictLabel);
    }
    /**
@@ -841,14 +878,15 @@
                    }
                    else
                    {
                        if ((Double) val % 1 > 0)
                        {
                            val = new DecimalFormat("0.00").format(val);
                        }
                        else
                        {
                            val = new DecimalFormat("0").format(val);
                        }
                  /* if ((Double) val % 1 > 0)
                  {
                      val = new DecimalFormat("0.00").format(val);
                  }
                  else
                  {
                      val = new DecimalFormat("0").format(val);
                  }*/
                       val = new BigDecimal(val.toString()); // 导入的数据保证原汁原味,不做处理
                    }
                }
                else if (cell.getCellTypeEnum() == CellType.STRING)