master
wangbing 5 years ago
parent a394852b42
commit a4b8fc39a9

@ -556,12 +556,18 @@ public class SpringBootCallable implements Callable {
ctx.put("moduleName", project.getProjectName()); ctx.put("moduleName", project.getProjectName());
ctx.put("timestamp", new Date().getTime()); ctx.put("timestamp", new Date().getTime());
File base = new File(root.getAbsolutePath(), "base");
base.mkdirs();
File auth = new File(root.getAbsolutePath(), "auth"); File auth = new File(root.getAbsolutePath(), "auth");
auth.mkdirs(); auth.mkdirs();
// File freemarker = new File(root.getAbsolutePath(), "freemarker"); File base = new File(root.getAbsolutePath(), "base");
// freemarker.mkdirs(); base.mkdirs();
File excel = new File(root.getAbsolutePath(), "excel");
excel.mkdirs();
File excelannotation = new File(excel.getAbsolutePath(), "annotation");
excelannotation.mkdirs();
File excelconverter = new File(excel.getAbsolutePath(), "converter");
excelconverter.mkdirs();
File excelstyle = new File(excel.getAbsolutePath(), "style");
excelstyle.mkdirs();
File utils = new File(root.getAbsolutePath(), "utils"); File utils = new File(root.getAbsolutePath(), "utils");
utils.mkdirs(); utils.mkdirs();
File validation = new File(root.getAbsolutePath(), "validation"); File validation = new File(root.getAbsolutePath(), "validation");
@ -588,14 +594,43 @@ public class SpringBootCallable implements Callable {
freeMarkerManager.outputTemp(new File(base.getAbsolutePath(), "SortType.java"), option + "/java/frame/base/SortType.java", ctx); freeMarkerManager.outputTemp(new File(base.getAbsolutePath(), "SortType.java"), option + "/java/frame/base/SortType.java", ctx);
freeMarkerManager.outputTemp(new File(base.getAbsolutePath(), "Token.java"), option + "/java/frame/base/Token.java", ctx); freeMarkerManager.outputTemp(new File(base.getAbsolutePath(), "Token.java"), option + "/java/frame/base/Token.java", ctx);
//freemarker //excel
// freeMarkerManager.outputTemp(new File(freemarker.getAbsolutePath(), "ViewNameTranslator.java"), option + "/java/frame/freemarker/ViewNameTranslator.java", ctx); freeMarkerManager.outputTemp(new File(excel.getAbsolutePath(), "WCell.java"), option + "/java/frame/excel/WCell.java", ctx);
// freeMarkerManager.outputTemp(new File(freemarker.getAbsolutePath(), "Layout.java"), option + "/java/frame/freemarker/Layout.java", ctx); freeMarkerManager.outputTemp(new File(excel.getAbsolutePath(), "WColumn.java"), option + "/java/frame/excel/WColumn.java", ctx);
// freeMarkerManager.outputTemp(new File(freemarker.getAbsolutePath(), "Uri.java"), option + "/java/frame/freemarker/Uri.java", ctx); freeMarkerManager.outputTemp(new File(excel.getAbsolutePath(), "WRow.java"), option + "/java/frame/excel/WRow.java", ctx);
freeMarkerManager.outputTemp(new File(excel.getAbsolutePath(), "WSheet.java"), option + "/java/frame/excel/WSheet.java", ctx);
freeMarkerManager.outputTemp(new File(excelannotation.getAbsolutePath(), "ColumnDescription.java"), option + "/java/frame/excel/annotation/ColumnDescription.java", ctx);
freeMarkerManager.outputTemp(new File(excelannotation.getAbsolutePath(), "ColumnName.java"), option + "/java/frame/excel/annotation/ColumnName.java", ctx);
freeMarkerManager.outputTemp(new File(excelannotation.getAbsolutePath(), "Converter.java"), option + "/java/frame/excel/annotation/Converter.java", ctx);
freeMarkerManager.outputTemp(new File(excelannotation.getAbsolutePath(), "Ignore.java"), option + "/java/frame/excel/annotation/Ignore.java", ctx);
freeMarkerManager.outputTemp(new File(excelannotation.getAbsolutePath(), "ParentFirst.java"), option + "/java/frame/excel/annotation/ParentFirst.java", ctx);
freeMarkerManager.outputTemp(new File(excelannotation.getAbsolutePath(), "SheetName.java"), option + "/java/frame/excel/annotation/SheetName.java", ctx);
freeMarkerManager.outputTemp(new File(excelconverter.getAbsolutePath(), "BooleanConverter.java"), option + "/java/frame/excel/converter/BooleanConverter.java", ctx);
freeMarkerManager.outputTemp(new File(excelconverter.getAbsolutePath(), "ByteConverter.java"), option + "/java/frame/excel/converter/ByteConverter.java", ctx);
freeMarkerManager.outputTemp(new File(excelconverter.getAbsolutePath(), "CharacterConverter.java"), option + "/java/frame/excel/converter/CharacterConverter.java", ctx);
freeMarkerManager.outputTemp(new File(excelconverter.getAbsolutePath(), "Converter.java"), option + "/java/frame/excel/converter/Converter.java", ctx);
freeMarkerManager.outputTemp(new File(excelconverter.getAbsolutePath(), "DateConverter.java"), option + "/java/frame/excel/converter/DateConverter.java", ctx);
freeMarkerManager.outputTemp(new File(excelconverter.getAbsolutePath(), "DoubleConverter.java"), option + "/java/frame/excel/converter/DoubleConverter.java", ctx);
freeMarkerManager.outputTemp(new File(excelconverter.getAbsolutePath(), "FloatConverter.java"), option + "/java/frame/excel/converter/FloatConverter.java", ctx);
freeMarkerManager.outputTemp(new File(excelconverter.getAbsolutePath(), "IntegerConverter.java"), option + "/java/frame/excel/converter/IntegerConverter.java", ctx);
freeMarkerManager.outputTemp(new File(excelconverter.getAbsolutePath(), "LongConverter.java"), option + "/java/frame/excel/converter/LongConverter.java", ctx);
freeMarkerManager.outputTemp(new File(excelconverter.getAbsolutePath(), "ShortConverter.java"), option + "/java/frame/excel/converter/ShortConverter.java", ctx);
freeMarkerManager.outputTemp(new File(excelconverter.getAbsolutePath(), "StringConverter.java"), option + "/java/frame/excel/converter/StringConverter.java", ctx);
freeMarkerManager.outputTemp(new File(excelstyle.getAbsolutePath(), "BaseCellStyle.java"), option + "/java/frame/excel/style/BaseCellStyle.java", ctx);
freeMarkerManager.outputTemp(new File(excelstyle.getAbsolutePath(), "BaseFont.java"), option + "/java/frame/excel/style/BaseFont.java", ctx);
freeMarkerManager.outputTemp(new File(excelstyle.getAbsolutePath(), "DataCellStyle.java"), option + "/java/frame/excel/style/DataCellStyle.java", ctx);
freeMarkerManager.outputTemp(new File(excelstyle.getAbsolutePath(), "ErrorCellStyle.java"), option + "/java/frame/excel/style/ErrorCellStyle.java", ctx);
freeMarkerManager.outputTemp(new File(excelstyle.getAbsolutePath(), "HeadCellStyle.java"), option + "/java/frame/excel/style/HeadCellStyle.java", ctx);
freeMarkerManager.outputTemp(new File(excelstyle.getAbsolutePath(), "NormalFont.java"), option + "/java/frame/excel/style/NormalFont.java", ctx);
freeMarkerManager.outputTemp(new File(excelstyle.getAbsolutePath(), "RedFont.java"), option + "/java/frame/excel/style/RedFont.java", ctx);
//uitls //uitls
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "Base64Util.java"), option + "/java/frame/utils/Base64Util.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "Base64Util.java"), option + "/java/frame/utils/Base64Util.java", ctx);
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "AESUtil.java"), option + "/java/frame/utils/AESUtil.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "AESUtil.java"), option + "/java/frame/utils/AESUtil.java", ctx);
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "ClassUtil.java"), option + "/java/frame/utils/ClassUtil.java", ctx);
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "CookieUtil.java"), option + "/java/frame/utils/CookieUtil.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "CookieUtil.java"), option + "/java/frame/utils/CookieUtil.java", ctx);
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "FileUtil.java"), option + "/java/frame/utils/FileUtil.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "FileUtil.java"), option + "/java/frame/utils/FileUtil.java", ctx);
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "LogUtil.java"), option + "/java/frame/utils/LogUtil.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "LogUtil.java"), option + "/java/frame/utils/LogUtil.java", ctx);
@ -603,6 +638,7 @@ public class SpringBootCallable implements Callable {
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "MD5Util.java"), option + "/java/frame/utils/MD5Util.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "MD5Util.java"), option + "/java/frame/utils/MD5Util.java", ctx);
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "ProcessUtil.java"), option + "/java/frame/utils/ProcessUtil.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "ProcessUtil.java"), option + "/java/frame/utils/ProcessUtil.java", ctx);
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "RSAUtil.java"), option + "/java/frame/utils/RSAUtil.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "RSAUtil.java"), option + "/java/frame/utils/RSAUtil.java", ctx);
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "StringUtil.java"), option + "/java/frame/utils/StringUtil.java", ctx);
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "ValidationUtil.java"), option + "/java/frame/utils/ValidationUtil.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "ValidationUtil.java"), option + "/java/frame/utils/ValidationUtil.java", ctx);
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "WebUtils.java"), option + "/java/frame/utils/WebUtils.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "WebUtils.java"), option + "/java/frame/utils/WebUtils.java", ctx);
freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "IDgenerator.java"), option + "/java/frame/utils/IDgenerator.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "IDgenerator.java"), option + "/java/frame/utils/IDgenerator.java", ctx);

