vue前台获取后台数据并导出excel

前台代码

 <el-button type="primary" @click="exportExcel">下载<i class="el-icon-download el-icon--right" ></i></el-button>

exportExcel() {
      // this.$refs.exportExcel.dispatchEvent(new MouseEvent('click'))
      // console.log(this.$refs.exportExcel.dispatchEvent(new MouseEvent('click')))
      downloadAuditValue(Object.assign({
        province_code: this.tenant_id
      })).then(res => {
        console.log(res)
        const blob = new Blob([res], { type: 'application/ms-excel;charset=utf-8' })
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = '稽核价值.xlsx' // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放掉blob对象
      })
    }

后台代码

pom依赖

		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

controller

@RequestMapping("downloadExcel")
    public void exprotAuditValue(@RequestBody Map<String, Object> params, HttpServletResponse response) {
        try {
            List<AuditValue> auditValues = AuditValueService.downloadExcel(params);
            String[] titleRow = new String[]{"省份编码", "省份", "稽核指标ID", "稽核指标名称",
                    "样本量", "发现异常记录", "已整改记录", "整改中记录", "无需整改记录",
                    "差错条数", "差错金额", "差错率", "准确率"};
            List<String[]> data = new LinkedList<String[]>();
            data.add(0, titleRow);
            for (AuditValue auditValue : auditValues) {
                data.add(new String[]{
                        auditValue.getProvince_code(),
                        auditValue.getProvince_name(),
                        auditValue.getQuota_id(),
                        auditValue.getQuota_name(),
                        auditValue.getSample_num(),
                        String.valueOf(auditValue.getAbnormal_record()),
                        auditValue.getModified_record(),
                        auditValue.getModifying_record(),
                        auditValue.getNo_record(),
                        String.valueOf(auditValue.getError_num()),
                        auditValue.getError_fee(),
                        auditValue.getError_rate(),
                        auditValue.getAccuracy()
                });
            }
            Map<String, List<String[]>> exprotData = new HashMap<>();
            exprotData.put("稽核价值总视图", data);
            String res = FileUtils.createExceleFile(response, exprotData);
        } catch (Exception e) {
            logger.error(" Errors caused by downloading : {} ", e.toString());
        }
    }

FileUtils

public class FileUtils {
    private static Logger logger =  LoggerFactory.getLogger(FileUtils.class);
    public static String createExceleFile(HttpServletResponse response, Map<String, List<String[]>> exportData) {
        OutputStream outputStream = null;
        try {
            Workbook wb = new HSSFWorkbook();

            for (String sheetName : exportData.keySet()) {
                Sheet sheet = wb.createSheet(sheetName);
                List<String[]> rowData = exportData.get(sheetName);
                for (int i = 0; i < rowData.size(); i++) {
                    String[] cellData = rowData.get(i);
                    Row row = sheet.createRow(i);
                    for (int j = 0; j < cellData.length; j++) {
                        Cell cell = row.createCell(j);
                        cell.setCellValue(cellData[j]);
                    }
                }
            }
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.flushBuffer();
            outputStream = response.getOutputStream();
            wb.write(outputStream);
        } catch (Exception e) {
            logger.error("FileUtils failure : {}", e.toString());
            return "failure";
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (Exception e) {
                logger.error("FileUtils close xxxStream: {}", e.toString());
            }
        }
        return "success";
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值