fix():格式规范修改

boot3.0
dongzeliang 2025-02-26 22:57:49 +08:00
parent fdd5cd030f
commit 3f32325c9b
9 changed files with 184 additions and 109 deletions

View File

@ -152,7 +152,12 @@ public @interface Excel {
* *
*/ */
enum Type { enum Type {
ALL(0), EXPORT(1), IMPORT(2); // 所有
ALL(0),
// 仅导出
EXPORT(1),
// 仅导入
IMPORT(2);
private final int value; private final int value;
Type (int value) { Type (int value) {
@ -168,7 +173,18 @@ public @interface Excel {
* *
*/ */
enum ColumnType { enum ColumnType {
NUMERIC(0), STRING(1), IMAGE(2); /**
*
*/
NUMERIC(0),
/**
*
*/
STRING(1),
/**
*
*/
IMAGE(2);
private final int value; private final int value;
ColumnType (int value) { ColumnType (int value) {

View File

@ -0,0 +1,44 @@
package com.muyu.common.core.constant;
/**
* @author dongzeliang
* @version 1.0
* @description:
* @date 2025/2/26 22:56
*/
public class FileConstants {
// GIF
public static final int PHOTO_BYTE_GIF_ZERO = 71;
// GIF
public static final int PHOTO_BYTE_GIF_ONE = 73;
// GIF
public static final int PHOTO_BYTE_GIF_TWO = 70;
// GIF
public static final int PHOTO_BYTE_GIF_THREE = 56;
// GIF
public static final int PHOTO_BYTE_GIF_FOUR = 55;
// GIF
public static final int PHOTO_BYTE_GIF_FOUR_OR = 57;
// GIF
public static final int PHOTO_BYTE_GIF_FIVE = 97;
// JPG
public static final int PHOTO_BYTE_JPG_SIX = 74;
// JPG
public static final int PHOTO_BYTE_JPG_SEVEN = 70;
// JPG
public static final int PHOTO_BYTE_JPG_EIGHT = 73;
// JPG
public static final int PHOTO_BYTE_JPG_NINE = 70;
// BMP
public static final int PHOTO_BYTE_BMP_ZERO = 66;
// BMP
public static final int PHOTO_BYTE_BMP_ONE = 77;
// PNG
public static final int PHOTO_BYTE_PNG_ONE = 80;
// PNG
public static final int PHOTO_BYTE_PNG_TWO = 78;
// PNG
public static final int PHOTO_BYTE_PNG_THREE = 71;
}

View File

@ -0,0 +1,28 @@
package com.muyu.common.core.constant;
/**
* @author dongzeliang
* @version 1.0
* @description: HTTP
* @date 2025/2/26 22:37
*/
public class HttpConstants {
// json请求
public static final String APPLICATION_JSON = "application/json";
// ACCEPT
public static final String ACCEPT = "accept";
// 请求
public static final String X_REQUESTED_WITH = "X-Requested-With";
// XMLHttpRequest
public static final String XML_HTTP_REQUEST = "XMLHttpRequest";
// 内容忽略
public static final String[] XML_HTTP_REQUEST_IGNORE = new String[]{".json", ".xml"};
// ajax
public static final String AJAX = "__ajax";
// ajax内容忽略
public static final String[] AJAX_IGNORE = new String[]{"json", "xml"};
}

View File

@ -12,7 +12,13 @@ import lombok.Getter;
@Getter @Getter
public enum SysWhetherEnum { public enum SysWhetherEnum {
/**
*
*/
YES("Y", "是"), YES("Y", "是"),
/**
*
*/
NO("N", "否"); NO("N", "否");
private final String code; private final String code;
private final String info; private final String info;

View File

@ -1,12 +1,26 @@
package com.muyu.common.core.enums; package com.muyu.common.core.enums;
import lombok.Getter;
/** /**
* *
* *
* @author muyu * @author muyu
*/ */
@Getter
public enum UserStatusEnum { public enum UserStatusEnum {
OK("0", "正常"), DISABLE("1", "停用"), DELETED("2", "删除"); /**
*
*/
OK("0", "正常"),
/**
*
*/
DISABLE("1", "停用"),
/**
*
*/
DELETED("2", "删除");
private final String code; private final String code;
private final String info; private final String info;
@ -16,11 +30,4 @@ public enum UserStatusEnum {
this.info = info; this.info = info;
} }
public String getCode () {
return code;
}
public String getInfo () {
return info;
}
} }

View File

@ -1,7 +1,6 @@
package com.muyu.common.core.utils; package com.muyu.common.core.utils;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.muyu.common.core.constant.Constants;
import com.muyu.common.core.domain.Result; import com.muyu.common.core.domain.Result;
import com.muyu.common.core.text.Convert; import com.muyu.common.core.text.Convert;
import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletRequest;
@ -21,12 +20,14 @@ import reactor.core.publisher.Mono;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import static com.muyu.common.core.constant.HttpConstants.*;
import static org.springframework.http.HttpHeaders.ACCEPT;
/** /**
* *
* *
@ -37,42 +38,42 @@ public class ServletUtils {
* String * String
*/ */
public static String getParameter (String name) { public static String getParameter (String name) {
return getRequest().getParameter(name); return Objects.requireNonNull(getRequest()).getParameter(name);
} }
/** /**
* String * String
*/ */
public static String getParameter (String name, String defaultValue) { public static String getParameter (String name, String defaultValue) {
return Convert.toStr(getRequest().getParameter(name), defaultValue); return Convert.toStr(Objects.requireNonNull(getRequest()).getParameter(name), defaultValue);
} }
/** /**
* Integer * Integer
*/ */
public static Integer getParameterToInt (String name) { public static Integer getParameterToInt (String name) {
return Convert.toInt(getRequest().getParameter(name)); return Convert.toInt(Objects.requireNonNull(getRequest()).getParameter(name));
} }
/** /**
* Integer * Integer
*/ */
public static Integer getParameterToInt (String name, Integer defaultValue) { public static Integer getParameterToInt (String name, Integer defaultValue) {
return Convert.toInt(getRequest().getParameter(name), defaultValue); return Convert.toInt(Objects.requireNonNull(getRequest()).getParameter(name), defaultValue);
} }
/** /**
* Boolean * Boolean
*/ */
public static Boolean getParameterToBool (String name) { public static Boolean getParameterToBool (String name) {
return Convert.toBool(getRequest().getParameter(name)); return Convert.toBool(Objects.requireNonNull(getRequest()).getParameter(name));
} }
/** /**
* Boolean * Boolean
*/ */
public static Boolean getParameterToBool (String name, Boolean defaultValue) { public static Boolean getParameterToBool (String name, Boolean defaultValue) {
return Convert.toBool(getRequest().getParameter(name), defaultValue); return Convert.toBool(Objects.requireNonNull(getRequest()).getParameter(name), defaultValue);
} }
/** /**
@ -170,8 +171,8 @@ public class ServletUtils {
public static void renderString (HttpServletResponse response, String string) { public static void renderString (HttpServletResponse response, String string) {
try { try {
response.setStatus(200); response.setStatus(200);
response.setContentType("application/json"); response.setContentType(APPLICATION_JSON);
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding(StandardCharsets.UTF_8);
response.getWriter().print(string); response.getWriter().print(string);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -181,26 +182,26 @@ public class ServletUtils {
/** /**
* Ajax * Ajax
* *
* @param request * @param request
*/ */
public static boolean isAjaxRequest (HttpServletRequest request) { public static boolean isAjaxRequest (HttpServletRequest request) {
String accept = request.getHeader("accept"); String accept = request.getHeader(ACCEPT);
if (accept != null && accept.contains("application/json")) { if (accept != null && accept.contains(APPLICATION_JSON)) {
return true; return true;
} }
String xRequestedWith = request.getHeader("X-Requested-With"); String xRequestedWith = request.getHeader(X_REQUESTED_WITH);
if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) { if (xRequestedWith != null && xRequestedWith.contains(XML_HTTP_REQUEST)) {
return true; return true;
} }
String uri = request.getRequestURI(); String uri = request.getRequestURI();
if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml")) { if (StringUtils.inStringIgnoreCase(uri, XML_HTTP_REQUEST_IGNORE)) {
return true; return true;
} }
String ajax = request.getParameter("__ajax"); String ajax = request.getParameter(AJAX);
return StringUtils.inStringIgnoreCase(ajax, "json", "xml"); return StringUtils.inStringIgnoreCase(ajax, AJAX_IGNORE);
} }
/** /**
@ -211,11 +212,7 @@ public class ServletUtils {
* @return * @return
*/ */
public static String urlEncode (String str) { public static String urlEncode (String str) {
try { return URLEncoder.encode(str, StandardCharsets.UTF_8);
return URLEncoder.encode(str, Constants.UTF8);
} catch (UnsupportedEncodingException e) {
return StringUtils.EMPTY;
}
} }
/** /**

View File

@ -311,9 +311,9 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
// 前置字符是否大写 // 前置字符是否大写
boolean preCharIsUpperCase = true; boolean preCharIsUpperCase;
// 当前字符是否大写 // 当前字符是否大写
boolean curreCharIsUpperCase = true; boolean curreCharIsUpperCase;
// 下一字符是否大写 // 下一字符是否大写
boolean nexteCharIsUpperCase = true; boolean nexteCharIsUpperCase = true;
for (int i = 0 ; i < str.length() ; i++) { for (int i = 0 ; i < str.length() ; i++) {
@ -345,13 +345,13 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* *
* *
* @param str * @param str
* @param strs * @param strArr
* *
* @return true * @return true
*/ */
public static boolean inStringIgnoreCase (String str, String... strs) { public static boolean inStringIgnoreCase (String str, String... strArr) {
if (str != null && strs != null) { if (str != null && strArr != null) {
for (String s : strs) { for (String s : strArr) {
if (str.equalsIgnoreCase(trim(s))) { if (str.equalsIgnoreCase(trim(s))) {
return true; return true;
} }
@ -448,8 +448,6 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* *
* @param pattern * @param pattern
* @param url url * @param url url
*
* @return
*/ */
public static boolean isMatch (String pattern, String url) { public static boolean isMatch (String pattern, String url) {
AntPathMatcher matcher = new AntPathMatcher(); AntPathMatcher matcher = new AntPathMatcher();
@ -469,7 +467,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* *
* @return * @return
*/ */
public static final String padl (final Number num, final int size) { public static String padl(final Number num, final int size) {
return padl(num.toString(), size, '0'); return padl(num.toString(), size, '0');
} }
@ -482,7 +480,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* *
* @return * @return
*/ */
public static final String padl (final String s, final int size, final char c) { public static String padl(final String s, final int size, final char c) {
final StringBuilder sb = new StringBuilder(size); final StringBuilder sb = new StringBuilder(size);
if (s != null) { if (s != null) {
final int len = s.length(); final int len = s.length();

View File

@ -7,6 +7,8 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.util.Objects; import java.util.Objects;
import static com.muyu.common.core.constant.FileConstants.*;
/** /**
* *
* *
@ -53,7 +55,7 @@ public class FileTypeUtils {
* *
* @return * @return
*/ */
public static final String getExtension (MultipartFile file) { public static String getExtension(MultipartFile file) {
String extension = FilenameUtils.getExtension(file.getOriginalFilename()); String extension = FilenameUtils.getExtension(file.getOriginalFilename());
if (StringUtils.isEmpty(extension)) { if (StringUtils.isEmpty(extension)) {
extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType())); extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
@ -70,14 +72,14 @@ public class FileTypeUtils {
*/ */
public static String getFileExtendName (byte[] photoByte) { public static String getFileExtendName (byte[] photoByte) {
String strFileExtendName = "JPG"; String strFileExtendName = "JPG";
if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56) if ((photoByte[0] == PHOTO_BYTE_GIF_ZERO) && (photoByte[1] == PHOTO_BYTE_GIF_ONE) && (photoByte[2] == PHOTO_BYTE_GIF_TWO) && (photoByte[3] == PHOTO_BYTE_GIF_THREE)
&& ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97)) { && ((photoByte[4] == PHOTO_BYTE_GIF_FOUR) || (photoByte[4] == PHOTO_BYTE_GIF_FOUR_OR)) && (photoByte[5] == PHOTO_BYTE_GIF_FIVE)) {
strFileExtendName = "GIF"; strFileExtendName = "GIF";
} else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70)) { } else if ((photoByte[6] == PHOTO_BYTE_JPG_SIX) && (photoByte[7] == PHOTO_BYTE_JPG_SEVEN) && (photoByte[8] == PHOTO_BYTE_JPG_EIGHT) && (photoByte[9] == PHOTO_BYTE_JPG_NINE)) {
strFileExtendName = "JPG"; strFileExtendName = "JPG";
} else if ((photoByte[0] == 66) && (photoByte[1] == 77)) { } else if ((photoByte[0] == PHOTO_BYTE_BMP_ZERO) && (photoByte[1] == PHOTO_BYTE_BMP_ONE)) {
strFileExtendName = "BMP"; strFileExtendName = "BMP";
} else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71)) { } else if ((photoByte[1] == PHOTO_BYTE_PNG_ONE) && (photoByte[2] == PHOTO_BYTE_PNG_TWO) && (photoByte[3] == PHOTO_BYTE_PNG_THREE)) {
strFileExtendName = "PNG"; strFileExtendName = "PNG";
} }
return strFileExtendName; return strFileExtendName;

View File

@ -154,7 +154,7 @@ public class ExcelUtil<T> {
if (StringUtils.containsAny(propertyValue, separator)) { if (StringUtils.containsAny(propertyValue, separator)) {
for (String value : propertyValue.split(separator)) { for (String value : propertyValue.split(separator)) {
if (itemArray[0].equals(value)) { if (itemArray[0].equals(value)) {
propertyString.append(itemArray[1] + separator); propertyString.append(itemArray[1]).append(separator);
break; break;
} }
} }
@ -184,7 +184,7 @@ public class ExcelUtil<T> {
if (StringUtils.containsAny(propertyValue, separator)) { if (StringUtils.containsAny(propertyValue, separator)) {
for (String value : propertyValue.split(separator)) { for (String value : propertyValue.split(separator)) {
if (itemArray[1].equals(value)) { if (itemArray[1].equals(value)) {
propertyString.append(itemArray[0] + separator); propertyString.append(itemArray[0]).append(separator);
break; break;
} }
} }
@ -201,8 +201,6 @@ public class ExcelUtil<T> {
* Excel * Excel
* *
* @param fields ["name"/"id","name"] * @param fields ["name"/"id","name"]
*
* @throws Exception
*/ */
public void hideColumn (String... fields) { public void hideColumn (String... fields) {
this.excludeFields = fields; this.excludeFields = fields;
@ -275,7 +273,7 @@ public class ExcelUtil<T> {
* @return * @return
*/ */
public List<T> importExcel (InputStream is) { public List<T> importExcel (InputStream is) {
List<T> list = null; List<T> list;
try { try {
list = importExcel(is, 0); list = importExcel(is, 0);
} catch (Exception e) { } catch (Exception e) {
@ -393,18 +391,16 @@ public class ExcelUtil<T> {
} else if (Boolean.TYPE == fieldType || Boolean.class == fieldType) { } else if (Boolean.TYPE == fieldType || Boolean.class == fieldType) {
val = Convert.toBool(val, false); val = Convert.toBool(val, false);
} }
if (StringUtils.isNotNull(fieldType)) { String propertyName = field.getName();
String propertyName = field.getName(); if (StringUtils.isNotEmpty(attr.targetAttr())) {
if (StringUtils.isNotEmpty(attr.targetAttr())) { propertyName = field.getName() + "." + attr.targetAttr();
propertyName = field.getName() + "." + attr.targetAttr();
}
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, null);
}
ReflectUtils.invokeSetter(entity, propertyName, val);
} }
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, null);
}
ReflectUtils.invokeSetter(entity, propertyName, val);
} }
list.add(entity); list.add(entity);
} }
@ -418,8 +414,6 @@ public class ExcelUtil<T> {
* @param response * @param response
* @param list * @param list
* @param sheetName * @param sheetName
*
* @return
*/ */
public void exportExcel (HttpServletResponse response, List<T> list, String sheetName) { public void exportExcel (HttpServletResponse response, List<T> list, String sheetName) {
exportExcel(response, list, sheetName, StringUtils.EMPTY); exportExcel(response, list, sheetName, StringUtils.EMPTY);
@ -433,7 +427,6 @@ public class ExcelUtil<T> {
* @param sheetName * @param sheetName
* @param title * @param title
* *
* @return
*/ */
public void exportExcel (HttpServletResponse response, List<T> list, String sheetName, String title) { public void exportExcel (HttpServletResponse response, List<T> list, String sheetName, String title) {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
@ -446,8 +439,6 @@ public class ExcelUtil<T> {
* listexcel * listexcel
* *
* @param sheetName * @param sheetName
*
* @return
*/ */
public void importTemplateExcel (HttpServletResponse response, String sheetName) { public void importTemplateExcel (HttpServletResponse response, String sheetName) {
importTemplateExcel(response, sheetName, StringUtils.EMPTY); importTemplateExcel(response, sheetName, StringUtils.EMPTY);
@ -458,8 +449,6 @@ public class ExcelUtil<T> {
* *
* @param sheetName * @param sheetName
* @param title * @param title
*
* @return
*/ */
public void importTemplateExcel (HttpServletResponse response, String sheetName, String title) { public void importTemplateExcel (HttpServletResponse response, String sheetName, String title) {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
@ -470,8 +459,6 @@ public class ExcelUtil<T> {
/** /**
* listexcel * listexcel
*
* @return
*/ */
public void exportExcel (HttpServletResponse response) { public void exportExcel (HttpServletResponse response) {
try { try {
@ -510,7 +497,7 @@ public class ExcelUtil<T> {
} }
} }
if (Type.EXPORT.equals(type)) { if (Type.EXPORT.equals(type)) {
fillExcelData(index, row); fillExcelData(index);
addStatisticsRow(); addStatisticsRow();
} }
} }
@ -520,18 +507,17 @@ public class ExcelUtil<T> {
* excel * excel
* *
* @param index * @param index
* @param row
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void fillExcelData (int index, Row row) { public void fillExcelData (int index) {
int startNo = index * SHEET_SIZE; int startNo = index * SHEET_SIZE;
int endNo = Math.min(startNo + SHEET_SIZE, list.size()); int endNo = Math.min(startNo + SHEET_SIZE, list.size());
int rowNo = (1 + rownum) - startNo; int rowNo = (1 + rownum) - startNo;
for (int i = startNo ; i < endNo ; i++) { for (int i = startNo ; i < endNo ; i++) {
rowNo = isSubList() ? (i > 1 ? rowNo + 1 : rowNo + i) : i + 1 + rownum - startNo; rowNo = isSubList() ? (i > 1 ? rowNo + 1 : rowNo + i) : i + 1 + rownum - startNo;
row = sheet.createRow(rowNo); Row row = sheet.createRow(rowNo);
// 得到导出对象. // 得到导出对象.
T vo = (T) list.get(i); T vo = list.get(i);
Collection<?> subList = null; Collection<?> subList = null;
if (isSubList()) { if (isSubList()) {
if (isSubListValue(vo)) { if (isSubListValue(vo)) {
@ -702,7 +688,7 @@ public class ExcelUtil<T> {
Cell cell = row.createCell(column); Cell cell = row.createCell(column);
// 写入列信息 // 写入列信息
cell.setCellValue(attr.name()); cell.setCellValue(attr.name());
setDataValidation(attr, row, column); setDataValidation(attr, column);
cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
if (isSubList()) { if (isSubList()) {
// 填充默认样式,防止合并单元格样式失效 // 填充默认样式,防止合并单元格样式失效
@ -762,7 +748,7 @@ public class ExcelUtil<T> {
/** /**
* *
*/ */
public void setDataValidation (Excel attr, Row row, int column) { public void setDataValidation (Excel attr, int column) {
if (attr.name().contains("注:")) { if (attr.name().contains("注:")) {
sheet.setColumnWidth(column, 6000); sheet.setColumnWidth(column, 6000);
} else { } else {
@ -772,7 +758,7 @@ public class ExcelUtil<T> {
if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0) { if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0) {
if (attr.combo().length > 15 || StringUtils.join(attr.combo()).length() > 255) { if (attr.combo().length > 15 || StringUtils.join(attr.combo()).length() > 255) {
// 如果下拉数大于15或字符串长度大于255则使用一个新sheet存储避免生成的模板下拉值获取不到 // 如果下拉数大于15或字符串长度大于255则使用一个新sheet存储避免生成的模板下拉值获取不到
setXSSFValidationWithHidden(sheet, attr.combo(), attr.prompt(), 1, 100, column, column); setXssfValidationWithHidden(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
} else { } else {
// 提示信息或只能选择不能输入的列内容. // 提示信息或只能选择不能输入的列内容.
setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column); setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
@ -784,7 +770,7 @@ public class ExcelUtil<T> {
* *
*/ */
public void addCell (Excel attr, Row row, T vo, Field field, int column) { public void addCell (Excel attr, Row row, T vo, Field field, int column) {
Cell cell = null; Cell cell;
try { try {
// 设置行高 // 设置行高
row.setHeight(maxHeight); row.setHeight(maxHeight);
@ -858,24 +844,24 @@ public class ExcelUtil<T> {
* ,. * ,.
* *
* @param sheet sheet. * @param sheet sheet.
* @param textlist * @param textList
* @param promptContent * @param promptContent
* @param firstRow * @param firstRow
* @param endRow * @param endRow
* @param firstCol * @param firstCol
* @param endCol * @param endCol
*/ */
public void setXSSFValidationWithHidden (Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow, int firstCol, int endCol) { public void setXssfValidationWithHidden(Sheet sheet, String[] textList, String promptContent, int firstRow, int endRow, int firstCol, int endCol) {
String hideSheetName = "combo_" + firstCol + "_" + endCol; String hideSheetName = "combo_" + firstCol + "_" + endCol;
// 用于存储 下拉菜单数据 // 用于存储 下拉菜单数据
Sheet hideSheet = wb.createSheet(hideSheetName); Sheet hideSheet = wb.createSheet(hideSheetName);
for (int i = 0 ; i < textlist.length ; i++) { for (int i = 0 ; i < textList.length ; i++) {
hideSheet.createRow(i).createCell(0).setCellValue(textlist[i]); hideSheet.createRow(i).createCell(0).setCellValue(textList[i]);
} }
// 创建名称,可被其他单元格引用 // 创建名称,可被其他单元格引用
Name name = wb.createName(); Name name = wb.createName();
name.setNameName(hideSheetName + "_data"); name.setNameName(hideSheetName + "_data");
name.setRefersToFormula(hideSheetName + "!$A$1:$A$" + textlist.length); name.setRefersToFormula(hideSheetName + "!$A$1:$A$" + textList.length);
DataValidationHelper helper = sheet.getDataValidationHelper(); DataValidationHelper helper = sheet.getDataValidationHelper();
// 加载下拉列表内容 // 加载下拉列表内容
DataValidationConstraint constraint = helper.createFormulaListConstraint(hideSheetName + "_data"); DataValidationConstraint constraint = helper.createFormulaListConstraint(hideSheetName + "_data");
@ -906,13 +892,11 @@ public class ExcelUtil<T> {
* *
* @param value * @param value
* @param excel * @param excel
*
* @return
*/ */
public String dataFormatHandlerAdapter (Object value, Excel excel, Cell cell) { public String dataFormatHandlerAdapter (Object value, Excel excel, Cell cell) {
try { try {
Object instance = excel.handler().getDeclaredConstructor().newInstance(); Object instance = excel.handler().getDeclaredConstructor().newInstance();
Method formatMethod = excel.handler().getMethod("format", new Class[]{Object.class, String[].class, Cell.class, Workbook.class}); Method formatMethod = excel.handler().getMethod("format", Object.class, String[].class, Cell.class, Workbook.class);
value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb); value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb);
} catch (Exception e) { } catch (Exception e) {
log.error("不能格式化数据 「{}-{}」", excel.handler() , e.getMessage()); log.error("不能格式化数据 「{}-{}」", excel.handler() , e.getMessage());
@ -931,7 +915,7 @@ public class ExcelUtil<T> {
} }
try { try {
temp = Double.valueOf(text); temp = Double.valueOf(text);
} catch (NumberFormatException e) { } catch (NumberFormatException ignored) {
} }
statistics.put(index, statistics.get(index) + temp); statistics.put(index, statistics.get(index) + temp);
} }
@ -941,7 +925,7 @@ public class ExcelUtil<T> {
* *
*/ */
public void addStatisticsRow () { public void addStatisticsRow () {
if (statistics.size() > 0) { if (!statistics.isEmpty()) {
Row row = sheet.createRow(sheet.getLastRowNum() + 1); Row row = sheet.createRow(sheet.getLastRowNum() + 1);
Set<Integer> keys = statistics.keySet(); Set<Integer> keys = statistics.keySet();
Cell cell = row.createCell(0); Cell cell = row.createCell(0);
@ -965,8 +949,6 @@ public class ExcelUtil<T> {
* @param excel * @param excel
* *
* @return * @return
*
* @throws Exception
*/ */
private Object getTargetValue (T vo, Field field, Excel excel) throws Exception { private Object getTargetValue (T vo, Field field, Excel excel) throws Exception {
Object o = field.get(vo); Object o = field.get(vo);
@ -987,12 +969,10 @@ public class ExcelUtil<T> {
/** /**
* get * get
* *
* @param o * @param o
* @param name * @param name
* *
* @return value * @return value
*
* @throws Exception
*/ */
private Object getValue (Object o, String name) throws Exception { private Object getValue (Object o, String name) throws Exception {
if (StringUtils.isNotNull(o) && StringUtils.isNotEmpty(name)) { if (StringUtils.isNotNull(o) && StringUtils.isNotEmpty(name)) {
@ -1043,8 +1023,7 @@ public class ExcelUtil<T> {
Excels attrs = field.getAnnotation(Excels.class); Excels attrs = field.getAnnotation(Excels.class);
Excel[] excels = attrs.value(); Excel[] excels = attrs.value();
for (Excel attr : excels) { for (Excel attr : excels) {
if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr()) if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr()) && (attr.type() == Type.ALL || attr.type() == type)) {
&& (attr != null && (attr.type() == Type.ALL || attr.type() == type))) {
field.setAccessible(true); field.setAccessible(true);
fields.add(new Object[]{field, attr}); fields.add(new Object[]{field, attr});
} }
@ -1102,7 +1081,7 @@ public class ExcelUtil<T> {
*/ */
public Object getCellValue (Row row, int column) { public Object getCellValue (Row row, int column) {
if (row == null) { if (row == null) {
return row; return null;
} }
Object val = ""; Object val = "";
try { try {
@ -1139,8 +1118,6 @@ public class ExcelUtil<T> {
* *
* *
* @param row * @param row
*
* @return
*/ */
private boolean isRowEmpty (Row row) { private boolean isRowEmpty (Row row) {
if (row == null) { if (row == null) {
@ -1184,14 +1161,14 @@ public class ExcelUtil<T> {
* *
*/ */
public boolean isSubList () { public boolean isSubList () {
return StringUtils.isNotNull(subFields) && subFields.size() > 0; return StringUtils.isNotNull(subFields) && !subFields.isEmpty();
} }
/** /**
* *
*/ */
public boolean isSubListValue (T vo) { public boolean isSubListValue (T vo) {
return StringUtils.isNotNull(subFields) && subFields.size() > 0 && StringUtils.isNotNull(getListCellValue(vo)) && getListCellValue(vo).size() > 0; return StringUtils.isNotNull(subFields) && !subFields.isEmpty() && StringUtils.isNotNull(getListCellValue(vo)) && !getListCellValue(vo).isEmpty();
} }
/** /**
@ -1200,7 +1177,7 @@ public class ExcelUtil<T> {
public Collection<?> getListCellValue (Object obj) { public Collection<?> getListCellValue (Object obj) {
Object value; Object value;
try { try {
value = subMethod.invoke(obj, new Object[]{}); value = subMethod.invoke(obj);
} catch (Exception e) { } catch (Exception e) {
return new ArrayList<>(); return new ArrayList<>();
} }
@ -1221,7 +1198,7 @@ public class ExcelUtil<T> {
getMethodName.append(name.substring(1)); getMethodName.append(name.substring(1));
Method method = null; Method method = null;
try { try {
method = pojoClass.getMethod(getMethodName.toString(), new Class[]{}); method = pojoClass.getMethod(getMethodName.toString());
} catch (Exception e) { } catch (Exception e) {
log.error("获取对象异常{}", e.getMessage()); log.error("获取对象异常{}", e.getMessage());
} }