@ -0,0 +1,33 @@
package ${basePackage}.frame.excel;
import java.io.Serializable;
public class WCell implements Serializable {
private String value;
/**
* ..
*/
public WCell() {
this.value = "";
}
/**
* .
*
* @param value .
*/
public WCell(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

@ -0,0 +1,128 @@
package ${basePackage}.frame.excel;
import xyz.wbsite.frame.excel.converter.Converter;
import xyz.wbsite.frame.utils.StringUtil;
import java.io.Serializable;
import java.lang.reflect.Field;
/**
* DataTable<br/>
*
* Created by edward on 9/19/14.
*/
public class WColumn implements Serializable {
/**
*
*/
private String name;
/**
*
*/
private int cellWidth;
/**
* Excel
*/
private boolean isHidden;
/**
*
*/
private boolean isRequired;
/**
*
*/
private String description;
/**
*
*/
private Converter converter;
private Field field;
private int cellType = 1;
public WColumn() {
this.name = "";
this.cellWidth = 1;
this.isHidden = false;
this.isRequired = false;
this.description = "";
}
public WColumn(String name) {
this.name = name;
this.cellWidth = 1;
this.isHidden = false;
this.isRequired = false;
this.description = "";
}
//----------- getter & setter --------------
public String getName() {
return name;
}
public void setName(String name) {
if (StringUtil.getByteLength(name) > cellWidth) {
cellWidth = StringUtil.getByteLength(name);
}
this.name = name;
}
public Field getField() {
return field;
}
public void setField(Field field) {
this.field = field;
}
public int getCellWidth() {
return cellWidth;
}
public void setCellWidth(int cellWidth) {
this.cellWidth = cellWidth;
}
public boolean isHidden() {
return isHidden;
}
public void setHidden(boolean isHidden) {
this.isHidden = isHidden;
}
public boolean isRequired() {
return isRequired;
}
public void setRequired(boolean isRequired) {
this.isRequired = isRequired;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Converter getConverter() {
return converter;
}
public void setConverter(Converter converter) {
this.converter = converter;
}
public int getCellType() {
return cellType;
}
public void setCellType(int cellType) {
this.cellType = cellType;
}
}

@ -0,0 +1,30 @@
package ${basePackage}.frame.excel;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* DataTable.
* Created on 2014/09/19.
*
* @author
* @since 0.1.0
*/
public class WRow extends HashMap<String, WCell> implements Serializable {
private List<String> errorList = new ArrayList<>();
public final boolean hasError() {
return errorList.size() > 0;
}
public final void addError(String errorMsg) {
errorList.add(errorMsg);
}
public List<String> getErrorList() {
return errorList;
}
}

@ -0,0 +1,598 @@
package ${basePackage}.frame.excel;
import xyz.wbsite.frame.excel.style.DataCellStyle;
import xyz.wbsite.frame.excel.style.ErrorCellStyle;
import xyz.wbsite.frame.excel.style.HeadCellStyle;
import xyz.wbsite.frame.excel.style.RedFont;
import xyz.wbsite.frame.utils.ClassUtil;
import xyz.wbsite.frame.utils.StringUtil;
import xyz.wbsite.frame.utils.ValidationUtil;
import xyz.wbsite.frame.excel.annotation.*;
import xyz.wbsite.frame.excel.converter.*;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import javax.validation.constraints.NotNull;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* <br/>
* Created on 2014/09/19.
*
* @author
* @since 0.1.0
*/
public class WSheet<T> implements Serializable, Cloneable {
/**
*
*/
private String name;
/**
*
*/
private boolean freezeFirst;
/**
*
*/
private List<WColumn> columnList = new ArrayList<>();
/**
*
*/
private List<WRow> rowList = new ArrayList<>();
/**
*
*/
public WSheet(Class<T> tClass) {
initColumns(tClass);
}
/**
* DataTable
*
* @return @NameSet
*/
private List<WColumn> initColumns(Class<?> clazz) {
//获取工作簿名称,没有则以类名为默认工作簿名称
if (clazz.isAnnotationPresent(SheetName.class)) {
SheetName sheetName = clazz.getAnnotation(SheetName.class);
this.setName(sheetName.value());
this.setFreezeFirst(sheetName.freezeFirst());
} else {
this.setName(clazz.getName());
}
//是否关注父类属性
boolean parentFirst = clazz.isAnnotationPresent(ParentFirst.class) && clazz.getAnnotation(ParentFirst.class).value();
Field[] fields = ClassUtil.getFields(clazz, parentFirst);
for (Field field : fields) {
WColumn WColumn = new WColumn();
WColumn.setField(field);
if (field.isAnnotationPresent(Ignore.class) && field.getAnnotation(Ignore.class).value()) {
WColumn.setHidden(true);
}
//获取列名称
if (!field.isAnnotationPresent(ColumnName.class)) {
WColumn.setName(field.getName());
} else {
ColumnName columnColumnName = field.getAnnotation(ColumnName.class);
WColumn.setName(columnColumnName.value());
WColumn.setCellWidth(columnColumnName.width());
}
//获取列填写说明或描述
if (field.isAnnotationPresent(ColumnDescription.class)) {
WColumn.setDescription(field.getAnnotation(ColumnDescription.class).value());
}
//列填写标志(是否必填)
if (field.isAnnotationPresent(NotNull.class)) {
WColumn.setRequired(true);
}
//获取列类型
if (field.isAnnotationPresent(xyz.wbsite.frame.excel.annotation.Converter.class)) {
xyz.wbsite.frame.excel.annotation.Converter converter = field.getAnnotation(xyz.wbsite.frame.excel.annotation.Converter.class);
Class target = converter.target();
try {
WColumn.setConverter((xyz.wbsite.frame.excel.converter.Converter) target.newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
if (field.getType() == boolean.class || field.getType() == Boolean.class) {
WColumn.setConverter(new BooleanConverter());
WColumn.setCellType(Cell.CELL_TYPE_BOOLEAN);
} else if (field.getType() == byte.class || field.getType() == Byte.class) {
WColumn.setConverter(new ByteConverter());
WColumn.setCellType(Cell.CELL_TYPE_NUMERIC);
} else if (field.getType() == char.class || field.getType() == Character.class) {
WColumn.setConverter(new CharacterConverter());
WColumn.setCellType(Cell.CELL_TYPE_STRING);
} else if (field.getType() == short.class || field.getType() == Short.class) {
WColumn.setConverter(new ShortConverter());
WColumn.setCellType(Cell.CELL_TYPE_NUMERIC);
} else if (field.getType() == int.class || field.getType() == Integer.class) {
WColumn.setConverter(new IntegerConverter());
WColumn.setCellType(Cell.CELL_TYPE_NUMERIC);
} else if (field.getType() == long.class || field.getType() == Long.class) {
WColumn.setConverter(new LongConverter());
WColumn.setCellType(Cell.CELL_TYPE_NUMERIC);
} else if (field.getType() == float.class || field.getType() == Float.class) {
WColumn.setConverter(new FloatConverter());
WColumn.setCellType(Cell.CELL_TYPE_NUMERIC);
} else if (field.getType() == double.class || field.getType() == Double.class) {
WColumn.setConverter(new DoubleConverter());
WColumn.setCellType(Cell.CELL_TYPE_NUMERIC);
} else if (field.getType() == Date.class) {
WColumn.setConverter(new DateConverter());
WColumn.setCellType(Cell.CELL_TYPE_NUMERIC);
} else if (field.getType() == String.class) {
WColumn.setConverter(new StringConverter());
WColumn.setCellType(Cell.CELL_TYPE_STRING);
} else {
throw new RuntimeException("Can not find Converter");
}
}
columnList.add(WColumn);
}
return columnList;
}
/**
* <br/>
*
*
* @param list
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public WSheet(List<T> list) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
if (list == null || list.size() == 0) {
throw new RuntimeException("不允许传入空的列表");
}
if (list.size() > 0) {
List<WColumn> wColumns = initColumns(list.get(0).getClass());
for (T t : list) {
WRow row = new WRow();
for (WColumn column : wColumns) {
if (column == null) {
continue;
}
Field field = column.getField();
String att = StringUtil.upperFirstWord(field.getName());
Method method = null;
try {
// 尝试获取get方法
method = t.getClass().getMethod("get" + att);
} catch (NoSuchMethodException e) {
// 尝试获取is方法工具生成布尔值可能是is而不是get
method = t.getClass().getMethod("is" + att);
}
Object value = method.invoke(t);
if (null == value) {
row.put(column.getName(), new WCell());
} else {
String string = column.getConverter().string(value);
row.put(column.getName(), new WCell(string));
}
}
this.rowList.add(row);
}
}
}
/**
* Excel<br/>
* <br/>
*
* @param bytes Excel
* @param clazz
* @throws IOException <br/>1:Excel - bytes <br/>
* 2:<br/>
*/
public WSheet(byte[] bytes, Class<T> clazz) throws IOException {
Workbook workbook = null;
InputStream is = null;
boolean flag;
try {
flag = true;
is = new ByteArrayInputStream(bytes); //读取文件流
workbook = new HSSFWorkbook(is);
} catch (Exception e) {
flag = false;
}
if (!flag) {
try {
flag = true;
is = new ByteArrayInputStream(bytes); //读取文件流
workbook = new XSSFWorkbook(is);
} catch (Exception e) {
flag = false;
}
}
if (is != null) {
is.close();
}
if (!flag) {
throw new RuntimeException("不支持的文件类型");
}
//第一张Sheet表
Sheet sheet = workbook.getSheetAt(0);
//获取Sheet名称
if (clazz.isAnnotationPresent(SheetName.class)) {//如果模板存在注解则使用注解名称
SheetName sheetName = clazz.getAnnotation(SheetName.class);
this.setName(sheetName.value());
} else {//将类名设为表名如果类名不存在将Excel表名设为表名
this.setName(sheet.getSheetName());
}
//读取表头
Row headRow = sheet.getRow(0);
//获取Excel列的总数
int columnSum = headRow.getPhysicalNumberOfCells();
//检查列数量
List<WColumn> list = initColumns(clazz);
if (list.size() != columnSum) {
throw new RuntimeException("与模板列数量不同。");
} else {
for (int i = 0; i < list.size(); i++) {
WColumn wColumn = list.get(i);
Cell cell = headRow.getCell(i);
String headValue = getValue(cell);
headValue = headValue.replace("*", "");
headValue = headValue.replace(" ", "");
if (!wColumn.getName().equals(headValue)) {
throw new RuntimeException("第" + (i + 1) + "项,不匹配的列名," + wColumn.getName() + "和" + headValue);
}
}
}
int maxRowNumber = sheet.getLastRowNum(); //Excel文件的总行数
// 逐行读取导入文件的数据
for (int i = 0; i < maxRowNumber; i++) {
Row inputRow = sheet.getRow(i + 1); //Excel中的一行数据第0行为表头所以要加1
WRow row = new WRow();
rowList.add(row);
if (null != inputRow) {
for (int j = 0; j < columnList.size(); j++) {
WColumn wcolumn = columnList.get(j);
/* 取得当前格子的值 */
Cell excelCell = inputRow.getCell(j);
WCell WCell = new WCell();
row.put(wcolumn.getName(), WCell);
String value = "";
if (null != excelCell) {
value = getValue(excelCell);
}
value = value.trim();
WCell.setValue(value);
}
}
try {
T t = transferOneObject(clazz, i);
List<String> validate = ValidationUtil.validate(t);
if (validate.size() > 0) {
for (String s : validate) {
row.addError(s);
}
}
} catch (Exception e) {
e.printStackTrace();
row.addError("数据检查错误");
}
}
}
/**
*
*
* @param clazz
* @param rowIndex
* @return
* @throws IllegalAccessException
* @throws InstantiationException
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
public T transferOneObject(Class<T> clazz, int rowIndex) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
T object = clazz.newInstance();
ParentFirst parentFirstAnnotation = clazz.getAnnotation(ParentFirst.class);
boolean parentFirst = parentFirstAnnotation != null && parentFirstAnnotation.value();
Field[] fields = ClassUtil.getFields(clazz, parentFirst);
Set<Field> set = new HashSet<>();
for (Field field : fields) {
set.add(field);
}
for (int j = 0; j < this.columnList.size(); j++) {
WColumn wColumn = this.columnList.get(0);
if (wColumn.isHidden()) {
continue;
}
String key = this.columnList.get(j).getName();
for (WColumn column : columnList) {
Field field = column.getField();
ColumnName fieldColumnName = field.getAnnotation(ColumnName.class);
if (key.equals(fieldColumnName.value())) {
String att = StringUtil.upperFirstWord(field.getName());
WCell WCell = this.rowList.get(rowIndex).get(column.getName());
if (null != WCell) {
String value = WCell.getValue();
Method method = clazz.getMethod("set" + att, field.getType());
//获取转换器
xyz.wbsite.frame.excel.converter.Converter converter = column.getConverter();
method.invoke(object, converter.convert(value));
}
break;
}
}
}
return object;
}
/**
* DataTableTList<br/><br/>
* ,@Name<br/>
* DataTable<br/>
*
*
* @return T
* @see WColumn
*/
public List<T> transferList(Class<T> clazz) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
List<T> list = new ArrayList<>();
for (int i = 0; i < this.rowList.size(); i++) {
T object = transferOneObject(clazz, i);
list.add(object);
}
return list;
}
/**
*
*
* @return
*/
public String getName() {
return name;
}
/**
*
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
public boolean getFreezeFirst() {
return freezeFirst;
}
public void setFreezeFirst(boolean freezeFirst) {
this.freezeFirst = freezeFirst;
}
//
// public final String toCSV() {
// StringBuilder sb = new StringBuilder();
//
// for (WColumn WColumn : this.getWColumns()) {
// if (WColumn != null) {
// sb.append(WColumn.getName()).append(",");
// }
// }
// sb.append("\n");
//
// for (int i = 0; i < this.getRowIndex(); i++) {
// for (int j = 0; j < this.getColumnIndex(); j++) {
// WCell WCell = this.getCell(i, j);
// if (this.columnList.get(j).getExcelType().equals(ExcelType.STRING)
// || this.columnList.get(j).getExcelType().equals(ExcelType.DATE)
// || this.columnList.get(j).getExcelType().equals(ExcelType.DATETIME)) {
// sb.append("\"\t").append(WCell.getValue()).append("\",");
// } else {
// sb.append(WCell.getValue()).append(",");
// }
// }
// sb.append("\n");
// }
//
// return sb.toString();
// }
/**
* DataTable
*
* @return .
*/
public final boolean hasError() {
for (WRow wRow : rowList) {
if (wRow.hasError()) {
return true;
}
}
return false;
}
/**
* Excel
*
* @return
* @throws IOException
*/
public byte[] getBytes() throws IOException {
XSSFWorkbook workbook = getExcel(false);
if (workbook != null) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
return outputStream.toByteArray();
}
return null;
}
public byte[] getBytes(boolean checkResult) throws IOException {
XSSFWorkbook workbook = getExcel(checkResult);
if (workbook != null) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
return outputStream.toByteArray();
}
return null;
}
public XSSFWorkbook getExcel(boolean checkResult) {
XSSFWorkbook workbook = new XSSFWorkbook();
//创建一个Sheet表
XSSFSheet sheet = workbook.createSheet(name);
sheet.setDefaultRowHeightInPoints(18);
Row firstRow = sheet.createRow(0); // 下标为0的行开始
XSSFDrawing drawing = sheet.createDrawingPatriarch();
int offset = 0;
// 添加错误说明列
if (checkResult && hasError()) {
offset++;
Cell firstCell = firstRow.createCell(0);
firstCell.setCellStyle(new ErrorCellStyle(workbook).getStyle());
firstCell.setCellValue(new XSSFRichTextString("检查结果说明"));
sheet.setColumnWidth(0, 18 * 256);
}
for (int j = 0; j < this.columnList.size(); j++) {
WColumn column = this.columnList.get(j);
Field field = column.getField();
Cell firstCell = firstRow.createCell(j + offset);
String columnName = column.getName();
XSSFRichTextString textString;
if (column.isRequired()) {
textString = new XSSFRichTextString("*" + columnName);
textString.applyFont(0, 1, new RedFont(workbook).getFont());
textString.applyFont(1, textString.length(), workbook.createFont());
} else {
textString = new XSSFRichTextString(columnName);
textString.applyFont(workbook.createFont());
}
StringBuilder sb = new StringBuilder();
sb.append(column.getDescription()).append("\n");
// 添加数据说明信息。
if (field.isAnnotationPresent(ColumnDescription.class)) {
// 获取声明字段上的Description信息。
ColumnDescription columnDescription = field.getAnnotation(ColumnDescription.class);
sb.append(columnDescription.value());
sb.append("\n");
}
// 如果填写了注释信息
if (sb.length() > 1) {
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6);
XSSFComment comment = drawing.createCellComment(anchor);
comment.setString(sb.toString());
firstCell.setCellComment(comment);
}
firstCell.setCellValue(textString);
firstCell.setCellStyle(new HeadCellStyle(workbook).getStyle());
sheet.setColumnWidth(j + offset, (4 + column.getCellWidth()) * 256);
}
// 冻结第一行
if (freezeFirst) {
sheet.createFreezePane(255, 1);
}
// 填充数据
for (int i = 0; i < this.rowList.size(); i++) {
boolean flag = this.rowList.get(i).hasError();
WRow wRow = this.rowList.get(i);
Row row = sheet.createRow(i + 1);
if (flag) {
List<String> errorList = wRow.getErrorList();
Cell xssfCell = row.createCell(0);
xssfCell.setCellStyle(new ErrorCellStyle(workbook).getStyle());
String join = String.join(";", errorList);
xssfCell.setCellValue(new XSSFRichTextString(join));
}
for (int j = 0; j < this.columnList.size(); j++) {
WColumn column = this.columnList.get(j);
Cell xssfCell = row.createCell(j + offset);
WCell WCell = this.rowList.get(i).get(column.getName());
if (null == WCell) {
continue;
}
String value = WCell.getValue();
xssfCell.setCellType(column.getCellType());
if (column.getCellType() == Cell.CELL_TYPE_NUMERIC || column.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
xssfCell.setCellStyle(new DataCellStyle(workbook, CellStyle.ALIGN_RIGHT, wRow.hasError()).getStyle());
} else {
xssfCell.setCellStyle(new DataCellStyle(workbook, CellStyle.ALIGN_LEFT, wRow.hasError()).getStyle());
}
xssfCell.setCellValue(value);
}
}
return workbook;
}
/**
*
*
* @param cell
* @return
*/
public static String getValue(Cell cell) {
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
case Cell.CELL_TYPE_BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case Cell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date value = cell.getDateCellValue();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);
} else {
double numericCellValue = cell.getNumericCellValue();
String s = String.valueOf(numericCellValue);
return s.replaceAll("\\.0$", "");
}
default:
return cell.getStringCellValue();
}
}
}

