若依框架下自定义字段集合类报表的导出功能

1.前端查询条件显示:



<el-input
style=“width: 100px;”
type=“number”
v-model=“queryCondition[item.fieldName][0]”
placeholder=“最小值”
@change=“getMaxName(item.fieldName)”
>
-
<el-input
style=“width: 100px;”
type=“number”
v-model=“queryCondition[item.fieldName][1]”
placeholder=“最大值”
@change=“getMaxName(item.fieldName)”
>






<el-button v-if=“queryList.length” type=“primary” @click=“onSubmit”>查询
<el-button v-if=“queryList.length” type=“primary” @click=“exportExcel”>导出

2.前端添加方法,使用post请求,将自定义字段封装为对象进行传递
async exportExcel() {
// 最大值 最小值必须都填写
if (this.maxName && this.queryCondition[this.maxName] && this.queryCondition[this.maxName].length) {
if (this.queryCondition[this.maxName].length !== 2) {
this.KaTeX parse error: Expected 'EOF', got '}' at position 61: …eturn; }̲ } …http.post(“/report/exportExcel”, this.queryCondition);
if (res.code === 200) {
fileExport.name(res.msg);
} else {
this.$message.error(res.msg);
}
}

3.在后端Controller添加导出方法,根据接收到的对象信息,查询报表内容
@RequestMapping(“/exportExcel”)
public AjaxResult exportExcel(@RequestBody JSONObject jsonObject, HttpServletResponse response) throws IOException {
OutputStream ouputStream = null;
String fileName = “报表详情列表”;
try {
List<LinkedHashMap<Object, Object>> detailList = reportCenterService.getDetail(jsonObject);
if(CollectionUtil.isNotEmpty(detailList)){
try {
//获取报表详情的表头
LinkedHashMap<Object, Object> map = detailList.get(0);
List kyList = map.keySet().stream().collect(Collectors.toList());
fileName = UUID.randomUUID().toString() + “_报表详情列表.xlsx”;
response.setHeader(“Content-disposition”, “attachment; filename=”
+ new String(fileName.getBytes(“utf-8”), “ISO8859-1”));//设置下载的文件名
SXSSFWorkbook wb = new SXSSFWorkbook();
exportDetail(kyList, detailList, wb, 0);
String downloadPath = DihugeConfig.getDownloadPath() + fileName;
File desc = new File(downloadPath);
if (!desc.getParentFile().exists())
{
desc.getParentFile().mkdirs();
}
ouputStream = new FileOutputStream(downloadPath);
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
} catch (Exception e) {
ouputStream.close();
throw e;
}
}
} catch (Exception e) {
logger.debug(“报表详情导出异常” + e.getMessage());
}
return AjaxResult.success(fileName);
}

4.报表内容的自定义方法解析
private SXSSFWorkbook exportDetail(List kyList, List<LinkedHashMap<Object, Object>> detailList, SXSSFWorkbook workbook, int index) {
Sheet sheet = workbook.createSheet();
workbook.setSheetName(index, “报表详情列表”);

    // 生成一个样式
    CellStyle style1 = workbook.createCellStyle();
    // 设置背景色
    style1.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());
    style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);

    //设置边框
    style1.setBorderBottom(BorderStyle.THIN);
    style1.setBorderLeft(BorderStyle.THIN);
    style1.setBorderRight(BorderStyle.THIN);
    style1.setBorderTop(BorderStyle.THIN);
    style1.setAlignment(HorizontalAlignment.CENTER);

    // 设置字体
    Font font1 = workbook.createFont();
    font1.setFontName("仿宋_GB2312");
    font1.setColor(IndexedColors.OLIVE_GREEN.index);
    font1.setFontHeightInPoints((short) 14);
    // 把字体应用到当前的样式
    style1.setFont(font1);
    for(int i =0; i < kyList.size(); i++){
        sheet.setColumnWidth(i, 12000);
    }

    //生成一行
    Row row2 = sheet.createRow(0);
    Cell cell1;
    for (int k = 0; k < kyList.size(); k++) {
        cell1 = row2.createCell(k);
        cell1.setCellValue(String.valueOf(kyList.get(k)));
        cell1.setCellStyle(style1);
    }

    sheet = workbook.getSheetAt(index);
    // 生成一个样式
    CellStyle style3 = workbook.createCellStyle();

    //设置边框
    style3.setBorderBottom(BorderStyle.THIN);
    style3.setBorderLeft(BorderStyle.THIN);
    style3.setBorderRight(BorderStyle.THIN);
    style3.setBorderTop(BorderStyle.THIN);
    style3.setAlignment(HorizontalAlignment.CENTER);
    style3.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中

    Font font3 = workbook.createFont();
    font3.setFontName("仿宋_GB2312");
    font3.setColor(IndexedColors.BLACK.index);
    font3.setFontHeightInPoints((short) 10);
    font3.setBold(true);
    // 把字体应用到当前的样式
    style3.setFont(font3);
    style3.setWrapText(true);

    Row row3;
    Cell cell3;
    for (int i = 0; i < detailList.size(); i++) {
        LinkedHashMap<Object, Object> detailMap = detailList.get(i);
        row3 = sheet.createRow(i+1);

        for (int k = 0; k < kyList.size(); k++) {
            for (Object key : detailMap.keySet()) {
                if (String.valueOf(kyList.get(k)).equals(String.valueOf(key))) {
                    cell3 = row3.createCell(k);
                    if(ObjectUtil.isNotEmpty(detailMap.get(key))){
                        cell3.setCellValue(String.valueOf(detailMap.get(key)));
                    }else {
                        cell3.setCellValue("");
                    }
                    cell3.setCellStyle(style3);
                }
            }
        }
    }
    return workbook;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值