//获取选择的需要导出的字段
getCheckedCol(value) {
var object = {}
//查询条件
object.params = this.queryParams
//table里的value和label List对象
object.tableData = this.checkedResultCol
//选择的ids
object.ids = this.guids
test(object).then(res => {
resolveBlob(res,"12312312.xlsx")
})
},
/**
* 解析blob响应内容并下载
* @param {*} res 返回值
* @param {String} 文件名 filename.xlsx
*/
export function resolveBlob(res, fileName) {
const aLink = document.createElement('a')
var blob = new Blob([res])
// //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
aLink.style.display = 'none'
aLink.href = URL.createObjectURL(blob)
aLink.setAttribute('download', decodeURI(fileName)) // 设置下载文件名称
document.body.appendChild(aLink)
aLink.click()
URL.revokeObjectURL(aLink.href)//清除引用
document.body.removeChild(aLink)
}
{
"params": {
"pageNum": 1,
"pageSize": 10,
"style": "jy",
"statusList": [],
"zbUnderUnit": "",
"answerType": "",
"answerWay": "",
"xbUnderUnit": ""
},
"tableData": [
{
"label": "建议编号",
"prop": "proposalNum"
},
{
"label": "代表姓名",
"prop": "joinPeopleNum"
},
{
"label": "代表团",
"prop": "daibiaotuan"
},
{
"label": "建议标题",
"prop": "title"
},
{
"label": "提交类别",
"prop": "tianDate"
},
{
"label": "承办单位",
"prop": "zbUnderUnit"
},
{
"label": "承办类型",
"prop": "underTakeType"
},
{
"label": "建议类别",
"prop": "tianClass"
},
{
"label": "当前状态",
"prop": "status"
},
{
"label": "办理类型",
"prop": "undertakerList[0].answerType"
},
{
"label": "答复内容能否发布",
"prop": "publishJg"
}
],
"ids": [
"87c06c17d68046d87e908",
"16257911777665",
"b0d515eeb9a370ddb8fe7bea",
"162541105",
"bbb2741750b686f3d58",
"573edc1f86dd4082842dd",
"ce8b3b1b5177d0f4df37b99fc",
"74c4f10d04f891d6ce61b3e",
"62a9b2185aad388e1a64fc"
]
}
/**
* 导出选择的字段
*/
@PostMapping("/exportExcelCol")
public void exportExcelCol(@RequestBody ExcelParams excelParams, HttpServletResponse response) {
//查询条件
JSONObject params = excelParams.getParams();
JyProposal jyProposal = JSONUtil.toBean(params, JyProposal.class);
//ids
String[] ids = excelParams.getIds();
//表头
List<tableHeader> tableData = excelParams.getTableData();
List<String> labelList = tableData.stream().map(tableHeader::getLabel).collect(Collectors.toList());
List<String> propsList = tableData.stream().peek(d -> d.setProp(upperCharToUnderLine(d.getProp()))).map(tableHeader::getProp).collect(Collectors.toList());
List<List<String>> headers = new LinkedList<>();
labelList.add("备注");
for (String name : labelList) {
List<String> field = new ArrayList<>();
field.add(name);
headers.add(field);
}
//条件处理
List<Map<String, Object>> datas = iJyProposalService.selectListByParamsOrIds(jyProposal, Arrays.asList(ids));
//数据拼接
List<List<Object>> resultData =ExcelExportCol.getData(datas, propsList);
ExcelExportCol.exportExcelChooseCol(resultData, headers, "文件导出.xlsx", response);
}
实体类
@Data
public class ExcelParams {
/**
* 查询的字段 可能需要转小写并且下换线
*/
private JSONObject params;
/**
* 多选按钮的 id 集合
*/
private String [] ids;
/**
* 选择的导出表头
*/
private List<tableHeader> tableData;
}
/**
* 自定义表头的excel 导出
* @param list 数据字段
* @param headers 表头字段
* @param fileName 文件名
* @param response 文件流
*/
public static void exportExcelChooseCol(List<List<Object>> list, List<List<String>> headers, String fileName, HttpServletResponse response) {
try {
resetResponse(fileName, response);
ServletOutputStream os = response.getOutputStream();
EasyExcel.write(os)
.head(headers)
.autoCloseStream(false)
// 自动适配
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.sheet("sheet1").doWrite(list);
} catch (IOException e) {
throw new RuntimeException("导出Excel异常");
}
}
/**
* 构建excel 数据信息
* @param allDataByTableName 表数据
* @param list1 表头
* @return
*/
public static List<List<Object>> getData(List<Map<String, Object>> allDataByTableName, List<String> list1) {
List<List<Object>> result = new ArrayList(allDataByTableName.size());
for (int i = 0; i < allDataByTableName.size(); i++) {
Map<String, Object> temp = allDataByTableName.get(i);
List<Object> list = new ArrayList<>();
for (String s : list1) {
Object o = temp.get(s);
list.add(o);
}
result.add(list);
}
return result;
}