master
wangbing 5 years ago
parent b86062b7a7
commit 6314510243

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

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

@ -14,4 +14,6 @@ import java.lang.annotation.*;
@Documented
public @interface SheetName {
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.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
/**
@ -17,16 +18,34 @@ public class BaseCellStyle {
protected CellStyle style;
public BaseCellStyle(Workbook workbook) {
this(workbook, false);
}
public BaseCellStyle(Workbook workbook, boolean error) {
style = workbook.createCellStyle();
style.setFillPattern(CellStyle.NO_FILL); //单元格不填充
if (error) {
style.setFillForegroundColor(HSSFColor.RED.index); //背景颜色红色
style.setFillPattern(CellStyle.SOLID_FOREGROUND); //设置单元格填充样式
}
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() {

@ -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.setAlignment(CellStyle.ALIGN_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) {
e.printStackTrace();
validResult.add("数据检查错误");
}
return validResult;

@ -13,8 +13,8 @@ import java.util.List;
public class ExampleTest {
public static void main(String[] args) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// testTemplate();
// testExport();
testTemplate();
testExport();
testImport();
}
@ -33,20 +33,24 @@ public class ExampleTest {
//第一步,准备数据模型及数据,模型需要打上注解
List<User> pos = new ArrayList();
User user = new User();
user.setWz("张三");
user.setDate(new Date());
user.setBr(true);
user.setBt((byte) 1);
user.setZf('A');
user.setDzs((short) 1);
user.setZs(1);
user.setCzs((long) 1);
user.setFd(1.0f);
user.setSjd(1.0);
pos.add(user);
pos.add(user);
{
User user = new User();
user.setWz("张三");
user.setDate(new Date());
user.setBr(true);
user.setCzs(1500);
pos.add(user);
}
{
User user = new User();
user.setWz("张三");
pos.add(user);
}
{
User user = new User();
user.setWz("张四");
pos.add(user);
}
//第二步,初始化数据
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.SheetName;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.*;
import java.util.Date;
@SheetName("用户")
@ -16,6 +15,7 @@ public class User {
@ColumnName("日期时间")
private Date date;
@ColumnName("布尔")
@AssertTrue(message = "布尔必须是TRUE")
private boolean br;
@ColumnName("比特")
private byte bt;
@ -26,9 +26,11 @@ public class User {
@ColumnName("整数")
private int zs;
@ColumnName("长整数")
@Min(value = 100,message = "最低不得小于100")
@Max(value = 2000, message = "长整数是需<2000之间")
@Min(value = 1000, message = "长整数是需>1000之间")
private long czs;
@ColumnName("浮点")
@Max(value = 1, message = "浮点必须小于1之间")
private float fd;
@ColumnName("双精度")
private double sjd;

Loading…
Cancel
Save

Powered by TurnKey Linux.