@ -0,0 +1,17 @@
package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*;
/**
* Created on 2014/9/24.
*
* @author
* @version v1.0.0.0
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ColumnDescription {
String value() default "";
}

@ -0,0 +1,31 @@
package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*;
/**
* <br/>
* Created on 2014/9/24.
*
* @author
* @version v1.0.0.0
*/
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ColumnName {
String value();
/**
*
*
* @return
*/
int width() default 8;
/**
*
*
* @return
*/
String dateFormat() default "yyyy-MM-dd";
}

@ -0,0 +1,16 @@
package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*;
/**
* target
*
* @author
* @version v0.0.1
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Converter {
Class target();
}

@ -0,0 +1,16 @@
package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*;
/**
*
*
* @author
* @since 2.1.0
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Ignore {
boolean value() default true;
}

@ -0,0 +1,16 @@
package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*;
/**
*
*
* @author
* @since 2.1.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ParentFirst {
boolean value() default true;
}

@ -0,0 +1,19 @@
package ${basePackage}.frame.excel.annotation;
import java.lang.annotation.*;
/**
* <br/>
* Created on 2014/9/24.
*
* @author
* @version v1.0.0.0
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SheetName {
String value();
boolean freezeFirst() default false;
}

@ -0,0 +1,21 @@
package ${basePackage}.frame.excel.converter;
public class BooleanConverter implements Converter<Boolean> {
@Override
public Boolean convert(String var) {
try {
return Boolean.parseBoolean(var);
} catch (Exception e) {
return false;
}
}
@Override
public String string(Boolean var) {
if (var == null) {
return "";
}
return String.valueOf(var);
}
}

@ -0,0 +1,21 @@
package ${basePackage}.frame.excel.converter;
public class ByteConverter implements Converter<Byte> {
@Override
public Byte convert(String var) {
try {
return Byte.parseByte(var);
} catch (Exception e) {
return 0;
}
}
@Override
public String string(Byte var) {
if (var == null) {
return "";
}
return String.valueOf(var);
}
}

@ -0,0 +1,21 @@
package ${basePackage}.frame.excel.converter;
public class CharacterConverter implements Converter<Character> {
@Override
public Character convert(String var) {
if (var == null || var.equals("")) {
return '0';
} else {
return var.charAt(0);
}
}
@Override
public String string(Character var) {
if (var == null) {
return "";
}
return String.valueOf(var).trim();
}
}

@ -0,0 +1,8 @@
package ${basePackage}.frame.excel.converter;
public interface Converter<T> {
T convert(String var);
String string(T var);
}

@ -0,0 +1,44 @@
package ${basePackage}.frame.excel.converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConverter implements Converter<Date> {
@Override
public Date convert(String var) {
if (var == null) {
return null;
}
Date date = null;
try {
var = var.trim();
if (var.matches("[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(var);
} else if (var.matches("[0-9]{4}/[0-9]{1,2}/[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}")) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(var);
} else if (var.matches("[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}")) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = sdf.parse(var);
} else if (var.matches("[0-9]{4}年[0-9]{1,2}月[0-9]{1,2}日 [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}")) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
date = sdf.parse(var);
}
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
@Override
public String string(Date var) {
if (var == null) {
return "";
}
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(var);
}
}

@ -0,0 +1,21 @@
package ${basePackage}.frame.excel.converter;
public class DoubleConverter implements Converter<Double> {
@Override
public Double convert(String var) {
try {
return Double.parseDouble(var);
} catch (Exception e) {
return 0d;
}
}
@Override
public String string(Double var) {
if (var == null) {
return "";
}
return String.valueOf(var);
}
}

@ -0,0 +1,26 @@
package ${basePackage}.frame.excel.converter;
public class FloatConverter implements Converter<Float> {
@Override
public Float convert(String var) {
try {
//增加对1.0一类的的优化显示
if (var.matches("(\\d+)\\.0")) {
var = var.replaceAll("\\.0$", "");
}
return Float.parseFloat(var);
} catch (Exception e) {
return 0f;
}
}
@Override
public String string(Float var) {
if (var == null) {
return "";
}
return String.valueOf(var);
}
}

@ -0,0 +1,21 @@
package ${basePackage}.frame.excel.converter;
public class IntegerConverter implements Converter<Integer> {
@Override
public Integer convert(String var) {
try {
return Integer.parseInt(var);
} catch (Exception e) {
return 0;
}
}
@Override
public String string(Integer var) {
if (var == null) {
return "";
}
return String.valueOf(var);
}
}

@ -0,0 +1,34 @@
package ${basePackage}.frame.excel.converter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LongConverter implements Converter<Long> {
@Override
public Long convert(String var) {
try {
//增加对1.0一类的的兼容性
if (var.matches("(\\d+)\\.(\\d*)")) {
Pattern compile = Pattern.compile("(\\d+)\\.(\\d*)");
Matcher matcher = compile.matcher(var);
if (matcher.find()) {
var = matcher.group(1);
}
}
return Long.parseLong(var);
} catch (Exception e) {
return 0L;
}
}
@Override
public String string(Long var) {
if (var == null) {
return "";
}
return String.valueOf(var);
}
}

@ -0,0 +1,21 @@
package ${basePackage}.frame.excel.converter;
public class ShortConverter implements Converter<Short> {
@Override
public Short convert(String var) {
try {
return Short.parseShort(var);
} catch (Exception e) {
return 0;
}
}
@Override
public String string(Short var) {
if (var == null) {
return "";
}
return String.valueOf(var);
}
}

@ -0,0 +1,17 @@
package ${basePackage}.frame.excel.converter;
public class StringConverter implements Converter<String> {
@Override
public String convert(String var) {
return var;
}
@Override
public String string(String var) {
if (var == null) {
return "";
}
return String.valueOf(var);
}
}

@ -0,0 +1,57 @@
package ${basePackage}.frame.excel.style;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
/**
* Created on 2015/1/29.
*
* @author
* @since 2.0.0
*/
public class BaseCellStyle {
/**
*
*/
protected CellStyle style;
public BaseCellStyle(Workbook workbook) {
style = workbook.createCellStyle();
style.setFillPattern(CellStyle.NO_FILL);
//下边框
style.setBorderBottom(CellStyle.SOLID_FOREGROUND);
//下边框颜色
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
//左边框
style.setBorderLeft(CellStyle.SOLID_FOREGROUND);
//左边框颜色
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
//右边框
style.setBorderRight(CellStyle.SOLID_FOREGROUND);
//右边框颜色
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
//上边框
style.setBorderTop(CellStyle.SOLID_FOREGROUND);
//上边框颜色
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //上下居中
style.setBorderBottom(CellStyle.SOLID_FOREGROUND); //下边框
style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色
style.setBorderLeft(CellStyle.SOLID_FOREGROUND); //左边框
style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); //左边框颜色
style.setBorderRight(CellStyle.SOLID_FOREGROUND); //右边框
style.setRightBorderColor(IndexedColors.BLACK.getIndex()); //右边框颜色
style.setBorderTop(CellStyle.SOLID_FOREGROUND); //上边框
style.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色
}
public CellStyle getStyle() {
return style;
}
public void setStyle(CellStyle style) {
this.style = style;
}
}

