master
wangbing 5 years ago
parent b86062b7a7
commit 6314510243

@ -36,6 +36,11 @@ public class WSheet<T> implements Serializable, Cloneable {
*/ */
private String name; private String name;
/**
*
*/
private boolean freezeFirst;
/** /**
* *
*/ */
@ -60,9 +65,10 @@ public class WSheet<T> implements Serializable, Cloneable {
*/ */
private List<WColumn> initColumns(Class<?> clazz) { private List<WColumn> initColumns(Class<?> clazz) {
//获取工作簿名称,没有则以类名为默认工作簿名称 //获取工作簿名称,没有则以类名为默认工作簿名称
SheetName sheetName = clazz.getAnnotation(SheetName.class); if (clazz.isAnnotationPresent(SheetName.class)) {
if (sheetName != null) { SheetName sheetName = clazz.getAnnotation(SheetName.class);
this.setName(sheetName.value()); this.setName(sheetName.value());
this.setFreezeFirst(sheetName.freezeFirst());
} else { } else {
this.setName(clazz.getName()); this.setName(clazz.getName());
} }
@ -83,6 +89,7 @@ public class WSheet<T> implements Serializable, Cloneable {
} else { } else {
ColumnName columnColumnName = field.getAnnotation(ColumnName.class); ColumnName columnColumnName = field.getAnnotation(ColumnName.class);
WColumn.setName(columnColumnName.value()); WColumn.setName(columnColumnName.value());
WColumn.setCellWidth(columnColumnName.width());
} }
//获取列填写说明或描述 //获取列填写说明或描述
if (field.isAnnotationPresent(ColumnDescription.class)) { if (field.isAnnotationPresent(ColumnDescription.class)) {
@ -182,8 +189,8 @@ public class WSheet<T> implements Serializable, Cloneable {
if (null == value) { if (null == value) {
row.put(column.getName(), new WCell()); row.put(column.getName(), new WCell());
} else { } else {
column.getConverter().string(value); String string = column.getConverter().string(value);
row.put(column.getName(), new WCell()); row.put(column.getName(), new WCell(string));
} }
} }
this.rowList.add(row); this.rowList.add(row);
@ -231,8 +238,8 @@ public class WSheet<T> implements Serializable, Cloneable {
//第一张Sheet表 //第一张Sheet表
Sheet sheet = workbook.getSheetAt(0); Sheet sheet = workbook.getSheetAt(0);
//获取Sheet名称 //获取Sheet名称
SheetName sheetName = clazz.getAnnotation(SheetName.class); if (clazz.isAnnotationPresent(SheetName.class)) {//如果模板存在注解则使用注解名称
if (sheetName != null) {//如果模板存在注解则使用注解名称 SheetName sheetName = clazz.getAnnotation(SheetName.class);
this.setName(sheetName.value()); this.setName(sheetName.value());
} else {//将类名设为表名如果类名不存在将Excel表名设为表名 } else {//将类名设为表名如果类名不存在将Excel表名设为表名
this.setName(sheet.getSheetName()); this.setName(sheet.getSheetName());
@ -296,6 +303,7 @@ public class WSheet<T> implements Serializable, Cloneable {
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
row.addError("数据检查错误"); row.addError("数据检查错误");
} }
} }
@ -390,6 +398,14 @@ public class WSheet<T> implements Serializable, Cloneable {
this.name = name; this.name = name;
} }
public boolean getFreezeFirst() {
return freezeFirst;
}
public void setFreezeFirst(boolean freezeFirst) {
this.freezeFirst = freezeFirst;
}
// //
// public final String toCSV() { // public final String toCSV() {
// StringBuilder sb = new StringBuilder(); // StringBuilder sb = new StringBuilder();
@ -460,43 +476,35 @@ public class WSheet<T> implements Serializable, Cloneable {
public XSSFWorkbook getExcel(boolean checkResult) { public XSSFWorkbook getExcel(boolean checkResult) {
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook workbook = new XSSFWorkbook();
//---------- 创建样式 ------------------
CellStyle headCellStyle = new HeadCellStyle(workbook).getStyle();
CellStyle alignRightCellStyle = new AlignRightCellStyle(workbook).getStyle();
//----------- 创建字体 ----------------
Font headFont = new HeadFont(workbook).getFont();
Font normalFont = new NormalFont(workbook).getFont();
Font redFont = new RedFont(workbook).getFont();
//创建一个Sheet表 //创建一个Sheet表
XSSFSheet sheet = workbook.createSheet(name); XSSFSheet sheet = workbook.createSheet(name);
sheet.setDefaultRowHeightInPoints(18); sheet.setDefaultRowHeightInPoints(18);
Row firstRow = sheet.createRow(0); // 下标为0的行开始 Row firstRow = sheet.createRow(0); // 下标为0的行开始
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
// 错误表头 int offset = 0;
// if (checkResult) { // 添加错误说明列
// // 检查结果栏 if (checkResult && hasError()) {
// Cell firstCell = firstRow.createCell(this.columnList.size()); offset++;
// firstCell.setCellStyle(headCellStyle); Cell firstCell = firstRow.createCell(0);
// firstCell.setCellValue(new XSSFRichTextString("检查结果")); firstCell.setCellStyle(new ErrorCellStyle(workbook).getStyle());
// sheet.setColumnWidth(0, (4 + 8) * 256); firstCell.setCellValue(new XSSFRichTextString("检查结果说明"));
// } sheet.setColumnWidth(0, 18 * 256);
}
for (int j = 0; j < this.columnList.size(); j++) { for (int j = 0; j < this.columnList.size(); j++) {
WColumn column = this.columnList.get(j); WColumn column = this.columnList.get(j);
Field field = column.getField(); Field field = column.getField();
Cell firstCell = firstRow.createCell(j); Cell firstCell = firstRow.createCell(j + offset);
String columnName = column.getName(); String columnName = column.getName();
XSSFRichTextString textString; XSSFRichTextString textString;
if (column.isRequired()) { if (column.isRequired()) {
textString = new XSSFRichTextString("*" + columnName); textString = new XSSFRichTextString("*" + columnName);
textString.applyFont(0, 1, redFont); textString.applyFont(0, 1, new RedFont(workbook).getFont());
textString.applyFont(1, textString.length(), headFont); textString.applyFont(1, textString.length(), workbook.createFont());
} else { } else {
textString = new XSSFRichTextString(columnName); textString = new XSSFRichTextString(columnName);
textString.applyFont(headFont); textString.applyFont(workbook.createFont());
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(column.getDescription()).append("\n"); sb.append(column.getDescription()).append("\n");
@ -516,48 +524,47 @@ public class WSheet<T> implements Serializable, Cloneable {
firstCell.setCellComment(comment); firstCell.setCellComment(comment);
} }
firstCell.setCellValue(textString); firstCell.setCellValue(textString);
firstCell.setCellStyle(headCellStyle); firstCell.setCellStyle(new HeadCellStyle(workbook).getStyle());
sheet.setColumnWidth(j, (4 + column.getCellWidth()) * 256); sheet.setColumnWidth(j + offset, (4 + column.getCellWidth()) * 256);
} }
// 错误消息栏
// if (checkResult) {
// Cell firstCell = firstRow.createCell(this.columnList.size());
// firstCell.setCellStyle(headCellStyle);
// firstCell.setCellValue(new XSSFRichTextString("======================"));
// sheet.setColumnWidth(this.columnList.size() + 1, (4 + 10) * 256);
// }
// 冻结第一行 // 冻结第一行
// sheet.createFreezePane(255,1); if (freezeFirst) {
sheet.createFreezePane(255, 1);
}
// 填充数据 // 填充数据
for (int i = 0; i < this.rowList.size(); i++) { for (int i = 0; i < this.rowList.size(); i++) {
boolean flag = this.rowList.get(i).; boolean flag = this.rowList.get(i).hasError();
WRow wRow = this.rowList.get(i); WRow wRow = this.rowList.get(i);
Row row = sheet.createRow(i + 1); 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++) { for (int j = 0; j < this.columnList.size(); j++) {
WColumn column = this.columnList.get(j); WColumn column = this.columnList.get(j);
Cell xssfCell = row.createCell(j); Cell xssfCell = row.createCell(j + offset);
WCell WCell = this.rowList.get(i).get(column.getName()); WCell WCell = this.rowList.get(i).get(column.getName());
if (null == WCell) { if (null == WCell) {
continue; continue;
} }
String value = WCell.getValue(); String value = WCell.getValue();
List<String> errorList = wRow.getErrorList();
xssfCell.setCellType(column.getCellType()); xssfCell.setCellType(column.getCellType());
if (column.getCellType() == Cell.CELL_TYPE_NUMERIC || column.getCellType() == Cell.CELL_TYPE_BOOLEAN) { if (column.getCellType() == Cell.CELL_TYPE_NUMERIC || column.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
xssfCell.setCellStyle(new AlignRightCellStyle(workbook, wRow.hasError()).getStyle()); xssfCell.setCellStyle(new DataCellStyle(workbook, CellStyle.ALIGN_RIGHT, wRow.hasError()).getStyle());
} else { } else {
xssfCell.setCellStyle(new NormalCellStyle(workbook, flag).getStyle()); xssfCell.setCellStyle(new DataCellStyle(workbook, CellStyle.ALIGN_LEFT, wRow.hasError()).getStyle());
} }
xssfCell.setCellValue(value); xssfCell.setCellValue(value);
} }
if (flag)
} }
return workbook; return workbook;
} }
@ -569,6 +576,9 @@ public class WSheet<T> implements Serializable, Cloneable {
* @return * @return
*/ */
public static String getValue(Cell cell) { public static String getValue(Cell cell) {
if (cell == null) {
return "";
}
switch (cell.getCellType()) { switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING: case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue(); return cell.getStringCellValue();

@ -20,7 +20,7 @@ public @interface ColumnName {
* *
* @return * @return
*/ */
String orderNum() default "0"; int width() default 8;
/** /**
* *

@ -14,4 +14,6 @@ import java.lang.annotation.*;
@Documented @Documented
public @interface SheetName { public @interface SheetName {
String value(); String value();
boolean freezeFirst() default false;
} }

@ -1,38 +0,0 @@
package com.wb.excel.api.interfaces;
/**
*
* Created on 2014/9/27.
*
* @author
* @version 0.1.0
*/
@Deprecated
public interface EnumSupport {
/**
* <br/>
*
*
* @param str
* @return true || false / ||
*/
Boolean check(String str);
/**
* ValueKey<br/>
* MalemaleFF
*
* @param value
* @return Key
*/
Object getKey(String value);
/**
* KeyValue<br/>
* F,
*
* @param key
* @return Value
*/
String getValue(Object key);
}

@ -1,34 +0,0 @@
package com.wb.excel.api.interfaces;
import com.wb.excel.api.entity.DataVerifyResult;
/**
*
*/
public interface IExcelVerifyHandler {
// /**
// * 获取需要处理的字段,导入和导出统一处理了, 减少书写的字段
// *
// * @return
// */
// public String[] getNeedVerifyFields();
//
// /**
// * 获取需要处理的字段,导入和导出统一处理了, 减少书写的字段
// *
// * @return
// */
// public void setNeedVerifyFields(String[] arr);
/**
*
*
* @param obj
* @param name
* @param value
* @return
*/
DataVerifyResult verifyHandler(Object obj, String name, Object value);
}

@ -1,24 +0,0 @@
package com.wb.excel.api.style;
import org.apache.poi.ss.usermodel.CellStyle;
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 AlignRightCellStyle extends NormalCellStyle {
public AlignRightCellStyle(Workbook workbook) {
this(workbook, false);
}
public AlignRightCellStyle(Workbook workbook, boolean error) {
super(workbook, error);
style.setAlignment(CellStyle.ALIGN_RIGHT);
Font font = new BaseFont(workbook).getFont();
style.setFont(font);
}
}

@ -2,6 +2,7 @@ package com.wb.excel.api.style;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
/** /**
@ -17,16 +18,34 @@ public class BaseCellStyle {
protected CellStyle style; protected CellStyle style;
public BaseCellStyle(Workbook workbook) { public BaseCellStyle(Workbook workbook) {
this(workbook, false);
}
public BaseCellStyle(Workbook workbook, boolean error) {
style = workbook.createCellStyle(); style = workbook.createCellStyle();
style.setFillPattern(CellStyle.NO_FILL); //单元格不填充 style.setFillPattern(CellStyle.NO_FILL);
if (error) { //下边框
style.setFillForegroundColor(HSSFColor.RED.index); //背景颜色红色 style.setBorderBottom(CellStyle.SOLID_FOREGROUND);
style.setFillPattern(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() { public CellStyle getStyle() {

@ -0,0 +1,29 @@
package com.wb.excel.api.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 com.wb.excel.api.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;
}
}

@ -12,23 +12,5 @@ public class HeadCellStyle extends BaseCellStyle {
style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格填充样式 style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格填充样式
style.setAlignment(CellStyle.ALIGN_CENTER); // 居中 style.setAlignment(CellStyle.ALIGN_CENTER); // 居中
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//上下居中 style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//上下居中
style.setFont(new HeadFont(workbook).getFont());
//下边框
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());
} }
} }

@ -1,16 +0,0 @@
package com.wb.excel.api.style;
import org.apache.poi.ss.usermodel.Workbook;
/**
* .
* Created on 2015/1/29.
*
* @author
* @since 2.0.0
*/
public class HeadFont extends BaseFont {
public HeadFont(Workbook workbook) {
super(workbook);
}
}

@ -1,30 +0,0 @@
package com.wb.excel.api.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 NormalCellStyle extends BaseCellStyle {
public NormalCellStyle(Workbook workbook) {
this(workbook, false);
}
public NormalCellStyle(Workbook workbook, boolean error) {
super(workbook, error);
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()); //上边框颜色
}
}

@ -35,6 +35,7 @@ public class ValidationUtil {
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
validResult.add("数据检查错误"); validResult.add("数据检查错误");
} }
return validResult; return validResult;

@ -13,8 +13,8 @@ import java.util.List;
public class ExampleTest { public class ExampleTest {
public static void main(String[] args) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { public static void main(String[] args) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// testTemplate(); testTemplate();
// testExport(); testExport();
testImport(); testImport();
} }
@ -33,20 +33,24 @@ public class ExampleTest {
//第一步,准备数据模型及数据,模型需要打上注解 //第一步,准备数据模型及数据,模型需要打上注解
List<User> pos = new ArrayList(); List<User> pos = new ArrayList();
User user = new User(); {
user.setWz("张三"); User user = new User();
user.setDate(new Date()); user.setWz("张三");
user.setBr(true); user.setDate(new Date());
user.setBt((byte) 1); user.setBr(true);
user.setZf('A'); user.setCzs(1500);
user.setDzs((short) 1); pos.add(user);
user.setZs(1); }
user.setCzs((long) 1); {
user.setFd(1.0f); User user = new User();
user.setSjd(1.0); user.setWz("张三");
pos.add(user); pos.add(user);
pos.add(user); }
{
User user = new User();
user.setWz("张四");
pos.add(user);
}
//第二步,初始化数据 //第二步,初始化数据
WSheet<User> WSheet = new WSheet<>(pos); WSheet<User> WSheet = new WSheet<>(pos);

@ -2,8 +2,7 @@ import com.wb.excel.api.annotation.ColumnDescription;
import com.wb.excel.api.annotation.ColumnName; import com.wb.excel.api.annotation.ColumnName;
import com.wb.excel.api.annotation.SheetName; import com.wb.excel.api.annotation.SheetName;
import javax.validation.constraints.Min; import javax.validation.constraints.*;
import javax.validation.constraints.NotNull;
import java.util.Date; import java.util.Date;
@SheetName("用户") @SheetName("用户")
@ -16,6 +15,7 @@ public class User {
@ColumnName("日期时间") @ColumnName("日期时间")
private Date date; private Date date;
@ColumnName("布尔") @ColumnName("布尔")
@AssertTrue(message = "布尔必须是TRUE")
private boolean br; private boolean br;
@ColumnName("比特") @ColumnName("比特")
private byte bt; private byte bt;
@ -26,9 +26,11 @@ public class User {
@ColumnName("整数") @ColumnName("整数")
private int zs; private int zs;
@ColumnName("长整数") @ColumnName("长整数")
@Min(value = 100,message = "最低不得小于100") @Max(value = 2000, message = "长整数是需<2000之间")
@Min(value = 1000, message = "长整数是需>1000之间")
private long czs; private long czs;
@ColumnName("浮点") @ColumnName("浮点")
@Max(value = 1, message = "浮点必须小于1之间")
private float fd; private float fd;
@ColumnName("双精度") @ColumnName("双精度")
private double sjd; private double sjd;

Loading…
Cancel
Save

Powered by TurnKey Linux.