java导出csv

xiaoxiao2021-02-27  325

导出数据 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(); } }
转载请注明原文地址: https://www.6miu.com/read-5688.html

最新回复(0)