1、Excel优化

Former-commit-id: 9db9fc9480eb5a56f35bb196f2ef3d50d7825a7f
master
wangbing 5 years ago
parent 4a05d55f1f
commit 042aa18a45

@ -3,7 +3,7 @@ package ${basePackage}.action.ajax.${module};
import com.fasterxml.jackson.core.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.excel.WSheet;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.utils.LogUtil;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.ErrorType;
@ -64,7 +64,7 @@ public class ${table.getCName()}Ajax{
<#if table.getHtml()>
public Object template(){
return ResponseUtil.apply(new WSheet<>(${table.getCName()}.class));
return ResponseUtil.apply(new WExcel<>(${table.getCName()}.class));
}
public Object exports(TreeNode jsonParam) {
@ -76,13 +76,13 @@ public class ${table.getCName()}Ajax{
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response;
}
return ResponseUtil.apply(new WSheet<>(response.getResult()));
return ResponseUtil.apply(new WExcel<>(response.getResult()));
}
public Object imports(MultipartFile file) {
BaseResponse baseResponse = new BaseResponse();
try {
WSheet<${table.getCName()}> sheet = new WSheet<>(file.getBytes(), ${table.getCName()}.class, new WSheet.Validator<${table.getCName()}>() {
WExcel<${table.getCName()}> sheet = new WExcel<>(file.getBytes(), ${table.getCName()}.class, new WExcel.Validator<${table.getCName()}>() {
@Override
public List<String> validate(${table.getCName()} o) {
${table.getCName()}CreateRequest request = MapperUtil.map(o, ${table.getCName()}CreateRequest.class);

@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.excel.WSheet;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.LogUtil;
@ -61,7 +61,7 @@ public class DeptAjax {
}
public Object template() {
return ResponseUtil.apply(new WSheet<>(Dept.class));
return ResponseUtil.apply(new WExcel<>(Dept.class));
}
public Object exports(TreeNode jsonParam) {
@ -73,13 +73,13 @@ public class DeptAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response;
}
return ResponseUtil.apply(new WSheet<>(response.getResult()));
return ResponseUtil.apply(new WExcel<>(response.getResult()));
}
public Object imports(MultipartFile file) {
BaseResponse baseResponse = new BaseResponse();
try {
WSheet<Dept> sheet = new WSheet<>(file.getBytes(), Dept.class, new WSheet.Validator<Dept>() {
WExcel<Dept> sheet = new WExcel<>(file.getBytes(), Dept.class, new WExcel.Validator<Dept>() {
@Override
public List<String> validate(Dept o) {
DeptCreateRequest request = MapperUtil.map(o, DeptCreateRequest.class);

@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.excel.WSheet;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.LogUtil;
@ -61,7 +61,7 @@ public class DictAjax {
}
public Object template() {
return ResponseUtil.apply(new WSheet<>(Dict.class));
return ResponseUtil.apply(new WExcel<>(Dict.class));
}
public Object exports(TreeNode jsonParam) {
@ -73,13 +73,13 @@ public class DictAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response;
}
return ResponseUtil.apply(new WSheet<>(response.getResult()));
return ResponseUtil.apply(new WExcel<>(response.getResult()));
}
public Object imports(MultipartFile file) {
BaseResponse baseResponse = new BaseResponse();
try {
WSheet<Dict> sheet = new WSheet<>(file.getBytes(), Dict.class, new WSheet.Validator<Dict>() {
WExcel<Dict> sheet = new WExcel<>(file.getBytes(), Dict.class, new WExcel.Validator<Dict>() {
@Override
public List<String> validate(Dict o) {
DictCreateRequest request = MapperUtil.map(o, DictCreateRequest.class);

@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.excel.WSheet;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.LogUtil;
@ -55,7 +55,7 @@ public class DictItemAjax {
}
public Object template() {
return ResponseUtil.apply(new WSheet<>(DictItem.class));
return ResponseUtil.apply(new WExcel<>(DictItem.class));
}
public Object exports(TreeNode jsonParam) {
@ -67,13 +67,13 @@ public class DictItemAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response;
}
return ResponseUtil.apply(new WSheet<>(response.getResult()));
return ResponseUtil.apply(new WExcel<DictItem>(DictItem.class).loadData(response.getResult()));
}
public Object imports(MultipartFile file) {
BaseResponse baseResponse = new BaseResponse();
try {
WSheet<DictItem> sheet = new WSheet<>(file.getBytes(), DictItem.class, new WSheet.Validator<DictItem>() {
WExcel<DictItem> sheet = new WExcel<>(file.getBytes(), DictItem.class, new WExcel.Validator<DictItem>() {
@Override
public List<String> validate(DictItem o) {
DictItemCreateRequest request = MapperUtil.map(o, DictItemCreateRequest.class);

@ -10,7 +10,7 @@ import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.excel.WSheet;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.LogUtil;
@ -55,7 +55,7 @@ public class FileAjax {
}
public Object template() {
return ResponseUtil.apply(new WSheet<>(File.class));
return ResponseUtil.apply(new WExcel<>(File.class));
}
public Object exports(TreeNode jsonParam) {
@ -67,13 +67,13 @@ public class FileAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response;
}
return ResponseUtil.apply(new WSheet<>(response.getResult()));
return ResponseUtil.apply(new WExcel<>(response.getResult()));
}
public Object imports(MultipartFile file) {
BaseResponse baseResponse = new BaseResponse();
try {
WSheet<File> sheet = new WSheet<>(file.getBytes(), File.class, new WSheet.Validator<File>() {
WExcel<File> sheet = new WExcel<>(file.getBytes(), File.class, new WExcel.Validator<File>() {
@Override
public List<String> validate(File o) {
FileCreateRequest request = MapperUtil.map(o, FileCreateRequest.class);

@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.excel.WSheet;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.LogUtil;
@ -61,7 +61,7 @@ public class ResAjax {
}
public Object template() {
return ResponseUtil.apply(new WSheet<>(Res.class));
return ResponseUtil.apply(new WExcel<>(Res.class));
}
public Object exports(TreeNode jsonParam) {
@ -73,13 +73,13 @@ public class ResAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response;
}
return ResponseUtil.apply(new WSheet<>(response.getResult()));
return ResponseUtil.apply(new WExcel<>(response.getResult()));
}
public Object imports(MultipartFile file) {
BaseResponse baseResponse = new BaseResponse();
try {
WSheet<Res> sheet = new WSheet<>(file.getBytes(), Res.class, new WSheet.Validator<Res>() {
WExcel<Res> sheet = new WExcel<>(file.getBytes(), Res.class, new WExcel.Validator<Res>() {
@Override
public List<String> validate(Res o) {
ResCreateRequest request = MapperUtil.map(o, ResCreateRequest.class);

@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.excel.WSheet;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.LogUtil;
@ -55,7 +55,7 @@ public class RoleAjax {
}
public Object template() {
return ResponseUtil.apply(new WSheet<>(Role.class));
return ResponseUtil.apply(new WExcel<>(Role.class));
}
public Object exports(TreeNode jsonParam) {
@ -67,13 +67,13 @@ public class RoleAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response;
}
return ResponseUtil.apply(new WSheet<>(response.getResult()));
return ResponseUtil.apply(new WExcel<>(response.getResult()));
}
public Object imports(MultipartFile file) {
BaseResponse baseResponse = new BaseResponse();
try {
WSheet<Role> sheet = new WSheet<>(file.getBytes(), Role.class, new WSheet.Validator<Role>() {
WExcel<Role> sheet = new WExcel<>(file.getBytes(), Role.class, new WExcel.Validator<Role>() {
@Override
public List<String> validate(Role o) {
RoleCreateRequest request = MapperUtil.map(o, RoleCreateRequest.class);

@ -6,7 +6,7 @@ import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.excel.WSheet;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.LogUtil;
@ -61,7 +61,7 @@ public class TokensAjax {
}
public Object template() {
return ResponseUtil.apply(new WSheet<>(Tokens.class));
return ResponseUtil.apply(new WExcel<>(Tokens.class));
}
public Object exports(TreeNode jsonParam) {
@ -73,13 +73,13 @@ public class TokensAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response;
}
return ResponseUtil.apply(new WSheet<>(response.getResult()));
return ResponseUtil.apply(new WExcel<>(response.getResult()));
}
public Object imports(MultipartFile file) {
BaseResponse baseResponse = new BaseResponse();
try {
WSheet<Tokens> sheet = new WSheet<>(file.getBytes(), Tokens.class, new WSheet.Validator<Tokens>() {
WExcel<Tokens> sheet = new WExcel<>(file.getBytes(), Tokens.class, new WExcel.Validator<Tokens>() {
@Override
public List<String> validate(Tokens o) {
TokensCreateRequest request = MapperUtil.map(o, TokensCreateRequest.class);

@ -7,7 +7,7 @@ import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.excel.WSheet;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.CookieUtil;
@ -196,7 +196,7 @@ public class UserAjax {
}
public Object template() {
return ResponseUtil.apply(new WSheet<>(User.class));
return ResponseUtil.apply(new WExcel<>(User.class));
}
public Object exports(TreeNode jsonParam) {
@ -208,13 +208,13 @@ public class UserAjax {
response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
return response;
}
return ResponseUtil.apply(new WSheet<>(response.getResult()));
return ResponseUtil.apply(new WExcel<>(response.getResult()));
}
public Object imports(MultipartFile file) {
BaseResponse baseResponse = new BaseResponse();
try {
WSheet<User> sheet = new WSheet<>(file.getBytes(), User.class, new WSheet.Validator<User>() {
WExcel<User> sheet = new WExcel<>(file.getBytes(), User.class, new WExcel.Validator<User>() {
@Override
public List<String> validate(User o) {
UserCreateRequest request = MapperUtil.map(o, UserCreateRequest.class);

@ -1,6 +1,5 @@
package ${basePackage}.frame.excel;
import java.io.Serializable;
/**
@ -17,6 +16,11 @@ public class WCell implements Serializable {
*/
private String value;
/**
* excel
*/
private String error;
public WCell() {
this.value = "";
}
@ -32,4 +36,12 @@ public class WCell implements Serializable {
public void setValue(String value) {
this.value = value;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}

@ -1,39 +1,71 @@
package ${basePackage}.frame.excel;
import ${basePackage}.frame.excel.converter.Converter;
import ${basePackage}.frame.excel.exception.ValueConverterException;
import ${basePackage}.frame.excel.style.DataCellStyle;
import ${basePackage}.frame.excel.style.ErrorCellStyle;
import ${basePackage}.frame.excel.style.HeadCellStyle;
import ${basePackage}.frame.excel.style.RedFont;
import ${basePackage}.frame.utils.ClassUtil;
import ${basePackage}.frame.utils.LogUtil;
import ${basePackage}.frame.utils.ValidationUtil;
import ${basePackage}.frame.excel.annotation.*;
import ${basePackage}.frame.excel.converter.*;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
package xyz.wbsite.frame.excel;
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 org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import xyz.wbsite.frame.excel.annotation.ColumnDescription;
import xyz.wbsite.frame.excel.annotation.ColumnName;
import xyz.wbsite.frame.excel.annotation.Ignore;
import xyz.wbsite.frame.excel.annotation.ParentFirst;
import xyz.wbsite.frame.excel.annotation.SheetName;
import xyz.wbsite.frame.excel.converter.BooleanConverter;
import xyz.wbsite.frame.excel.converter.ByteConverter;
import xyz.wbsite.frame.excel.converter.CharacterConverter;
import xyz.wbsite.frame.excel.converter.Converter;
import xyz.wbsite.frame.excel.converter.DateConverter;
import xyz.wbsite.frame.excel.converter.DoubleConverter;
import xyz.wbsite.frame.excel.converter.FloatConverter;
import xyz.wbsite.frame.excel.converter.IntegerConverter;
import xyz.wbsite.frame.excel.converter.LongConverter;
import xyz.wbsite.frame.excel.converter.ShortConverter;
import xyz.wbsite.frame.excel.converter.StringConverter;
import xyz.wbsite.frame.excel.exception.ReadErrorException;
import xyz.wbsite.frame.excel.exception.TemplateNotMatchException;
import xyz.wbsite.frame.excel.exception.ValueConverterException;
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.FileUtil;
import xyz.wbsite.frame.utils.LogUtil;
import xyz.wbsite.frame.utils.ValidationUtil;
import xyz.wbsite.module.system.ent.Dept;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* WSheet - Excel-Sheet
* WExcel - Excel-Sheet
*
* @author wangbing
* @version 0.0.1
* @since 2017-01-01
*/
public class WSheet<T> implements Serializable, Cloneable {
public class WExcel<T> implements Serializable, Cloneable {
/**
*
@ -41,10 +73,15 @@ public class WSheet<T> implements Serializable, Cloneable {
private String name;
/**
*
*
*/
private boolean freezeFirst;
/**
*
*/
private Class<T> templateClass;
/**
*
*/
@ -58,7 +95,8 @@ public class WSheet<T> implements Serializable, Cloneable {
/**
*
*/
public WSheet(Class<T> tClass) {
public WExcel(Class<T> tClass) {
this.templateClass = tClass;
initColumns(tClass);
}
@ -109,8 +147,8 @@ public class WSheet<T> implements Serializable, Cloneable {
}
//获取列类型
if (field.isAnnotationPresent(${basePackage}.frame.excel.annotation.Converter.class)) {
${basePackage}.frame.excel.annotation.Converter converter = field.getAnnotation(${basePackage}.frame.excel.annotation.Converter.class);
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((Converter) target.newInstance());
@ -166,16 +204,11 @@ public class WSheet<T> implements Serializable, Cloneable {
*
* @param list
*/
public WSheet(List<T> list) {
if (list == null || list.size() == 0) {
throw new RuntimeException("导出数据为空");
}
public xyz.wbsite.frame.excel.WExcel loadData(List<T> list) {
if (list.size() > 0) {
List<WColumn> wColumns = initColumns(list.get(0).getClass());
for (T t : list) {
WRow row = new WRow();
for (WColumn column : wColumns) {
for (WColumn column : columnList) {
if (column == null) {
continue;
}
@ -197,22 +230,18 @@ public class WSheet<T> implements Serializable, Cloneable {
this.rowList.add(row);
}
}
return this;
}
public xyz.wbsite.frame.excel.WExcel check(byte[] bytes) throws TemplateNotMatchException, ReadErrorException {
return check(bytes, null);
}
/**
* 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 TemplateNotMatchException, ReadErrorException {
this(bytes, clazz, null);
}
public WSheet(byte[] bytes, Class<T> clazz, Validator validator) throws TemplateNotMatchException, ReadErrorException {
public xyz.wbsite.frame.excel.WExcel check(byte[] bytes, Validator validator) throws TemplateNotMatchException, ReadErrorException {
Workbook workbook = null;
InputStream is = null;
boolean flag;
@ -245,8 +274,8 @@ public class WSheet<T> implements Serializable, Cloneable {
//第一张Sheet表
Sheet sheet = workbook.getSheetAt(0);
//获取Sheet名称
if (clazz.isAnnotationPresent(SheetName.class)) {//如果模板存在注解则使用注解名称
SheetName sheetName = clazz.getAnnotation(SheetName.class);
if (templateClass.isAnnotationPresent(SheetName.class)) {//如果模板存在注解则使用注解名称
SheetName sheetName = templateClass.getAnnotation(SheetName.class);
this.setName(sheetName.value());
} else {//将类名设为表名如果类名不存在将Excel表名设为表名
this.setName(sheet.getSheetName());
@ -258,12 +287,11 @@ public class WSheet<T> implements Serializable, Cloneable {
int columnSum = headRow.getPhysicalNumberOfCells();
//检查列数量
List<WColumn> list = initColumns(clazz);
if (list.size() != columnSum) {
if (columnList.size() != columnSum) {
throw new TemplateNotMatchException("与模板列数量不同。");
} else {
for (int i = 0; i < list.size(); i++) {
WColumn wColumn = list.get(i);
for (int i = 0; i < columnList.size(); i++) {
WColumn wColumn = columnList.get(i);
Cell cell = headRow.getCell(i);
String headValue = getValue(cell);
headValue = headValue.replace("*", "");
@ -302,7 +330,7 @@ public class WSheet<T> implements Serializable, Cloneable {
}
try {
T t = clazz.newInstance();
T t = templateClass.newInstance();
List<String> errs = transferMap(row, t);
errs.addAll(ValidationUtil.validate(t));
errs.addAll(validator != null ? validator.validate(t) : Collections.emptyList());
@ -315,6 +343,7 @@ public class WSheet<T> implements Serializable, Cloneable {
row.addError("模板对象默认构造函数错误");
}
}
return this;
}
/**
@ -380,33 +409,27 @@ public class WSheet<T> implements Serializable, Cloneable {
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();
// }
public final String toCSV() {
StringBuilder sb = new StringBuilder();
for (WColumn WColumn : this.columnList) {
if (WColumn != null) {
sb.append(WColumn.getName()).append(",");
}
}
sb.append("\n");
for (int i = 0; i < this.rowList.size(); i++) {
for (int j = 0; j < this.columnList.size(); j++) {
WColumn wColumn = this.columnList.get(j);
WCell wCell = this.rowList.get(i).get(wColumn.getName());
sb.append(wCell.getValue()).append(",");
}
sb.append("\n");
}
return sb.toString();
}
/**
* DataTable
@ -422,18 +445,8 @@ public class WSheet<T> implements Serializable, Cloneable {
return false;
}
/**
* Excel
*
* @return
* @throws IOException
*/
public byte[] getBytes() {
return getBytes(false);
}
public byte[] getBytes(boolean checkResult) {
XSSFWorkbook workbook = getExcel(checkResult);
XSSFWorkbook workbook = getExcel();
if (workbook != null) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
@ -446,7 +459,7 @@ public class WSheet<T> implements Serializable, Cloneable {
return null;
}
public XSSFWorkbook getExcel(boolean checkResult) {
public XSSFWorkbook getExcel() {
XSSFWorkbook workbook = new XSSFWorkbook();
//创建一个Sheet表
XSSFSheet sheet = workbook.createSheet(name);
@ -455,13 +468,29 @@ public class WSheet<T> implements Serializable, Cloneable {
XSSFDrawing drawing = sheet.createDrawingPatriarch();
int offset = 0;
// 添加错误说明列
if (checkResult && hasError()) {
// 添加结果和错误说明列
if (hasError()) {
offset++;
{// 第一栏结果栏
Cell firstCell = firstRow.createCell(0);
firstCell.setCellStyle(new ErrorCellStyle(workbook).getStyle());
firstCell.setCellValue(new XSSFRichTextString("检查结果说明"));
sheet.setColumnWidth(0, 18 * 256);
firstCell.setCellStyle(new HeadCellStyle(workbook).getStyle());
firstCell.setCellValue(new XSSFRichTextString("执行结果"));
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6);
XSSFComment comment = drawing.createCellComment(anchor);
comment.setString("此列为检查结果,导入时请处理完错误,并删除该列!");
firstCell.setCellComment(comment);
}
offset++;
{// 第二栏错误信息栏
Cell secondCell = firstRow.createCell(1);
secondCell.setCellStyle(new HeadCellStyle(workbook).getStyle());
secondCell.setCellValue(new XSSFRichTextString("结果说明"));
sheet.setColumnWidth(1, 18 * 256);
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6);
XSSFComment comment = drawing.createCellComment(anchor);
comment.setString("此列为检查结果信息列,导入时请处理完错误,并删除该列!");
secondCell.setCellComment(comment);
}
}
for (int j = 0; j < this.columnList.size(); j++) {
@ -499,16 +528,29 @@ public class WSheet<T> implements Serializable, Cloneable {
// 填充数据
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) {
if (this.rowList.get(i).hasError()) {
// 添加结果
List<String> errorList = wRow.getErrorList();
Cell xssfCell = row.createCell(0);
xssfCell.setCellStyle(new ErrorCellStyle(workbook).getStyle());
Cell resultCell = row.createCell(0);
resultCell.setCellStyle(new ErrorCellStyle(workbook).getStyle());
resultCell.setCellValue("错误");
// 添加错误信息详细说明
Cell errsCell = row.createCell(1);
errsCell.setCellStyle(new ErrorCellStyle(workbook).getStyle());
String join = String.join(";", errorList);
xssfCell.setCellValue(new XSSFRichTextString(join));
errsCell.setCellValue(new XSSFRichTextString(join));
} else {
// 添加结果
Cell resultCell = row.createCell(0);
resultCell.setCellStyle(new xyz.wbsite.frame.excel.style.SuccessCellStyle(workbook).getStyle());
resultCell.setCellValue("成功");
// 添加错误信息详细说明
Cell errsCell = row.createCell(1);
errsCell.setCellStyle(new xyz.wbsite.frame.excel.style.SuccessCellStyle(workbook).getStyle());
errsCell.setCellValue("");
}
for (int j = 0; j < this.columnList.size(); j++) {
@ -565,4 +607,42 @@ public class WSheet<T> implements Serializable, Cloneable {
public interface Validator<T> {
List<String> validate(T t);
}
public void toFile(File file) throws IOException {
File parentFile = file.getParentFile();
if (!parentFile.exists() && !parentFile.mkdirs()) {
throw new RuntimeException("路径创建失败");
}
if (!file.exists() && !file.createNewFile()) {
throw new RuntimeException("文件创建失败");
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(getBytes());
fileOutputStream.close();
}
public static void main(String[] args) throws IOException {
Dept dept = new Dept();
dept.setDeptName("AAAAAA");
ArrayList<Dept> depts = new ArrayList<Dept>();
depts.add(dept);
File file = new File("E:\\E.xlsx");
// 导出模板
// new WExcel<Dept>(Dept.class).loadData(depts).toFile(file);
// 检查excel文件是否符合导入
byte[] bytes = FileUtil.readFileToByteArray(new File("E:\\E.xlsx"));
try {
xyz.wbsite.frame.excel.WExcel check = new xyz.wbsite.frame.excel.WExcel<Dept>(Dept.class).check(bytes);
check.toFile(new File("E:\\E_err.xlsx"));
System.out.println(check.hasError());
} catch (TemplateNotMatchException e) {
e.printStackTrace();
} catch (ReadErrorException e) {
e.printStackTrace();
}
System.out.println();
}
}

@ -0,0 +1,22 @@
package xyz.wbsite.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 SuccessCellStyle extends BaseCellStyle {
public SuccessCellStyle(Workbook workbook) {
super(workbook);
style.setFillForegroundColor(HSSFColor.GREEN.index); //背景颜色红色
style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式
}
public CellStyle getStyle() {
return style;
}
public void setStyle(CellStyle style) {
this.style = style;
}
}

@ -4,7 +4,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import ${basePackage}.frame.excel.WSheet;
import ${basePackage}.frame.excel.WExcel;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@ -36,7 +36,7 @@ public class ResponseUtil {
}
}
public static ResponseEntity<byte[]> apply(WSheet sheet) {
public static ResponseEntity<byte[]> apply(WExcel sheet) {
return apply(sheet.getBytes(), sheet.getName() + ".xlsx");
}
}

@ -46,7 +46,7 @@ import ${basePackage}.module.system.rsp.TokensUpdateResponse;
@Service
public class TokensManagerImpl implements xyz.wbsite.module.system.mgr.TokensManager {
@Value("${web.url.auth.admin}")
@Value("${r'${web.url.auth.admin}'}")
private String admin;
@Autowired
private TokensMapper tokensMapper;

Loading…
Cancel
Save

Powered by TurnKey Linux.