|
|
|
@ -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 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 firstCell = firstRow.createCell(0);
|
|
|
|
|
firstCell.setCellStyle(new ErrorCellStyle(workbook).getStyle());
|
|
|
|
|
firstCell.setCellValue(new XSSFRichTextString("检查结果说明"));
|
|
|
|
|
sheet.setColumnWidth(0, 18 * 256);
|
|
|
|
|
{// 第二栏错误信息栏
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|