| | |
| | | package com.ruoyi.common.utils.poi;
|
| | |
|
| | | import java.io.File;
|
| | | import java.io.FileOutputStream;
|
| | | import java.io.IOException;
|
| | | import java.io.InputStream;
|
| | | import java.io.OutputStream;
|
| | | import java.lang.reflect.Field;
|
| | | import java.lang.reflect.Method;
|
| | | import java.lang.reflect.ParameterizedType;
|
| | | import java.math.BigDecimal;
|
| | | import java.text.DecimalFormat;
|
| | | import java.time.LocalDate;
|
| | | import java.time.LocalDateTime;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Arrays;
|
| | | import java.util.Collection;
|
| | | import java.util.Comparator;
|
| | | import java.util.Date;
|
| | | import java.util.HashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.Set;
|
| | | import java.util.UUID;
|
| | | import java.util.stream.Collectors;
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | | import org.apache.commons.lang3.ArrayUtils;
|
| | | import org.apache.commons.lang3.RegExUtils;
|
| | | import org.apache.commons.lang3.reflect.FieldUtils;
|
| | | import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
|
| | | import org.apache.poi.hssf.usermodel.HSSFPicture;
|
| | | import org.apache.poi.hssf.usermodel.HSSFPictureData;
|
| | | import org.apache.poi.hssf.usermodel.HSSFShape;
|
| | | import org.apache.poi.hssf.usermodel.HSSFSheet;
|
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
| | | import org.apache.poi.ooxml.POIXMLDocumentPart;
|
| | | import org.apache.poi.ss.usermodel.BorderStyle;
|
| | | import org.apache.poi.ss.usermodel.Cell;
|
| | | import org.apache.poi.ss.usermodel.CellStyle;
|
| | | import org.apache.poi.ss.usermodel.CellType;
|
| | | import org.apache.poi.ss.usermodel.ClientAnchor;
|
| | | import org.apache.poi.ss.usermodel.DataValidation;
|
| | | import org.apache.poi.ss.usermodel.DataValidationConstraint;
|
| | | import org.apache.poi.ss.usermodel.DataValidationHelper;
|
| | | import org.apache.poi.ss.usermodel.DateUtil;
|
| | | import org.apache.poi.ss.usermodel.Drawing;
|
| | | import org.apache.poi.ss.usermodel.FillPatternType;
|
| | | import org.apache.poi.ss.usermodel.Font;
|
| | | import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
| | | import org.apache.poi.ss.usermodel.IndexedColors;
|
| | | import org.apache.poi.ss.usermodel.Name;
|
| | | import org.apache.poi.ss.usermodel.PictureData;
|
| | | import org.apache.poi.ss.usermodel.Row;
|
| | | import org.apache.poi.ss.usermodel.Sheet;
|
| | | import org.apache.poi.ss.usermodel.VerticalAlignment;
|
| | | import org.apache.poi.ss.usermodel.Workbook;
|
| | | import org.apache.poi.ss.usermodel.WorkbookFactory;
|
| | | import org.apache.poi.ss.util.CellRangeAddress;
|
| | | import org.apache.poi.ss.util.CellRangeAddressList;
|
| | | import org.apache.poi.util.IOUtils;
|
| | | import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
| | | import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
| | | import org.apache.poi.xssf.usermodel.XSSFDataValidation;
|
| | | import org.apache.poi.xssf.usermodel.XSSFDrawing;
|
| | | import org.apache.poi.xssf.usermodel.XSSFPicture;
|
| | | import org.apache.poi.xssf.usermodel.XSSFShape;
|
| | | import org.apache.poi.xssf.usermodel.XSSFSheet;
|
| | | import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
| | | import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
|
| | | 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;
|
| | |
| | | import com.ruoyi.common.utils.file.FileUtils;
|
| | | import com.ruoyi.common.utils.file.ImageUtils;
|
| | | import com.ruoyi.common.utils.reflect.ReflectUtils;
|
| | | import org.apache.commons.lang3.ArrayUtils;
|
| | | import org.apache.commons.lang3.RegExUtils;
|
| | | import org.apache.commons.lang3.reflect.FieldUtils;
|
| | | import org.apache.poi.hssf.usermodel.*;
|
| | | import org.apache.poi.ooxml.POIXMLDocumentPart;
|
| | | import org.apache.poi.ss.usermodel.*;
|
| | | import org.apache.poi.ss.util.CellRangeAddress;
|
| | | import org.apache.poi.ss.util.CellRangeAddressList;
|
| | | import org.apache.poi.util.IOUtils;
|
| | | import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
| | | import org.apache.poi.xssf.usermodel.*;
|
| | | import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
|
| | | import org.slf4j.Logger;
|
| | | import org.slf4j.LoggerFactory;
|
| | |
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | | import java.io.*;
|
| | | import java.lang.reflect.Field;
|
| | | import java.lang.reflect.Method;
|
| | | import java.lang.reflect.ParameterizedType;
|
| | | import java.math.BigDecimal;
|
| | | import java.text.DecimalFormat;
|
| | | import java.time.LocalDate;
|
| | | import java.time.LocalDateTime;
|
| | | import java.util.*;
|
| | | import java.util.stream.Collectors;
|
| | |
|
| | | /**
|
| | | * Excel相关处理
|
| | |
| | | public static final String[] FORMULA_STR = { "=", "-", "+", "@" };
|
| | |
|
| | | /**
|
| | | * 导出Excel时,如果有大量的字典数据,就会有大量的查询redis(打开、关闭),导致特别慢。
|
| | | * 于是使用map存储字典数据,相同的key就不需要再次去查询redis
|
| | | * 用于dictType属性数据存储,避免重复查缓存
|
| | | */
|
| | | public Map<String,String> sysDictMap = new HashMap<String,String>();
|
| | | public Map<String, String> sysDictMap = new HashMap<String, String>();
|
| | |
|
| | | /**
|
| | | * Excel sheet最大行数,默认65536
|
| | |
| | | * @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;
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | }
|
| | | // 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
|
| | | int rows = sheet.getLastRowNum();
|
| | |
|
| | | if (rows > 0)
|
| | | {
|
| | | // 定义一个map用于存放excel列的序号和field.
|
| | |
| | | {
|
| | | 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());
|
| | | }
|
| | |
| | | }
|
| | | 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))
|
| | | {
|
| | |
| | | }
|
| | | else if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotNull(value))
|
| | | {
|
| | | if (!sysDictMap.containsKey(dictType+value)){
|
| | | if (!sysDictMap.containsKey(dictType + value))
|
| | | {
|
| | | String lable = convertDictByExp(Convert.toStr(value), dictType, separator);
|
| | | sysDictMap.put(dictType+value,lable);
|
| | | sysDictMap.put(dictType + value, lable);
|
| | | }
|
| | | cell.setCellValue(sysDictMap.get(dictType+value));
|
| | | cell.setCellValue(sysDictMap.get(dictType + value));
|
| | | }
|
| | | else if (value instanceof BigDecimal && -1 != attr.scale())
|
| | | {
|
| | |
| | | }
|
| | | else if (!attr.handler().equals(ExcelHandlerAdapter.class))
|
| | | {
|
| | | cell.setCellValue(dataFormatHandlerAdapter(value, attr));
|
| | | cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell));
|
| | | }
|
| | | else
|
| | | {
|
| | |
| | | * @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)
|
| | | {
|