导出excel工具类
This commit is contained in:
parent
6ee8b5f044
commit
887ec2084a
|
|
@ -1,7 +1,9 @@
|
|||
package com.engine.salary.util.excel;
|
||||
|
||||
import com.engine.salary.entity.taxdeclaration.dto.TaxDeclarationLaborListDTO;
|
||||
import com.engine.salary.util.SalaryDateUtil;
|
||||
import com.engine.salary.util.SalaryI18nUtil;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.poi.ss.usermodel.CellType;
|
||||
import org.apache.poi.ss.usermodel.FillPatternType;
|
||||
|
|
@ -10,7 +12,12 @@ import org.apache.poi.ss.usermodel.IndexedColors;
|
|||
import org.apache.poi.xssf.usermodel.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -69,6 +76,41 @@ public class ExcelUtil {
|
|||
return workbook;
|
||||
}
|
||||
|
||||
public static <T> XSSFWorkbook genWorkbook(String sheetName, List<T> rowList) {
|
||||
List<Object> headerList = Lists.newArrayList();
|
||||
List<String> dataIndexList = Lists.newArrayList();
|
||||
// 解析表头
|
||||
ExcelUtil.parseHeader(TaxDeclarationLaborListDTO.class, headerList, dataIndexList);
|
||||
|
||||
List<List<Object>> rows = new ArrayList<>();
|
||||
rows.add(headerList);
|
||||
for (int i = 0; i < rowList.size(); i++) {
|
||||
List<Object> row = Lists.newArrayListWithExpectedSize(dataIndexList.size());
|
||||
for (int j = 0; j < dataIndexList.size(); j++) {
|
||||
Object value = getValue(rowList.get(i), dataIndexList.get(j));
|
||||
row.add(value);
|
||||
}
|
||||
rows.add(row);
|
||||
}
|
||||
return genWorkbookV2(rows, sheetName);
|
||||
}
|
||||
|
||||
private static <T> Object getValue(T t, String fieldName) {
|
||||
Object value = null;
|
||||
try {
|
||||
BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
|
||||
PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
|
||||
for (PropertyDescriptor property : props) {
|
||||
if (fieldName.equals(property.getName())) {
|
||||
Method method = property.getReadMethod();
|
||||
value = method.invoke(t, new Object[]{});
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public static XSSFWorkbook genWorkbookV2(List<List<Object>> rowList, String sheetName) {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
|
|
|
|||
Loading…
Reference in New Issue