根据一些条件查询表得到的数据如何写入excel表格。(后端写入.csv临时文件)

博客介绍了表格导出为Excel的实现方法,在Controller层和ServiceImpl层进行相关定义,前端通过另存为操作将表格保存为Excel文件,涉及前后端配合完成表格导出功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Controller层,定义如下

@ApiOperation(value = "条件导出门店表格信息",tags="管理端-活跃门店")
@RequestMapping(value = "/exportStoreTableResult", method = RequestMethod.GET)
public void exportStoresInfo(HttpServletResponse getResponse, @ModelAttribute("StoreGetEntity") StoreGetEntity storeGetEntity) throws IOException, CommonException {
    storeActiveService.exportStoreTableByConditions(getResponse,storeGetEntity);
}

在ServiceImpl层,定义如下

    /**
     * @author weiguyu
     * 表格文件导出
     */
    @Override
    public void exportStoreTableByConditions(HttpServletResponse getResponse, StoreGetEntity storeGetEntity) throws IOException, CommonException {
        //新建RetEntity对象,这里怎么查询出来省略,主要是如何写入临时文件
        List<Store> stores = storeService.searchForStore(storeGetEntity).getRecords();
        //判断查询结果
        if(stores!=null){
            // 写入临时文件
            File tempFile = File.createTempFile("vehicle", ".csv");
            CsvWriter csvWriter = new CsvWriter(tempFile.getCanonicalPath(), ',', Charset.forName("UTF-8"));
            // 写表头
            long s = System.currentTimeMillis();
            System.err.println();
            String[] headers = { "门店", "活跃度", "联系人", "联系电话", "标签", "运营人员", "运营商", "销售人员", "创建时间" };
            csvWriter.writeRecord(headers);
            for (Store cuo : stores) {
                csvWriter.write(cuo.getName());
                csvWriter.write(cuo.getSleepDay()+"天未活跃");
                csvWriter.write(cuo.getManagerName());
                csvWriter.write(cuo.getManagerMobile());
                csvWriter.write(cuo.getTag());
                csvWriter.write(cuo.getAgentUserName());
                csvWriter.write(cuo.getAgentName());
                csvWriter.write(cuo.getCcyAdminUserName());
                csvWriter.write(cuo.getDateAdded());
                csvWriter.endRecord();
            }
            csvWriter.close();
            long e = System.currentTimeMillis();

            System.err.println(e - s);

            /**
             * 写入csv结束,写出流
             */
            java.io.OutputStream out = getResponse.getOutputStream();
            byte[] b = new byte[10240];
            java.io.File fileLoad = new java.io.File(tempFile.getCanonicalPath());
            getResponse.reset();
            getResponse.setContentType("application/csv");
            getResponse.setHeader("content-disposition", "attachment; filename=vehicleModel.csv");
            long fileLength = fileLoad.length();
            String length1 = String.valueOf(fileLength);
            getResponse.setHeader("Content_Length", length1);
            java.io.FileInputStream in = new java.io.FileInputStream(fileLoad);
            int n;
            while ((n = in.read(b)) != -1) {
                out.write(b, 0, n); // 每次写入out1024字节
            }
            in.close();
            out.close();
            tempFile.delete();
         }else{
            throw new CommonException(CommonConstant.RESULT_CODE_EXPECTATION_FAILED,"没有找到数据");
         }
     }
}

前端另存为excel表格就行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值