|
@@ -1,16 +1,17 @@
|
|
|
package com.qmth.teachcloud.common.util.excel;
|
|
|
|
|
|
-import org.apache.poi.ss.usermodel.Cell;
|
|
|
-import org.apache.poi.ss.usermodel.FillPatternType;
|
|
|
-import org.apache.poi.ss.usermodel.Row;
|
|
|
-import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import com.qmth.teachcloud.common.annotation.excelStyle.ExcelDataStyle;
|
|
|
+import com.qmth.teachcloud.common.annotation.excelStyle.ExcelHeaderStyle;
|
|
|
+import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFColor;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
-import java.awt.*;
|
|
|
+import java.awt.Color;
|
|
|
import java.io.OutputStream;
|
|
|
+import java.lang.reflect.Field;
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.util.Collection;
|
|
@@ -19,7 +20,7 @@ import java.util.Objects;
|
|
|
|
|
|
public class ExcelWriter extends ExcelExport {
|
|
|
|
|
|
- private XSSFWorkbook workbook;// 工作簿
|
|
|
+ private final XSSFWorkbook workbook;// 工作簿
|
|
|
|
|
|
private Sheet sheet; // 工作表
|
|
|
|
|
@@ -59,11 +60,56 @@ public class ExcelWriter extends ExcelExport {
|
|
|
List<ColumnSetting> columnSettings = this.getColumnSettings();
|
|
|
// 产生表格标题行
|
|
|
row = sheet.createRow(0);
|
|
|
- for (short i = 0; i < columnSettings.size(); i++) {
|
|
|
- cell = row.createCell(i);
|
|
|
- style = workbook.createCellStyle();
|
|
|
+ style = workbook.createCellStyle();
|
|
|
+ ExcelHeaderStyle excelHeaderStyle = this.getDataClass().getAnnotation(ExcelHeaderStyle.class);
|
|
|
+ if (Objects.nonNull(excelHeaderStyle)) {
|
|
|
+ // 文字位置
|
|
|
+ style.setAlignment(excelHeaderStyle.horizontal());
|
|
|
+ style.setVerticalAlignment(excelHeaderStyle.vertical());
|
|
|
+
|
|
|
+ //边框
|
|
|
+ int borderRed = excelHeaderStyle.borderColorRed();
|
|
|
+ int borderGreen = excelHeaderStyle.borderColorGreen();
|
|
|
+ int borderBlue = excelHeaderStyle.borderColorBlue();
|
|
|
+ XSSFColor borderColor = new XSSFColor(new Color(borderRed, borderGreen, borderBlue));
|
|
|
+ // 上边框
|
|
|
+ if (excelHeaderStyle.borderTop().getCode() > 0) {
|
|
|
+ style.setBorderTop(excelHeaderStyle.borderTop());
|
|
|
+ style.setTopBorderColor(borderColor);
|
|
|
+ }
|
|
|
+ // 下边框
|
|
|
+ if (excelHeaderStyle.borderBottom().getCode() > 0) {
|
|
|
+ style.setBorderBottom(excelHeaderStyle.borderBottom());
|
|
|
+ style.setBottomBorderColor(borderColor);
|
|
|
+ }
|
|
|
+ // 左边框
|
|
|
+ if (excelHeaderStyle.borderLeft().getCode() > 0) {
|
|
|
+ style.setBorderLeft(excelHeaderStyle.borderLeft());
|
|
|
+ style.setLeftBorderColor(borderColor);
|
|
|
+ }
|
|
|
+ // 右边框
|
|
|
+ if (excelHeaderStyle.borderRight().getCode() > 0) {
|
|
|
+ style.setBorderRight(excelHeaderStyle.borderRight());
|
|
|
+ style.setRightBorderColor(borderColor);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 背景
|
|
|
+ style.setFillPattern(excelHeaderStyle.fillPartner());
|
|
|
+ XSSFColor fillColor = new XSSFColor(new Color(excelHeaderStyle.fillColorRed(), excelHeaderStyle.fillColorGreen(), excelHeaderStyle.fillColorBlue()));
|
|
|
+ style.setFillForegroundColor(fillColor);
|
|
|
+
|
|
|
+ // 字体
|
|
|
+ Font font = workbook.createFont();
|
|
|
+ font.setFontHeightInPoints(excelHeaderStyle.fontSize());
|
|
|
+ font.setFontName(excelHeaderStyle.fontName());
|
|
|
+ font.setColor(excelHeaderStyle.fontColor().getIndex());
|
|
|
+ style.setFont(font);
|
|
|
+ } else {
|
|
|
style.setFillForegroundColor(new XSSFColor(new Color(227, 239, 217)));
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ }
|
|
|
+ for (short i = 0; i < columnSettings.size(); i++) {
|
|
|
+ cell = row.createCell(i);
|
|
|
cell.setCellStyle(style);
|
|
|
XSSFRichTextString text = new XSSFRichTextString(columnSettings.get(i).getHeader());
|
|
|
cell.setCellValue(text);
|
|
@@ -95,7 +141,9 @@ public class ExcelWriter extends ExcelExport {
|
|
|
// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
|
|
|
for (short i = 0; i < columnSettings.size(); i++) {
|
|
|
cell = row.createCell(i);// 创建列
|
|
|
- cell.setCellStyle(style2);
|
|
|
+ Field field = columnSettings.get(i).getField();
|
|
|
+
|
|
|
+ cell.setCellStyle(this.getCellStyle(workbook.createCellStyle(), field));
|
|
|
String methodName = columnSettings.get(i).getGetMethodName();
|
|
|
Method method = this.getDataClass().getMethod(methodName, new Class[]{});
|
|
|
Object value = method.invoke(obj, new Object[]{});
|
|
@@ -160,4 +208,62 @@ public class ExcelWriter extends ExcelExport {
|
|
|
public XSSFWorkbook getWorkbook() {
|
|
|
return workbook;
|
|
|
}
|
|
|
+
|
|
|
+ private XSSFCellStyle getCellStyle(XSSFCellStyle style, Field field) {
|
|
|
+ ExcelDataStyle excelDataStyle = field.getAnnotation(ExcelDataStyle.class);
|
|
|
+ if (Objects.isNull(excelDataStyle)) {
|
|
|
+ return style;
|
|
|
+ } else {
|
|
|
+ // 文字位置
|
|
|
+ style.setAlignment(excelDataStyle.horizontal());
|
|
|
+ style.setVerticalAlignment(excelDataStyle.vertical());
|
|
|
+
|
|
|
+ //边框
|
|
|
+ int borderRed = excelDataStyle.borderColorRed();
|
|
|
+ int borderGreen = excelDataStyle.borderColorGreen();
|
|
|
+ int borderBlue = excelDataStyle.borderColorBlue();
|
|
|
+ XSSFColor borderColor = new XSSFColor(new Color(borderRed, borderGreen, borderBlue));
|
|
|
+ // 上边框
|
|
|
+ if (excelDataStyle.borderTop().getCode() > 0) {
|
|
|
+ style.setBorderTop(excelDataStyle.borderTop());
|
|
|
+ style.setTopBorderColor(borderColor);
|
|
|
+ }
|
|
|
+ // 下边框
|
|
|
+ if (excelDataStyle.borderBottom().getCode() > 0) {
|
|
|
+ style.setBorderBottom(excelDataStyle.borderBottom());
|
|
|
+ style.setBottomBorderColor(borderColor);
|
|
|
+ }
|
|
|
+ // 左边框
|
|
|
+ if (excelDataStyle.borderLeft().getCode() > 0) {
|
|
|
+ style.setBorderLeft(excelDataStyle.borderLeft());
|
|
|
+ style.setLeftBorderColor(borderColor);
|
|
|
+ }
|
|
|
+ // 右边框
|
|
|
+ if (excelDataStyle.borderRight().getCode() > 0) {
|
|
|
+ style.setBorderRight(excelDataStyle.borderRight());
|
|
|
+ style.setRightBorderColor(borderColor);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 背景
|
|
|
+ style.setFillPattern(excelDataStyle.fillPartner());
|
|
|
+ XSSFColor fillColor = new XSSFColor(new Color(excelDataStyle.fillColorRed(), excelDataStyle.fillColorGreen(), excelDataStyle.fillColorBlue()));
|
|
|
+ style.setFillForegroundColor(fillColor);
|
|
|
+
|
|
|
+ // 字体
|
|
|
+ Font font = workbook.createFont();
|
|
|
+ font.setFontHeightInPoints(excelDataStyle.fontSize());
|
|
|
+ font.setFontName(excelDataStyle.fontName());
|
|
|
+ font.setColor(excelDataStyle.fontColor().getIndex());
|
|
|
+ style.setFont(font);
|
|
|
+
|
|
|
+ // 锁定
|
|
|
+ if (excelDataStyle.lock()) {
|
|
|
+ style.setLocked(true);
|
|
|
+ sheet.protectSheet(SystemConstant.EXCEL_PROTECT_KEY);
|
|
|
+ } else {
|
|
|
+ style.setLocked(false);
|
|
|
+ }
|
|
|
+ return style;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|