|
|
|
@ -1,7 +1,47 @@
|
|
|
|
|
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.math.BigDecimal;
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFFont;
|
|
|
|
|
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
|
|
|
|
|
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.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.FillPatternType;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Font;
|
|
|
|
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
|
|
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.CellRangeAddressList;
|
|
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
|
|
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.Type;
|
|
|
|
|
import com.ruoyi.common.annotation.Excels;
|
|
|
|
|
import com.ruoyi.common.config.Global;
|
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
|
import com.ruoyi.common.core.text.Convert;
|
|
|
|
@ -9,21 +49,6 @@ import com.ruoyi.common.exception.BusinessException;
|
|
|
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
|
import com.ruoyi.common.utils.reflect.ReflectUtils;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFFont;
|
|
|
|
|
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Excel相关处理
|
|
|
|
@ -153,7 +178,6 @@ public class ExcelUtil<T>
|
|
|
|
|
cellMap.put(null, i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 有数据时才处理 得到类的所有field.
|
|
|
|
|
Field[] allFields = clazz.getDeclaredFields();
|
|
|
|
|
// 定义一个map用于存放列的序号和field.
|
|
|
|
@ -289,65 +313,31 @@ public class ExcelUtil<T>
|
|
|
|
|
for (int index = 0; index <= sheetNo; index++)
|
|
|
|
|
{
|
|
|
|
|
createSheet(sheetNo, index);
|
|
|
|
|
Cell cell = null; // 产生单元格
|
|
|
|
|
|
|
|
|
|
// 产生一行
|
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
|
int excelsNo = 0;
|
|
|
|
|
// 写入各个字段的列头名称
|
|
|
|
|
for (int i = 0; i < fields.size(); i++)
|
|
|
|
|
for (int column = 0; column < fields.size(); column++)
|
|
|
|
|
{
|
|
|
|
|
Field field = fields.get(i);
|
|
|
|
|
Excel attr = field.getAnnotation(Excel.class);
|
|
|
|
|
// 创建列
|
|
|
|
|
cell = row.createCell(i);
|
|
|
|
|
// 设置列中写入内容为String类型
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
CellStyle cellStyle = wb.createCellStyle();
|
|
|
|
|
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
if (attr.name().indexOf("注:") >= 0)
|
|
|
|
|
Field field = fields.get(column);
|
|
|
|
|
if (field.isAnnotationPresent(Excel.class))
|
|
|
|
|
{
|
|
|
|
|
Font font = wb.createFont();
|
|
|
|
|
font.setColor(HSSFFont.COLOR_RED);
|
|
|
|
|
cellStyle.setFont(font);
|
|
|
|
|
cellStyle.setFillForegroundColor(HSSFColorPredefined.YELLOW.getIndex());
|
|
|
|
|
sheet.setColumnWidth(i, 6000);
|
|
|
|
|
Excel excel = field.getAnnotation(Excel.class);
|
|
|
|
|
createCell(excel, row, column);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
if (field.isAnnotationPresent(Excels.class))
|
|
|
|
|
{
|
|
|
|
|
Font font = wb.createFont();
|
|
|
|
|
// 粗体显示
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
// 选择需要用到的字体格式
|
|
|
|
|
cellStyle.setFont(font);
|
|
|
|
|
cellStyle.setFillForegroundColor(HSSFColorPredefined.LIGHT_YELLOW.getIndex());
|
|
|
|
|
// 设置列宽
|
|
|
|
|
sheet.setColumnWidth(i, (int) ((attr.width() + 0.72) * 256));
|
|
|
|
|
row.setHeight((short) (attr.height() * 20));
|
|
|
|
|
}
|
|
|
|
|
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
cellStyle.setWrapText(true);
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
|
|
|
|
|
Excels attrs = field.getAnnotation(Excels.class);
|
|
|
|
|
Excel[] excels = attrs.value();
|
|
|
|
|
// 写入列名
|
|
|
|
|
cell.setCellValue(attr.name());
|
|
|
|
|
|
|
|
|
|
// 如果设置了提示信息则鼠标放上去提示.
|
|
|
|
|
if (StringUtils.isNotEmpty(attr.prompt()))
|
|
|
|
|
{
|
|
|
|
|
// 这里默认设了2-101列提示.
|
|
|
|
|
setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, i, i);
|
|
|
|
|
}
|
|
|
|
|
// 如果设置了combo属性则本列只能选择不能输入
|
|
|
|
|
if (attr.combo().length > 0)
|
|
|
|
|
{
|
|
|
|
|
// 这里默认设了2-101列只能选择不能输入.
|
|
|
|
|
setXSSFValidation(sheet, attr.combo(), 1, 100, i, i);
|
|
|
|
|
Excel excel = excels[excelsNo++];
|
|
|
|
|
createCell(excel, row, column);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (Type.EXPORT.equals(type))
|
|
|
|
|
{
|
|
|
|
|
fillExcelData(index, row, cell);
|
|
|
|
|
fillExcelData(index, row);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
String filename = encodingFilename(sheetName);
|
|
|
|
@ -394,7 +384,7 @@ public class ExcelUtil<T>
|
|
|
|
|
* @param row 单元格行
|
|
|
|
|
* @param cell 类型单元格
|
|
|
|
|
*/
|
|
|
|
|
public void fillExcelData(int index, Row row, Cell cell)
|
|
|
|
|
public void fillExcelData(int index, Row row)
|
|
|
|
|
{
|
|
|
|
|
int startNo = index * sheetSize;
|
|
|
|
|
int endNo = Math.min(startNo + sheetSize, list.size());
|
|
|
|
@ -407,13 +397,95 @@ public class ExcelUtil<T>
|
|
|
|
|
row = sheet.createRow(i + 1 - startNo);
|
|
|
|
|
// 得到导出对象.
|
|
|
|
|
T vo = (T) list.get(i);
|
|
|
|
|
for (int j = 0; j < fields.size(); j++)
|
|
|
|
|
int excelsNo = 0;
|
|
|
|
|
for (int column = 0; column < fields.size(); column++)
|
|
|
|
|
{
|
|
|
|
|
// 获得field.
|
|
|
|
|
Field field = fields.get(j);
|
|
|
|
|
Field field = fields.get(column);
|
|
|
|
|
// 设置实体类私有属性可访问
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
Excel attr = field.getAnnotation(Excel.class);
|
|
|
|
|
if (field.isAnnotationPresent(Excel.class))
|
|
|
|
|
{
|
|
|
|
|
addCell(field.getAnnotation(Excel.class), row, vo, field, column, cs);
|
|
|
|
|
}
|
|
|
|
|
if (field.isAnnotationPresent(Excels.class))
|
|
|
|
|
{
|
|
|
|
|
Excels attrs = field.getAnnotation(Excels.class);
|
|
|
|
|
Excel[] excels = attrs.value();
|
|
|
|
|
Excel excel = excels[excelsNo++];
|
|
|
|
|
addCell(excel, row, vo, field, column, cs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建单元格
|
|
|
|
|
*/
|
|
|
|
|
public Cell createCell(Excel attr, Row row, int column)
|
|
|
|
|
{
|
|
|
|
|
// 创建列
|
|
|
|
|
Cell cell = row.createCell(column);
|
|
|
|
|
// 设置列中写入内容为String类型
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
// 写入列名
|
|
|
|
|
cell.setCellValue(attr.name());
|
|
|
|
|
CellStyle cellStyle = createStyle(attr, row, column);
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建表格样式
|
|
|
|
|
*/
|
|
|
|
|
public CellStyle createStyle(Excel attr, Row row, int column)
|
|
|
|
|
{
|
|
|
|
|
CellStyle cellStyle = wb.createCellStyle();
|
|
|
|
|
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
if (attr.name().indexOf("注:") >= 0)
|
|
|
|
|
{
|
|
|
|
|
Font font = wb.createFont();
|
|
|
|
|
font.setColor(HSSFFont.COLOR_RED);
|
|
|
|
|
cellStyle.setFont(font);
|
|
|
|
|
cellStyle.setFillForegroundColor(HSSFColorPredefined.YELLOW.getIndex());
|
|
|
|
|
sheet.setColumnWidth(column, 6000);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Font font = wb.createFont();
|
|
|
|
|
// 粗体显示
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
// 选择需要用到的字体格式
|
|
|
|
|
cellStyle.setFont(font);
|
|
|
|
|
cellStyle.setFillForegroundColor(HSSFColorPredefined.LIGHT_YELLOW.getIndex());
|
|
|
|
|
// 设置列宽
|
|
|
|
|
sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
|
|
|
|
|
row.setHeight((short) (attr.height() * 20));
|
|
|
|
|
}
|
|
|
|
|
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
cellStyle.setWrapText(true);
|
|
|
|
|
// 如果设置了提示信息则鼠标放上去提示.
|
|
|
|
|
if (StringUtils.isNotEmpty(attr.prompt()))
|
|
|
|
|
{
|
|
|
|
|
// 这里默认设了2-101列提示.
|
|
|
|
|
setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
|
|
|
|
|
}
|
|
|
|
|
// 如果设置了combo属性则本列只能选择不能输入
|
|
|
|
|
if (attr.combo().length > 0)
|
|
|
|
|
{
|
|
|
|
|
// 这里默认设了2-101列只能选择不能输入.
|
|
|
|
|
setXSSFValidation(sheet, attr.combo(), 1, 100, column, column);
|
|
|
|
|
}
|
|
|
|
|
return cellStyle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加单元格
|
|
|
|
|
*/
|
|
|
|
|
public Cell addCell(Excel attr, Row row, T vo, Field field, int column, CellStyle cs)
|
|
|
|
|
{
|
|
|
|
|
Cell cell = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 设置行高
|
|
|
|
@ -422,14 +494,8 @@ public class ExcelUtil<T>
|
|
|
|
|
if (attr.isExport())
|
|
|
|
|
{
|
|
|
|
|
// 创建cell
|
|
|
|
|
cell = row.createCell(j);
|
|
|
|
|
cell = row.createCell(column);
|
|
|
|
|
cell.setCellStyle(cs);
|
|
|
|
|
if (vo == null)
|
|
|
|
|
{
|
|
|
|
|
// 如果数据存在就填入,不存在填入空格.
|
|
|
|
|
cell.setCellValue("");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 用于读取对象中的属性
|
|
|
|
|
Object value = getTargetValue(vo, field, attr);
|
|
|
|
@ -455,8 +521,7 @@ public class ExcelUtil<T>
|
|
|
|
|
{
|
|
|
|
|
log.error("导出Excel失败{}", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -657,33 +722,39 @@ public class ExcelUtil<T>
|
|
|
|
|
{
|
|
|
|
|
this.fields = new ArrayList<Field>();
|
|
|
|
|
List<Field> tempFields = new ArrayList<>();
|
|
|
|
|
Class<?> tempClass = clazz;
|
|
|
|
|
tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
|
|
|
|
|
tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
|
|
|
|
|
while (tempClass != null)
|
|
|
|
|
for (Field field : tempFields)
|
|
|
|
|
{
|
|
|
|
|
tempClass = tempClass.getSuperclass();
|
|
|
|
|
if (tempClass != null)
|
|
|
|
|
// 单注解
|
|
|
|
|
if (field.isAnnotationPresent(Excel.class))
|
|
|
|
|
{
|
|
|
|
|
tempFields.addAll(Arrays.asList(tempClass.getDeclaredFields()));
|
|
|
|
|
putToField(field, field.getAnnotation(Excel.class));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 多注解
|
|
|
|
|
if (field.isAnnotationPresent(Excels.class))
|
|
|
|
|
{
|
|
|
|
|
Excels attrs = field.getAnnotation(Excels.class);
|
|
|
|
|
Excel[] excels = attrs.value();
|
|
|
|
|
for (Excel excel : excels)
|
|
|
|
|
{
|
|
|
|
|
putToField(field, excel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
putToFields(tempFields);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 放到字段集合中
|
|
|
|
|
*/
|
|
|
|
|
private void putToFields(List<Field> fields)
|
|
|
|
|
{
|
|
|
|
|
for (Field field : fields)
|
|
|
|
|
private void putToField(Field field, Excel attr)
|
|
|
|
|
{
|
|
|
|
|
Excel attr = field.getAnnotation(Excel.class);
|
|
|
|
|
if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
|
|
|
|
|
{
|
|
|
|
|
this.fields.add(field);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建一个工作簿
|
|
|
|
|