java easyExcel动态导出字段
需求根据实体+动态字段导出数据
思路
1 把实体转成JSON作为接收数据的值
2 把查询的数据转成jsonobject 通过上面的值对应放入List
代码实现
- 引入包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
- 实体
public class ExportEntity {
private String officeId;
private String taskId;
private String extend;
public String getExtend() {
return extend;
}
public void setExtend(String extend) {
this.extend = extend;
}
public String getOfficeId() {
return officeId;
}
public void setOfficeId(String officeId) {
this.officeId = officeId;
}
public String getTaskId() {
return taskId;
}
public void setTaskId(String taskId) {
this.taskId = taskId;
}
}
- 实现
public class Export{
public void exportExcel() throws JSONException {
String json = "{\"序号\":\"1\",\"编号\":\"0001\"}";
String filedNames = "组织id,任务id";
String filedCodes = "officeId,taskId";
List<ExportEntity> callRecordEntities = getExportEntity();
List<JSONObject> exportCallRecord = Lists.newArrayList();
Map<String, String> titleMap = new TreeMap();
Boolean flag = true;
for(ExportEntity entity:callRecordEntities){
JSONObject jsonObject = new JSONObject(JsonUtils.objectToJson(entity));
if(flag){
entity.setExtend("{\"序号\":\"1\",\"编号\":\"0001\"}");
flag =false;
}else {
entity.setExtend("{\"编号\":\"0001\",\"上上迁\":\"666\"}");
flag = true;
}
JSONObject hand = new JSONObject(entity.getExtend());
Iterator<String> sIterator = hand.keys();
while(sIterator.hasNext()){
String key = sIterator.next();
jsonObject.put(ReflectUtils.getPinyin(key),hand.getString(key));
titleMap.put(ReflectUtils.getPinyin(key),key);
}
exportCallRecord.add(jsonObject);
log.info("OBJECT : " + jsonObject);
}
for (Map.Entry<String, String> entry : titleMap.entrySet()) {
filedNames=filedNames+","+ entry.getValue();
filedCodes=filedCodes+","+ entry.getKey();
}
String[] head = filedNames.split(",");
List<String> headList = new ArrayList<>(Arrays.asList(head));
File file1 = new File("f:/data");
if (!file1.exists()) {
file1.mkdir();
}
String[] file = filedCodes.split(",");
List<String> fileList = new ArrayList<>(Arrays.asList(file));
ExcelWriter excelWriter = null;
excelWriter = EasyExcel.write(file1 + "/" + new Date().getTime()+".xls").build();
WriteSheet writeSheet0 = EasyExcelUtils.noModelWrite("测试例子", 0, headList);
WriteSheet writeSheet1 = EasyExcelUtils.noModelWrite("测试例子1", 1, headList);
excelWriter.write(EasyExcelUtils.dataListJson(exportCallRecord, fileList), writeSheet0);
excelWriter.write(EasyExcelUtils.dataListJson(exportCallRecord, fileList), writeSheet1);
excelWriter.finish();
}
private List<ExportEntity> getExportEntity() {
List<ExportEntity> list = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
ExportEntity exportEntity = new ExportEntity();
exportEntity.setOfficeId("officeId"+i);
exportEntity.setTaskId("taskId"+i);
list.add(exportEntity);
}
return list;
}
}
- utils
public class EasyExcelUtils {
public static WriteSheet noModelWrite( String sheetName,Integer sheetNo, List<String> headList) {
return EasyExcel.writerSheet(sheetNo, sheetName).head(head(headList)).registerWriteHandler(new CustomCellWriteHandler()).build();
}
public static List<List<Object>> dataListJson(List listIn, List<String> fileList) {
List<JSONObject> dataList = listIn;
List<List<Object>> list = new ArrayList<>();
try {
for (JSONObject jSONObject : dataList) {
List<Object> data = new ArrayList<>();
for (String fieldName : fileList) {
Object o = "";
try{
o = jSONObject.get(fieldName) == null ? "" : jSONObject.get(fieldName);
}catch (Exception e){
log.info("dynimicDateIsNull");
};
data.add(o);
}
list.add(data);
}
}catch (Exception e){
e.printStackTrace();
}
return list;
}
}
|