JAVA导出数据保存为CSV文件

1.html文件:

  <form action="/prospect/downloadPhone" method="post" >
                             <div class="alert alert-warning col-lg-5" role="alert" id="alertSelect" style="display: none"><span id="dataText" style="line-height: 34px;"></span>
                                 <button type="submit" class="btn btn-info" id="download" style="float: right">导出</button>
                             </div>
                         </form>

2.controller层

 @RequestMapping(value = "/downloadPhone",method = RequestMethod.POST)
    public void  downloadPhone(
            @RequestParam(value = "type", required = false, defaultValue = "text/plain") String type,
            HttpServletRequest request,
            HttpServletResponse response){
        try {
        	//取出session中村的手机号,或者直接数据库中查询
            List<String> phoneList=(List<String>) request.getSession().getAttribute("phoneList");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
            String csvFilePath = "/" +sdf.format(new Date())+".csv";
            //文件名
            String fileName = new ImportController().getFileName(csvFilePath,request);
            //文件内容
            String text = "";
            if(phoneList!=null && !phoneList.isEmpty()){
                for(String data : phoneList){
                    text+=data+"\r\n";
                }
            }
            OutputStream os = response.getOutputStream();
            BufferedOutputStream buff = new BufferedOutputStream(os);
            response.setHeader("Content-Type", type);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            buff.write(text.getBytes("UTF-8"));
            buff.flush();
            buff.close();
            os.close();
        }catch (Exception e){
            log.error("导出短信群发手机号:{}",e.getMessage());
        }
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值