@@ -91,7 +91,7 @@ public abstract class ExcelExportBase extends ExportBase { | |||||
for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) { | ||||
entity = excelParams.get(k); | entity = excelParams.get(k); | ||||
if (entity.getList() != null) { | if (entity.getList() != null) { | ||||
Collection<?> list = (Collection<?>) entity.getMethod().invoke(t, new Object[] {}); | |||||
Collection<?> list = getListCellValue(entity,t); | |||||
int listC = 0; | int listC = 0; | ||||
for (Object obj : list) { | for (Object obj : list) { | ||||
createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), | createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), | ||||
@@ -6,6 +6,7 @@ import java.lang.reflect.ParameterizedType; | |||||
import java.text.SimpleDateFormat; | import java.text.SimpleDateFormat; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import java.util.Collection; | |||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.Date; | import java.util.Date; | ||||
import java.util.List; | import java.util.List; | ||||
@@ -176,6 +177,23 @@ public class ExportBase { | |||||
return value == null ? "" : value.toString(); | return value == null ? "" : value.toString(); | ||||
} | } | ||||
/** | |||||
* 获取集合的值 | |||||
* @param entity | |||||
* @param obj | |||||
* @return | |||||
* @throws Exception | |||||
*/ | |||||
public Collection<?> getListCellValue(ExcelExportEntity entity, Object obj) throws Exception { | |||||
Object value; | |||||
if (obj instanceof Map) { | |||||
value = ((Map<?, ?>) obj).get(entity.getKey()); | |||||
} else { | |||||
value = (Collection<?>) entity.getMethod().invoke(obj, new Object[] {}); | |||||
} | |||||
return (Collection<?>) value; | |||||
} | |||||
/** | /** | ||||
* 注解到导出对象的转换 | * 注解到导出对象的转换 | ||||
* | * | ||||
@@ -21,19 +21,31 @@ public class ExcelExportForMap { | |||||
/** | /** | ||||
* Map 测试 | * Map 测试 | ||||
*/ | */ | ||||
// @Test | |||||
@Test | |||||
public void test() { | public void test() { | ||||
try { | try { | ||||
List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>(); | List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>(); | ||||
entity.add(new ExcelExportEntity("姓名", "name")); | entity.add(new ExcelExportEntity("姓名", "name")); | ||||
entity.add(new ExcelExportEntity("性别", "sex")); | entity.add(new ExcelExportEntity("性别", "sex")); | ||||
ExcelExportEntity excelentity = new ExcelExportEntity("学生", "students"); | |||||
List<ExcelExportEntity> temp = new ArrayList<ExcelExportEntity>(); | |||||
temp.add(new ExcelExportEntity("姓名", "name")); | |||||
temp.add(new ExcelExportEntity("性别", "sex")); | |||||
excelentity.setList(temp); | |||||
entity.add(excelentity); | |||||
List<Map<String, String>> list = new ArrayList<Map<String, String>>(); | |||||
Map<String, String> map; | |||||
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); | |||||
Map<String, Object> map; | |||||
for (int i = 0; i < 10; i++) { | for (int i = 0; i < 10; i++) { | ||||
map = new HashMap<String, String>(); | |||||
map = new HashMap<String, Object>(); | |||||
map.put("name", "1" + i); | map.put("name", "1" + i); | ||||
map.put("sex", "2" + i); | map.put("sex", "2" + i); | ||||
List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>(); | |||||
tempList.add(map); | |||||
tempList.add(map); | |||||
map.put("students", tempList); | |||||
list.add(map); | list.add(map); | ||||
} | } | ||||
@@ -49,7 +61,7 @@ public class ExcelExportForMap { | |||||
} | } | ||||
} | } | ||||
@Test | |||||
//@Test | |||||
public void testMany() { | public void testMany() { | ||||
try { | try { | ||||
List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>(); | List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>(); | ||||