@ -0,0 +1,29 @@
package ${basePackage}.frame.excel.style;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
/**
* Created on 2015/1/29.
*
* @author
* @since 2.0.0
*/
public class BaseFont {
/**
*
*/
protected Font font;
public BaseFont(Workbook workbook) {
font = workbook.createFont();
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
}

@ -0,0 +1,29 @@
package ${basePackage}.frame.excel.style;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
public class DataCellStyle extends BaseCellStyle {
public DataCellStyle(Workbook workbook, short align, boolean error) {
super(workbook);
style.setAlignment(align);
if (error) {
style.setFillForegroundColor(HSSFColor.RED.index); //背景颜色红色
style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式
}
}
public DataCellStyle(Workbook workbook, short align) {
this(workbook, align, false);
}
public DataCellStyle(Workbook workbook, boolean error) {
this(workbook, CellStyle.ALIGN_LEFT, error);
}
public DataCellStyle(Workbook workbook) {
this(workbook, CellStyle.ALIGN_LEFT, false);
}
}

@ -0,0 +1,22 @@
package ${basePackage}.frame.excel.style;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
public class ErrorCellStyle extends BaseCellStyle {
public ErrorCellStyle(Workbook workbook) {
super(workbook);
style.setFillForegroundColor(HSSFColor.RED.index); //背景颜色红色
style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式
}
public CellStyle getStyle() {
return style;
}
public void setStyle(CellStyle style) {
this.style = style;
}
}

@ -0,0 +1,15 @@
package ${basePackage}.frame.excel.style;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
public class HeadCellStyle extends BaseCellStyle {
public HeadCellStyle(Workbook workbook) {
super(workbook);
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); //背景颜色-灰色
style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格填充样式
style.setAlignment(CellStyle.ALIGN_CENTER); // 居中
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//上下居中
}
}

