package com.ruoyi.common.core.utils.poi;
im
port java.io.IOException;
im
port java.io.InputStream;
im
port java.lang.reflect.Field;
im
port java.lang.reflect.Method;
im
port java.lang.reflect.ParameterizedType;
im
port java.math.BigDecimal;
im
port java.text.DecimalFormat;
im
port java.time.LocalDate;
im
port java.time.LocalDateTime;
im
port java.util.ArrayList;
im
port java.util.Arrays;
im
port java.util.Collection;
im
port java.util.Comparator;
im
port java.util.Date;
im
port java.util.HashMap;
im
port java.util.List;
im
port java.util.Map;
im
port java.util.Set;
im
port java.util.stream.Collectors;
im
port javax.servlet.http.HttpServletResponse;
im
port org.apache.commons.lang3.ArrayUtils;
im
port org.apache.commons.lang3.RegExUtils;
im
port org.apache.commons.lang3.reflect.FieldUtils;
im
port org.apache.poi.ss.usermodel.BorderStyle;
im
port org.apache.poi.ss.usermodel.Cell;
im
port org.apache.poi.ss.usermodel.CellStyle;
im
port org.apache.poi.ss.usermodel.CellType;
im
port org.apache.poi.ss.usermodel.ClientAnchor;
im
port org.apache.poi.ss.usermodel.DataValidation;
im
port org.apache.poi.ss.usermodel.DataValidationConstraint;
im
port org.apache.poi.ss.usermodel.DataValidationHelper;
im
port org.apache.poi.ss.usermodel.DateUtil;
im
port org.apache.poi.ss.usermodel.Drawing;
im
port org.apache.poi.ss.usermodel.FillPatternType;
im
port org.apache.poi.ss.usermodel.Font;
im
port org.apache.poi.ss.usermodel.HorizontalAlignment;
im
port org.apache.poi.ss.usermodel.IndexedColors;
im
port org.apache.poi.ss.usermodel.Name;
im
port org.apache.poi.ss.usermodel.Row;
im
port org.apache.poi.ss.usermodel.Sheet;
im
port org.apache.poi.ss.usermodel.VerticalAlignment;
im
port org.apache.poi.ss.usermodel.Workbook;
im
port org.apache.poi.ss.usermodel.WorkbookFactory;
im
port org.apache.poi.ss.util.CellRangeAddress;
im
port org.apache.poi.ss.util.CellRangeAddressList;
im
port org.apache.poi.util.IOUtils;
im
port org.apache.poi.xssf.streaming.SXSSFWorkbook;
im
port org.apache.poi.xssf.usermodel.XSSFClientAnchor;
im
port org.apache.poi.xssf.usermodel.XSSFDataValidation;
im
port org.slf4j.Logger;
im
port org.slf4j.LoggerFactory;
im
port com.ruoyi.common.core.annotation.Excel;
im
port com.ruoyi.common.core.annotation.Excel.ColumnType;
im
port com.ruoyi.common.core.annotation.Excel.Type;
im
port com.ruoyi.common.core.annotation.Excels;
im
port com.ruoyi.common.core.text.Convert;
im
port com.ruoyi.common.core.utils.DateUtils;
im
port com.ruoyi.common.core.utils.StringUtils;
im
port com.ruoyi.common.core.utils.file.FileTypeUtils;
im
port com.ruoyi.common.core.utils.file.ImageUtils;
im
port com.ruoyi.common.core.utils.reflect.ReflectUtils;
public class ExcelUtil<T>
{
private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);
public static final String FORMULA_REGEX_STR = "=|-|\+|@";
public static final String[] FORMULA_STR = { "=", "-", "+", "@" };
public static final int sheetSize = 65536;
private String sheetName;
private Type type;
private Workbook wb;
private Sheet sheet;
private Map<String, CellStyle> styles;
private List<T> list;
private List<Object[]> fields;
private int rownum;
private String title;
private short maxHeight;
private int subMergedLastRowNum = 0;
private int subMergedFirstRowNum = 1;
private Method subMethod;
private List<Field> subFields;
private Map<Integer, Double> statistics = new HashMap<Integer, Double>();
private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00");
public Class<T> clazz;
public String[] excludeFields;
public ExcelUtil(Class<T> clazz)
{
this.clazz = clazz;
}
public void hideColumn(String... fields)
{
this.excludeFields = fields;
}
public void init(List<T> list, String sheetName, String title, Type type)
{
if (list == null)
{
list = new ArrayList<T>();
}
this.list = list;
this.sheetName = sheetName;
this.type = type;
this.title = title;
createExcelField();
createWorkbook();
createTitle();
createSubHead();
}
public void createTitle()
{
if (StringUtils.isNotEmpty(title))
{
subMergedFirstRowNum++;
subMergedLastRowNum++;
int titleLastCol = this.fields.size() - 1;
if (isSubList())
{
titleLastCol = titleLastCol + subFields.size() - 1;
}
Row titleRow = sheet.createRow(rownum == 0 ? rownum++ : 0);
titleRow.setHeightInPoints(30);
Cell titleCell = titleRow.createCell(0);
titleCell.setCellStyle(styles.get("title"));
titleCell.setCellValue(title);
sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), titleLastCol));
}
}
public void createSubHead()
{
if (isSubList())
{
subMergedFirstRowNum++;
subMergedLastRowNum++;
Row subRow = sheet.createRow(rownum);
int excelNum = 0;
for (Object[] objects : fields)
{
Excel attr = (Excel) objects[1];
Cell headCell1 = subRow.createCell(excelNum);
headCell1.setCellValue(attr.name());
headCell1.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
excelNum++;
}
int headFirstRow = excelNum - 1;
int headLastRow = headFirstRow + subFields.size() - 1;
if (headLastRow > headFirstRow)
{
sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, headFirstRow, headLastRow));
}
rownum++;
}
}
public List<T> im
portExcel(InputStream is) throws Exception
{
return im
portExcel(is, 0);
}
public List<T> im
portExcel(InputStream is, int titleNum) throws Exception
{
return im
portExcel(StringUtils.EMPTY, is, titleNum);
}
public List<T> im
portExcel(String sheetName, InputStream is, int titleNum) throws Exception
{
this.type = Type.im
port;
this.wb = WorkbookFactory.create(is);