一 前言
接到一个需求,要求可以自定义导出字段,翻看了大量资料,最终集齐众家所长做了出来,也对excel导出有了不少了解
二 前后端代码
2.1 Vue页面
{{ct.label}}
导出已选择的列
导出全部列
导出
exportDatChecked(){
let columnTitleNew=[]
this.columnTitle.filter(item =>item.checked).forEach(item => {
columnTitleNew.push(item)
})
console.log(columnTitleNew)
this.queryParams.fieldsName= JSON.stringify(columnTitleNew)
console.log(this.queryParams.fieldsName)
this.download('/smms/server-smms/ResourceReserve/exportResearchTasks', {
...this.queryParams
}, `研培任务详情导出${new Date().getTime()}.xls`).then(res => {
if(res.code == '200'){
this.$message.success('导出成功')
}else {
this.$message.error("导出失败")
}
this.queryParams.fieldsName=null
})
this.queryParams.fieldsName=null
},
exportDataAll(){
let columnTitleNew=[]
this.columnTitle.forEach(item => {
columnTitleNew.push(item)
})
this.queryParams.fieldsName= JSON.stringify(columnTitleNew)
console.log(this.queryParams.fieldsName)
this.download('/smms/server-smms/ResourceReserve/exportResearchTasks', {
...this.queryParams
}, `研培任务详情导出${new Date().getTime()}.xls`).then(res => {
if(res.code == '200'){
this.$message.success('导出成功')
}else {
this.$message.error("导出失败")
}
this.queryParams.fieldsName=null
})
this.queryParams.fieldsName=null
},
columnTitle:[
{'label':'任务名称','checked':false,'prop':'name'},
{'label':'任务内容','checked':false,'prop':'taskContent'},
{'label':'责任部门','checked':false,'prop':'dutyDept'},
{'label':'负责人','checked':false,'prop':'responsiblePerson'},
{'label':'上周工作完成情况','checked':false,'prop':'nextWeek'},
{'label':'本周工作计划','checked':false,'prop':'thisWeek'},
{'label':'目标值','checked':false,'prop':'planSchedule'},
{'label':'时序进度','checked':false,'prop':'timingSchedule'},
{'label':'偏差','checked':false,'prop':'deviation'},
{'label':'偏差说明','checked':false,'prop':'remark'},
{'label':'周','checked':false,'prop':'week'},
{'label':'更新时间','checked':false,'prop':'updateTime'},
],
这里我用了element-UI的弹出层,主要是想尝试一下不同的写法,个人觉得应该还可以用dialog写,然后就是这里标题总数是写死了的,不利于后期维护,有想法的可以通过别的方法得到值,不用将它写死。
2.2 java后端
导出工具类
package com.hckj.utils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class ExportExcel {
// 导出表的标题
private String title;
// 导出表的列名
private String[] rowName;
// 导出表的数据
private List
转换工具类
package com.hckj.utils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Json转换类
*/
public class JsonUtil {
// 使用jackson进行json转换
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static JavaType getCollectionType(Class collectionClass, Class... elementClasses) {
return OBJECT_MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 将对象转换成json字符串。
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json结果集转化为对象
*
* @param jsonData json数据
* @param beanType 对象中的object类型
*/
public static T jsonToPojo(String jsonData, Class beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
*/
public static List jsonToList(String jsonData, Class beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String listToJson(List list) throws JsonParseException, JsonMappingException, IOException{
String comment_json = OBJECT_MAPPER.writeValueAsString(list);
return comment_json;
}
public static String mapToJson(Map map) throws JsonParseException, JsonMappingException, IOException{
String comment_json = OBJECT_MAPPER.writeValueAsString(map);
return comment_json;
}
public static Map jsonStringToMap(String jsonString) throws Exception{
Map map = OBJECT_MAPPER.readValue(jsonString, Map.class);
return map;
}
public static String listToJson1(List