@ -0,0 +1,18 @@
package ${basePackage}.frame.excel.style;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Workbook;
/**
* .
* Created on 2015/1/29.
*
* @author
* @since 2.0.0
*/
public class NormalFont extends BaseFont {
public NormalFont(Workbook workbook) {
super(workbook);
font.setColor(HSSFColor.BLACK.index); //字体颜色-黑色
}
}

@ -0,0 +1,18 @@
package ${basePackage}.frame.excel.style;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Workbook;
/**
* .
* Created on 2015/1/29.
*
* @author
* @since 2.0.0
*/
public class RedFont extends BaseFont {
public RedFont(Workbook workbook) {
super(workbook);
font.setColor(HSSFColor.RED.index); //字体颜色-黑色
}
}

@ -0,0 +1,173 @@
package xyz.wbsite.frame.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
/**
* Created on 2015/5/28.
*
* @author
* @version 2.1.0
*/
public class ClassUtil {
/**
*
*
* @param clazz
* @param parentFirst
* @return
*/
public static Field[] getFields(Class clazz, boolean parentFirst) {
List<Field> returnList = new ArrayList<>();
Set<String> nameSet = new HashSet<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
returnList.add(field);
nameSet.add(field.getName());
}
List<Field> parentList = getParentFields(clazz.getSuperclass(), nameSet, parentFirst);
if (parentFirst) {
parentList.addAll(returnList);
returnList = parentList;
} else {
returnList.addAll(parentList);
}
fields = new Field[returnList.size()];
int index = 0;
for (Field field : returnList) {
fields[index++] = field;
}
return fields;
}
/**
* class
*
* @param clazz
* @return
*/
public static Field[] getClassFields(Class<?> clazz) {
List<Field> list = new ArrayList<Field>();
Field[] fields;
do {
fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
list.add(fields[i]);
}
clazz = clazz.getSuperclass();
} while (clazz != Object.class && clazz != null);
return list.toArray(fields);
}
/**
*
*
* @param clazz
* @return
*/
public static boolean isCollection(Class<?> clazz) {
return Collection.class.isAssignableFrom(clazz);
}
/**
* GET
*
* @param name
* @param pojoClass
* @return
* @throws Exception
*/
public static Method getMethod(String name, Class<?> pojoClass) throws Exception {
StringBuffer getMethodName = new StringBuffer("get");
getMethodName.append(name.substring(0, 1).toUpperCase());
getMethodName.append(name.substring(1));
Method method = null;
try {
method = pojoClass.getMethod(getMethodName.toString());
} catch (Exception e) {
method = pojoClass.getMethod(
getMethodName.toString().replace("get", "is")
);
}
return method;
}
/**
* SET
*
* @param name
* @param pojoClass
* @param type
* @return
* @throws Exception
*/
public static Method getMethod(String name, Class<?> pojoClass, Class<?> type) throws Exception {
StringBuffer getMethodName = new StringBuffer("set");
getMethodName.append(name.substring(0, 1).toUpperCase());
getMethodName.append(name.substring(1));
return pojoClass.getMethod(getMethodName.toString(), type);
}
/**
* java
*
* @param field
* @return
*/
public static boolean isJavaClass(Field field) {
Class<?> fieldType = field.getType();
boolean isBaseClass = false;
if (fieldType.isArray()) {
isBaseClass = false;
} else if (fieldType.isPrimitive() || fieldType.getPackage() == null
|| fieldType.getPackage().getName().equals("java.lang")
|| fieldType.getPackage().getName().equals("java.math")
|| fieldType.getPackage().getName().equals("java.sql")
|| fieldType.getPackage().getName().equals("java.util")) {
isBaseClass = true;
}
return isBaseClass;
}
/**
*
*
* @param clazz
* @return
*/
public static Field[] getFields(Class clazz) {
return getFields(clazz, false);
}
private static List<Field> getParentFields(Class parentClazz, Set<String> nameSet, boolean parentFirst) {
List<Field> fieldList = new ArrayList<>();
if (parentClazz != null) {
Field[] parentList = parentClazz.getDeclaredFields();
int index = 0;
for (Field field : parentList) {
int beginSize = nameSet.size();
nameSet.add(field.getName());
int endSize = nameSet.size();
if (endSize > beginSize) {
if (parentFirst) {
fieldList.add(index++, field);
} else {
fieldList.add(field);
}
}
}
fieldList.addAll(getParentFields(parentClazz.getSuperclass(), nameSet, parentFirst));
}
return fieldList;
}
}

