java导出csv




导出数据
public  boolean exportCsv(File file, List<String> dataList ,String checkUser ,BigCustomerOrderVo vo){
    boolean isSucess=false;

    FileOutputStream out=null;
    OutputStreamWriter osw=null;
    BufferedWriter bw=null;
    try {
        out = new FileOutputStream(file);
        osw = new OutputStreamWriter(out);
        bw =new BufferedWriter(osw);
        if(dataList!=null && !dataList.isEmpty()){
            for(String data : dataList){
                bw.append(data).append("\r");
            }
        }
        isSucess=true;
    } catch (Exception e) {
        isSucess=false;
    }finally{
        if(bw!=null){
            try {
                bw.close();
                bw=null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(osw!=null){
            try {
                osw.close();
                osw=null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(out!=null){
            try {
                out.close();
                out=null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    try {
        FlowLogPo flowLogPo = new FlowLogPo("project","",checkUser,"大客户订单导出操作", com.jd.payment.paycommon.utils.GsonUtils.toJson(vo));
        flowLogDao.insertFlowLog(flowLogPo);
    } catch (Exception e) {
        logger.error("大客户订单导出操作,插入日志流水表异常",e);
    }
    return isSucess;
}



输出并下载
    public static void outputNewExcel (HttpServletRequest req, HttpServletResponse response, String filename, String path) {
        response.setHeader ("Content-Transfer-Encoding", "binary");
        response.setHeader ("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader ("Pragma", "public");
        response.setHeader ("Pragma", "no-cache"); // 禁止数据缓存。
        response.setDateHeader ("Expires", 0);
        response.setContentType ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");// 设置响应的类型格式为电子表格格式
        response.setHeader ("Content-disposition", "attachment;filename="
                + filename);

        try {
            ServletOutputStream sos = response.getOutputStream ();
            File f = new File (path + filename);
            FileInputStream fi = new FileInputStream (f);
//            byte[] buf = new byte[(int) f.length ()];
//            fi.read(buf, 0, (int) f.length());
//            sos.write (buf);
            byte[] buf = new byte[1024*4];
            int sizetag =0;
            while ((sizetag= fi.read (buf, 0, 1024*4))!=-1){
                sos.write (buf,0,sizetag);
            }
            sos.flush();
            fi.close();
            sos.close();
        } catch (Exception e) {
            e.printStackTrace ();
        } finally {
            File f = new File (path + filename);
            if (f.exists ()) {
                f.delete ();
            }
            System.gc();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值