Skip to content

Commit

Permalink
sync ruoyi code
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Mar 22, 2024
1 parent 17186c2 commit 26c0a33
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public int value()

enum ColumnType
{
NUMERIC(0), STRING(1), IMAGE(2);
NUMERIC(0), STRING(1), IMAGE(2), TEXT(3);
private final int value;

ColumnType(int value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class SysUser extends BaseEntity
private String email;

/** 手机号码 */
@Excel(name = "手机号码")
@Excel(name = "手机号码", cellType = ColumnType.TEXT)
private String phonenumber;

/** 用户性别 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class ExcelUtil<T>
/**
* Excel sheet最大行数,默认65536
*/
public static final int sheetSize = 65536;
public static final int SHEET_SIZE = 65536;

/**
* 工作表名称
Expand Down Expand Up @@ -164,7 +164,6 @@ public ExcelUtil(Class<T> clazz)
* 隐藏Excel中列属性
*
* @param fields 列属性名 示例[单个"name"/多个"id","name"]
* @throws Exception
*/
public void hideColumn(String... fields)
{
Expand Down Expand Up @@ -485,7 +484,6 @@ public AjaxResult exportExcel(List<T> list, String sheetName, String title)
* @param response 返回数据
* @param list 导出数据集合
* @param sheetName 工作表的名称
* @return 结果
*/
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName)
{
Expand All @@ -499,7 +497,6 @@ public void exportExcel(HttpServletResponse response, List<T> list, String sheet
* @param list 导出数据集合
* @param sheetName 工作表的名称
* @param title 标题
* @return 结果
*/
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title)
{
Expand Down Expand Up @@ -536,8 +533,8 @@ public AjaxResult importTemplateExcel(String sheetName, String title)
/**
* 对list数据源将其里面的数据导入到excel表单
*
* @param response response
* @param sheetName 工作表的名称
* @return 结果
*/
public void importTemplateExcel(HttpServletResponse response, String sheetName)
{
Expand All @@ -547,9 +544,9 @@ public void importTemplateExcel(HttpServletResponse response, String sheetName)
/**
* 对list数据源将其里面的数据导入到excel表单
*
* @param response http servlert response
* @param sheetName 工作表的名称
* @param title 标题
* @return 结果
*/
public void importTemplateExcel(HttpServletResponse response, String sheetName, String title)
{
Expand All @@ -561,8 +558,6 @@ public void importTemplateExcel(HttpServletResponse response, String sheetName,

/**
* 对list数据源将其里面的数据导入到excel表单
*
* @return 结果
*/
public void exportExcel(HttpServletResponse response)
{
Expand Down Expand Up @@ -615,7 +610,7 @@ public AjaxResult exportExcel()
public void writeSheet()
{
// 取出一共有多少个sheet.
int sheetNo = Math.max(1, (int) Math.ceil(list.size() * 1.0 / sheetSize));
int sheetNo = Math.max(1, (int) Math.ceil(list.size() * 1.0 / SHEET_SIZE));
for (int index = 0; index < sheetNo; index++)
{
createSheet(sheetNo, index);
Expand Down Expand Up @@ -658,8 +653,8 @@ public void writeSheet()
@SuppressWarnings("unchecked")
public void fillExcelData(int index, Row row)
{
int startNo = index * sheetSize;
int endNo = Math.min(startNo + sheetSize, list.size());
int startNo = index * SHEET_SIZE;
int endNo = Math.min(startNo + SHEET_SIZE, list.size());
int rowNo = (1 + rownum) - startNo;
for (int i = startNo; i < endNo; i++)
{
Expand Down Expand Up @@ -738,6 +733,8 @@ private Map<String, CellStyle> createStyles(Workbook wb)
titleFont.setFontHeightInPoints((short) 16);
titleFont.setBold(true);
style.setFont(titleFont);
DataFormat dataFormat = wb.createDataFormat();
style.setDataFormat(dataFormat.getFormat("@"));
styles.put("title", style);

style = wb.createCellStyle();
Expand Down Expand Up @@ -800,6 +797,9 @@ private Map<String, CellStyle> annotationHeaderStyles(Workbook wb, Map<String, C
headerFont.setBold(true);
headerFont.setColor(excel.headerColor().index);
style.setFont(headerFont);
// 设置表格头单元格文本形式
DataFormat dataFormat = wb.createDataFormat();
style.setDataFormat(dataFormat.getFormat("@"));
headerStyles.put(key, style);
}
}
Expand All @@ -817,34 +817,66 @@ private Map<String, CellStyle> annotationDataStyles(Workbook wb)
Map<String, CellStyle> styles = new HashMap<>();
for (Object[] os : fields)
{
Field field = (Field) os[0];
Excel excel = (Excel) os[1];
String key = StringUtils.format("data_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor());
if (!styles.containsKey(key))
if (Collection.class.isAssignableFrom(field.getType()))
{
CellStyle style = wb.createCellStyle();
style.setAlignment(excel.align());
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFillForegroundColor(excel.backgroundColor().getIndex());
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
dataFont.setColor(excel.color().index);
style.setFont(dataFont);
styles.put(key, style);
ParameterizedType pt = (ParameterizedType) field.getGenericType();
Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
List<Field> subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);
for (Field subField : subFields)
{
Excel subExcel = subField.getAnnotation(Excel.class);
annotationDataStyles(styles, subField, subExcel);
}
}
else
{
annotationDataStyles(styles, field, excel);
}
}
return styles;
}

/**
* 根据Excel注解创建表格列样式
*
* @param styles 自定义样式列表
* @param field 属性列信息
* @param excel 注解信息
*/
public void annotationDataStyles(Map<String, CellStyle> styles, Field field, Excel excel)
{
String key = StringUtils.format("data_{}_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor(), excel.cellType());
if (!styles.containsKey(key))
{
CellStyle style = wb.createCellStyle();
style.setAlignment(excel.align());
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFillForegroundColor(excel.backgroundColor().getIndex());
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
dataFont.setColor(excel.color().index);
style.setFont(dataFont);
if (ColumnType.TEXT == excel.cellType())
{
DataFormat dataFormat = wb.createDataFormat();
style.setDataFormat(dataFormat.getFormat("@"));
}
styles.put(key, style);
}
}

/**
* 创建单元格
*/
Expand All @@ -859,7 +891,7 @@ public Cell createHeadCell(Excel attr, Row row, int column)
if (isSubList())
{
// 填充默认样式,防止合并单元格样式失效
sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType())));
if (attr.needMerge())
{
sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
Expand All @@ -877,7 +909,7 @@ public Cell createHeadCell(Excel attr, Row row, int column)
*/
public void setCellVo(Object value, Excel attr, Cell cell)
{
if (ColumnType.STRING == attr.cellType())
if (ColumnType.STRING == attr.cellType() || ColumnType.TEXT == attr.cellType())
{
String cellValue = Convert.toStr(value);
// 对于任何以表达式触发字符 =-+@开头的单元格,直接使用tab字符作为前缀,防止CSV注入。
Expand Down Expand Up @@ -989,7 +1021,7 @@ public Cell addCell(Excel attr, Row row, T vo, Field field, int column)
CellRangeAddress cellAddress = new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column);
sheet.addMergedRegion(cellAddress);
}
cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType())));

// 用于读取对象中的属性
Object value = getTargetValue(vo, field, attr);
Expand Down Expand Up @@ -1032,7 +1064,7 @@ else if (!attr.handler().equals(ExcelHandlerAdapter.class))
}
catch (Exception e)
{
log.error("导出Excel失败{}", e);
log.error("导出Excel失败.", e);
}
return cell;
}
Expand Down Expand Up @@ -1231,7 +1263,8 @@ public static String reverseDictByExp(String dictLabel, String dictType, String
*
* @param value 数据值
* @param excel 数据注解
* @return
* @param cell 单元格
* @return value
*/
public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell)
{
Expand All @@ -1243,7 +1276,7 @@ public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell)
}
catch (Exception e)
{
log.error("不能格式化数据 " + excel.handler(), e.getMessage());
log.error("不能格式化数据 " + excel.handler(), e);
}
return Convert.toStr(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void testNewArray() {
assertThat(array2).hasSize(9);
}

private static final String test = "xxx";
private static String test = "xxx";
private static final String STR = "123";

@Test
Expand All @@ -71,6 +71,8 @@ public void testReflect() throws IllegalAccessException {
FieldUtils.writeField(f, (Object) null, "yyy", true);
assertThat("yyy").isEqualTo(test);
assertThat("yyy").isEqualTo(FieldUtils.readField(f, (Object) null));
test = "123";
assertThat("123").isEqualTo(test);

// static final field
Field f1 = FieldUtils.getField(CollectsTest.class, "STR", true);
Expand Down

0 comments on commit 26c0a33

Please sign in to comment.