@ -0,0 +1,87 @@
package xyz.wbsite.frame.utils;
public class StringUtil {
public static int getByteLength(String str) {
int length = str.replaceAll("[^\\x00-\\xff]", "**").length();
return length;
}
public static String upperFirstWord(String str) {
String temp = str.substring(0, 1);
return temp.toUpperCase() + str.substring(1);
}
public static boolean isEmpty(String value) {
int strLen;
if (value == null || (strLen = value.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(value.charAt(i)) == false)) {
return false;
}
}
return true;
}
public static boolean isNotEmpty(String value) {
return !isEmpty(value);
}
/**
* ,
*/
public static boolean isNumeric(Object obj) {
if (obj == null) {
return false;
}
char[] chars = obj.toString().toCharArray();
int length = chars.length;
if (length < 1)
return false;
int i = 0;
if (length > 1 && chars[0] == '-')
i = 1;
for (; i < length; i++) {
if (!Character.isDigit(chars[i])) {
return false;
}
}
return true;
}
/**
*
*/
public static String unicodeToChinese(String unicode) {
StringBuilder out = new StringBuilder();
if (!isEmpty(unicode)) {
for (int i = 0; i < unicode.length(); i++) {
out.append(unicode.charAt(i));
}
}
return out.toString();
}
public static String toUnderlineStyle(String name) {
StringBuilder newName = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (Character.isUpperCase(c)) {
if (i > 0) {
newName.append("_");
}
newName.append(Character.toLowerCase(c));
} else {
newName.append(c);
}
}
return newName.toString();
}
}

