com.dongyue.util.parse.ExportIrregularExcelUtil Maven / Gradle / Ivy
The newest version!
package com.dongyue.util.parse;
import com.dongyue.util.anno.*;
import com.dongyue.util.common.ExportUtil;
import com.dongyue.util.model.CellValue;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 导出异形表
*/
public class ExportIrregularExcelUtil {
public static void export(T t, OutputStream outputStream) {
Class aClass = (Class) t.getClass();
IrregularExcel annotation = aClass.getAnnotation(IrregularExcel.class);
String sheetName;
if (annotation == null) {
sheetName = "sheetName";
} else {
sheetName = annotation.sheetName();
}
//第一步:创建一个webbook,对应一个excel
HSSFWorkbook wb = new HSSFWorkbook();
//第二步:在webbook添加一个sheet,与之对应的是excle中的excel
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow(0);
//设置默认样式
HSSFCellStyle cellStyle = ExportUtil.setDefaultStyle(wb);
int sheetMaxRow = 0;
int sheetMaxColumn = 0;
Field[] fields = aClass.getDeclaredFields();
List list = new ArrayList<>();
for (Field field : fields) {
IrregularFieldNameAnno fileName = field.getAnnotation(IrregularFieldNameAnno.class);
if (fileName != null) {
CellValue cellValue = ExportUtil.createCellValue(field, fileName.row(), fileName.cloumn(),
fileName.title(), fileName.colspan(), fileName.rowspan(),null,wb);
sheetMaxColumn = Math.max(fileName.colspan() + fileName.cloumn(), sheetMaxColumn);
sheetMaxRow = Math.max(fileName.row(), sheetMaxRow);
list.add(cellValue);
}
IrregularFieldAnno fieldAnno = field.getAnnotation(IrregularFieldAnno.class);
if (fieldAnno != null) {
try {
field.setAccessible(Boolean.TRUE);
CellValue cellValue1 = ExportUtil.createCellValue(field, fieldAnno.row(), fieldAnno.cloumn()
, field.get(t).toString(), fieldAnno.colspan(), fieldAnno.rowspan(),null, wb);
sheetMaxColumn = Math.max(fieldAnno.colspan() + fieldAnno.cloumn(), sheetMaxColumn);
sheetMaxRow = Math.max(fieldAnno.row(), sheetMaxRow);
list.add(cellValue1);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
IrregularItem irregularItem = field.getAnnotation(IrregularItem.class);
if (irregularItem != null) {
//获取f的类
Class> fieldClazz = field.getType();
//判断是否为基本类型
if (fieldClazz.isPrimitive()) {
continue;
}
//判断fc是否和List相同或者其父类
if (fieldClazz.isAssignableFrom(List.class)) {
//如果是List类型,得到其Generic的类型
Type fc = field.getGenericType();
if (fc instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) fc;
//得到泛型里的class类型对象。
Class listItem = (Class) pt.getActualTypeArguments()[0];
//获取最大的列
Integer maxColumn = 0;
Map cloumnFeild = new HashMap<>();
Field[] declaredFields = listItem.getDeclaredFields();
for (Field declaredField : declaredFields) {
StandardExcelAttr standardExcelAttr = declaredField.getAnnotation(StandardExcelAttr.class);
//导出列表
int i = standardExcelAttr.excelColumn();
maxColumn = Math.max(i + 1, maxColumn);
cloumnFeild.put(i, declaredField.getName());
}
sheetMaxColumn = Math.max(maxColumn, sheetMaxColumn);
try {
field.setAccessible(true);
List list1 = (List) field.get(t);
Integer listRow = irregularItem.startRow();
boolean nullShowTitle = irregularItem.nullShowTitle();
if (list1 != null && list1.isEmpty()) {
if (nullShowTitle) {
//设置标题
listRow += ExportUtil.title(listRow, irregularItem.startRow(), maxColumn, cloumnFeild, listItem, list,wb);
}
} else {
for (Object item : list1) {
listRow += ExportUtil.title(listRow, irregularItem.startRow(), maxColumn, cloumnFeild, listItem, list,wb);
//判断item是否有List 有的话需要先打印list
listRow += ExportUtil.value(item, maxColumn, cloumnFeild, listItem, listRow, list,wb);
}
}
sheetMaxRow = Math.max(listRow, sheetMaxRow);
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
}
}
}
}
ExportUtil.border(sheet, row, cellStyle, sheetMaxColumn, sheetMaxRow);
//HSSFCell对应的是每一行的数据插入
for (CellValue cellValue : list) {
ExportUtil.setCellValue(sheet, cellValue, cellStyle);
}
// 第六步,将文件存到指定位置
try {
wb.write(outputStream);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}