@ -3,18 +3,14 @@ package ${basePackage}.frame.utils;
import ${basePackage}.frame.base.BaseRequest; import ${basePackage}.frame.base.BaseRequest;
import ${basePackage}.frame.base.BaseResponse; import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.base.ErrorType; import ${basePackage}.frame.base.ErrorType;
import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolation;
import javax.validation.Validation; import javax.validation.Validation;
import javax.validation.Validator; import javax.validation.Validator;
import javax.validation.ValidatorFactory; import javax.validation.ValidatorFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* *
@ -51,4 +47,26 @@ public class ValidationUtil {
} }
return response; return response;
} }
public static List<String> validate(Object req) {
List<String> validResult = new ArrayList<>();
if (req == null) {
validResult.add("当前数据为空");
return validResult;
}
try {
Validator validator = factory.getValidator();
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(req);
if (constraintViolations.size() > 0) {
for (ConstraintViolation<Object> violation : constraintViolations) {
validResult.add(violation.getMessage());
}
}
} catch (Exception e) {
e.printStackTrace();
validResult.add("数据检查错误");
}
return validResult;
}
} }

@ -27,6 +27,7 @@
<ehcache-version>1.1.0</ehcache-version> <ehcache-version>1.1.0</ehcache-version>
<dozer-version>5.5.1</dozer-version> <dozer-version>5.5.1</dozer-version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version> <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
<poi-ooxml-version>3.8</poi-ooxml-version>
</properties> </properties>
<repositories> <repositories>
@ -101,6 +102,11 @@
<artifactId>dozer</artifactId> <artifactId>dozer</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<#if dataBase == 'MYSQL'> <#if dataBase == 'MYSQL'>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
@ -149,6 +155,11 @@
<artifactId>dozer</artifactId> <artifactId>dozer</artifactId>
<version>${r'${dozer-version}'}</version> <version>${r'${dozer-version}'}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${r'${poi-ooxml-version}'}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

@ -41,8 +41,18 @@
<el-col :span="12"> <el-col :span="12">
<el-button-group style="float: right;"> <el-button-group style="float: right;">
<el-tooltip effect="dark" content="Excel模板下载" placement="bottom">
<el-button size="small" icon="el-icon-date"></el-button>
</el-tooltip>
<el-tooltip effect="dark" content="Excel导入" placement="bottom">
<el-button size="small" icon="el-icon-upload"></el-button>
</el-tooltip>
<el-tooltip effect="dark" content="批量删除" placement="bottom">
<el-button size="small" icon="el-icon-delete" @click="onBitchDelete"></el-button> <el-button size="small" icon="el-icon-delete" @click="onBitchDelete"></el-button>
</el-tooltip>
<el-tooltip effect="dark" content="刷新" placement="bottom">
<el-button size="small" icon="el-icon-refresh" @click="onFind"></el-button> <el-button size="small" icon="el-icon-refresh" @click="onFind"></el-button>
</el-tooltip>
</el-button-group> </el-button-group>
</el-col> </el-col>
</el-row> </el-row>
@ -93,12 +103,13 @@
</el-table> </el-table>
<el-pagination <el-pagination
style="margin-top: 10px" background
v-if="vm.totalCount > vm.pageSize" v-if="vm.totalCount > vm.pageSize"
style="margin-top: 10px"
@current-change="onPage" @current-change="onPage"
:current-page="vm.pageNumber" :current-page="vm.pageNumber"
:page-size="vm.pageSize" :page-size="vm.pageSize"
layout="total, prev, pager, next, jumper" layout="total, sizes, prev, pager, next, jumper"
:total="vm.totalCount"> :total="vm.totalCount">
</el-pagination> </el-pagination>
</el-card> </el-card>

@ -24,8 +24,8 @@
<el-card class="box-card"> <el-card class="box-card">
<el-row> <el-row>
<el-col :span="12"> <el-col :span="12">
<el-button type="success" size="small" icon="el-icon-plus" @click="onAction(['create',''])">新增 <el-button type="success" size="small" icon="el-icon-plus" @click="onAction(['create',''])">新增</el-button>
</el-button>
<el-button type="warning" size="small" icon="el-icon-download">导出</el-button> <el-button type="warning" size="small" icon="el-icon-download">导出</el-button>
<el-dialog class="form" :title="form.title" :visible.sync="form.dialog"> <el-dialog class="form" :title="form.title" :visible.sync="form.dialog">
@ -58,8 +58,18 @@
<el-col :span="12"> <el-col :span="12">
<el-button-group style="float: right;"> <el-button-group style="float: right;">
<el-tooltip effect="dark" content="Excel模板下载" placement="bottom">
<el-button size="small" icon="el-icon-date"></el-button>
</el-tooltip>
<el-tooltip effect="dark" content="Excel导入" placement="bottom">
<el-button size="small" icon="el-icon-upload"></el-button>
</el-tooltip>
<el-tooltip effect="dark" content="批量删除" placement="bottom">
<el-button size="small" icon="el-icon-delete" @click="onBitchDelete"></el-button> <el-button size="small" icon="el-icon-delete" @click="onBitchDelete"></el-button>
</el-tooltip>
<el-tooltip effect="dark" content="刷新" placement="bottom">
<el-button size="small" icon="el-icon-refresh" @click="onFind"></el-button> <el-button size="small" icon="el-icon-refresh" @click="onFind"></el-button>
</el-tooltip>
</el-button-group> </el-button-group>
</el-col> </el-col>
</el-row> </el-row>
@ -131,12 +141,13 @@
</el-table> </el-table>
<el-pagination <el-pagination
style="margin-top: 10px" background
v-if="vm.totalCount > vm.pageSize" v-if="vm.totalCount > vm.pageSize"
style="margin-top: 10px"
@current-change="onPage" @current-change="onPage"
:current-page="vm.pageNumber" :current-page="vm.pageNumber"
:page-size="vm.pageSize" :page-size="vm.pageSize"
layout="total, prev, pager, next, jumper" layout="total, sizes, prev, pager, next, jumper"
:total="vm.totalCount"> :total="vm.totalCount">
</el-pagination> </el-pagination>
</el-card> </el-card>

@ -28,6 +28,7 @@
<el-row> <el-row>
<el-col :span="12"> <el-col :span="12">
<el-button type="success" size="small" icon="el-icon-plus" @click="onAction(['create',''])">新增</el-button> <el-button type="success" size="small" icon="el-icon-plus" @click="onAction(['create',''])">新增</el-button>
<el-button type="warning" size="small" icon="el-icon-download">导出</el-button> <el-button type="warning" size="small" icon="el-icon-download">导出</el-button>
<el-dialog class="form" :title="form.title" :visible.sync="form.dialog"> <el-dialog class="form" :title="form.title" :visible.sync="form.dialog">
@ -55,8 +56,18 @@
<el-col :span="12"> <el-col :span="12">
<el-button-group style="float: right;"> <el-button-group style="float: right;">
<el-tooltip effect="dark" content="Excel模板下载" placement="bottom">
<el-button size="small" icon="el-icon-date"></el-button>
</el-tooltip>
<el-tooltip effect="dark" content="Excel导入" placement="bottom">
<el-button size="small" icon="el-icon-upload"></el-button>
</el-tooltip>
<el-tooltip effect="dark" content="批量删除" placement="bottom">
<el-button size="small" icon="el-icon-delete" @click="onBitchDelete"></el-button> <el-button size="small" icon="el-icon-delete" @click="onBitchDelete"></el-button>
</el-tooltip>
<el-tooltip effect="dark" content="刷新" placement="bottom">
<el-button size="small" icon="el-icon-refresh" @click="onFind"></el-button> <el-button size="small" icon="el-icon-refresh" @click="onFind"></el-button>
</el-tooltip>
</el-button-group> </el-button-group>
</el-col> </el-col>
</el-row> </el-row>
@ -126,11 +137,13 @@
</el-table> </el-table>
<el-pagination <el-pagination
background
v-if="vm.totalCount > vm.pageSize" v-if="vm.totalCount > vm.pageSize"
style="margin-top: 10px"
@current-change="onPage" @current-change="onPage"
:current-page="vm.pageNumber" :current-page="vm.pageNumber"
:page-size="vm.pageSize" :page-size="vm.pageSize"
layout="total, prev, pager, next, jumper" layout="total, sizes, prev, pager, next, jumper"
:total="vm.totalCount"> :total="vm.totalCount">
</el-pagination> </el-pagination>
</el-card> </el-card>

Loading…
Cancel
Save

Powered by TurnKey Linux.