java-使用BufferedWriter离线下载csv/Excel文件,使用response在线下载csv/Excel文件

 离线下载:


	   @Scheduled(cron = "${dmp.task.download-task.cron}")
	    public void isRun() throws IOException {
	        if (null != isDownload && isDownload.equals("1")){
	            log.info("############开始调用离线下载程序!");
	            String excelOrCsv = dmpSysConfigService.getSegmentDownloadExcelOrCsv();
	            if ("1".equals(excelOrCsv)){
	                consoleInfo();
	            }else {
	                log.info("#########excelOrCsv:"+excelOrCsv+"csv下载模式");
	                consoleInfoCsv();
	            }
	        }else {
	            log.info("############不执行下载任务!");
	        }
	    }

	public File consoleInfoCsv()throws IOException {

        File csvFile = null;
        BufferedWriter csvWtriter = null;
            if (taskFlag) {//如果正在执行
                log.info("#######有正在执行的任务--退出,等待下次执行");
                return csvFile;//退出,等下下次执行
            } else {
                log.info("#######锁住程序");
                taskFlag = true;//锁住程序
            }

            DownLoadTaskEntity downLoadTask = new DownLoadTaskEntity();
            downLoadTask.setStatus(1);
            //查询所有待处理的任务
            List taskList = this.getAllDownLoadTask(downLoadTask);
            log.info("############待处理的条数为:"+taskList.size());

            if (taskList != null && taskList.size() > 0) {
                log.info("#########csv下载模式");
                String csvFilePath = downloadPath;
                log.info(">>>>>>>>>>离线下载任务 csvFilePath=" + csvFilePath);

                for (DownLoadTaskEntity task : taskList) {//循环处理任务
                    log.info(">>>>>>>>>>执行离线下载任务,taskId=" + task.getTaskId() + "start");
                    task.setStatus(2);//设置状态为正在执行
                    task.setUpdateTime(new Date());
                    int num = this.updateTask(task);
                    if (num > 0) {//状态锁定成功
                        //查询数据
                        String conditions = task.getConditions();
                        JSONObject json = new JSONObject();
                        json = json.fromObject(conditions);
                        JSONArray downCols = json.optJSONArray("downCols");//es下载列
                        JSONArray downHeaders = json.optJSONArray("downHeaders");//抬头
                        String expression = json.optString("expression") == "" ? null : json.optString("expression");//标签ID
                        String segmentId = json.optString("segmentId") == "" ? null : json.optString("segmentId");
                        Long userId = json.optLong("userId");
                        Long deptId = json.optLong("deptId");
                        String deptCode = json.optString("deptCode");
                        int type = json.optInt("type");
                        String keyWord = json.optString("keyWord") == "" ? null : json.optString("keyWord");
                        String productIdStr = json.optString("productIdStr") == "" ? null : json.optString("productIdStr");
                        String last = json.optString("last") == "" ? null : json.optString("last");
                        String start = json.optString("start") == "" ? null : json.optString("start");
                        String columnName = json.optString("columnName") == "" ? null : json.optString("columnName");

                        PersonalQueryEntity entity = new PersonalQueryEntity();
                        entity.setFields(JSONArray.toList(downCols, String.class, new JsonConfig()));
                        entity.setType(type);
                        entity.setSegmentId(segmentId);
                        entity.setExpression(expression);
                        entity.setUserId(userId);
                        entity.setKeyWord(keyWord);
                        entity.setProductIdStr(productIdStr);
                        entity.setLast(last);
                        entity.setStart(start);
                        entity.setDeptId(deptId);
                        entity.setDeptCode(deptCode);
                        entity.setColumnName(columnName);
                        if (!entity.getFields().contains("gid")){
                            entity.getFields().add("gid");

                        }

                        ESPage page = new ESPage();
                        try {
        
                            if (entity.getType() == 9) {// 姓名,资金账号,手机号,tag,fg,产品,行为事件
                                log.info("## query by Expression List");

                                page = personalService.queryByExpressionListNoEsFilter(entity);
                            }


                            String fileName =  task.getTaskName();
                            csvFile = new File(csvFilePath + File.separator + fileName + ".csv");
                            File parent = csvFile.getParentFile();
                            if (parent != null && !parent.exists()) {
                                parent.mkdirs();
                            }
                            csvFile.createNewFile();
                            if (page != null && page.getDatas() != null) {
                                //获取数据总数
                                long totalNum = page.getTotalCount();
                                int totalNumInt = (int) page.getTotalCount();
                                //如果下载总数超过限制,则不进行下载
                                int max = Integer.parseInt(dmpSysConfigService.segmentDownloadMax());
                                if (totalNumInt > max){
                                    log.info("#############客群下载总量为:"+totalNumInt+"超过最大限制:"+max+"条");
                                    task.setStatus(-1);//失败
                                    task.setMsg("数量超过最大限制");
                                    task.setTotalSize(totalNum);
                                    task.setProcessSize(totalNum);
                                    task.setUpdateTime(new Date());
                                    this.updateTask(task);
                                    csvWtriter.flush();
                                    csvWtriter.close();
                                    taskFlag = false;
                                    return null;
                                }

                                //每次下载数量
                                int downloadsize = Integer.parseInt(getDonloadSize());

                                //entity设置page,size (最大10000)
                                if (totalNumInt > downloadsize) {
                                    entity.setSize(downloadsize);
                                } else {
                                    entity.setSize((int) totalNum);
                                }
                                log.info("总数:"+totalNumInt+";  每次下载数量downloadsize:"+downloadsize);
                                int totalPage = totalNumInt / downloadsize + 1;
                                log.info("总页数:"+totalPage);
                                List headers = JSONArray.toList(downHeaders, String.class, new JsonConfig());
                                csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                                        csvFile), "gbk"), 1024);
                                // 写入文件头部
                                writeRow(headers, csvWtriter);
                                for (int k = 0; k < totalPage; k++) {
                                    
                                    if (dmpSysConfigService.getSegmentDownloadIsBreak()){
                                        //停止文件下载
                                        log.info("#############停止文件下载#######");
                                        task.setStatus(-1);//失败
                                        task.setMsg("中断文件下载");
                                        task.setTotalSize(totalNum);
                                        task.setProcessSize(totalNum);
                                        task.setUpdateTime(new Date());
                                        this.updateTask(task);
                                        csvWtriter.flush();
                                        csvWtriter.close();
                                        taskFlag = false;
                                        return null;
                                    }
                                    if (!entity.getFields().contains("gid")){
                                        entity.getFields().add("gid");

                                    }
                                    page = new ESPage();
                                    entity.setPage(k);
                                    if (entity.getType() == 9) {// 姓名,资金账号,手机号,tag,fg,产品,行为事件
                                        log.info("## query by Expression List");
                                        page = personalService.queryByExpressionListNoEsFilter (entity);
                                    }
                                    if (entity.getFields().contains("gid")) {
                                        boolean flag = entity.getFields().remove("gid");
                                        log.info("############下载列表删除gid列结果:" + flag);
                                    }
                                    if (page != null && page.getDatas() != null) {
                                        List> dataList = editCsvDataList(page.getDatas(), entity.getFields());
                                        // 写入文件内容
                                        for (List row : dataList) {
                                            writeRow(row, csvWtriter);
                                        }

                                    }

                                }
                                csvWtriter.flush();
                                csvWtriter.close();
                                log.info("############数据set完成");
                                task.setStatus(3);//完成
                                task.setMsg("文件已生成");
                                task.setUpdateTime(new Date());
                                task.setTotalSize(totalNum);
                                task.setProcessSize(totalNum);
                                task.setDownLoadPath(downloadIp+ "/dmp/downLoad/"+ task.getTaskName() + ".csv");
                                this.updateTask(task);
                            }
                        } catch (Exception e) {
                            csvWtriter.flush();
                            csvWtriter.close();
                            task.setStatus(-1);//失败
                            task.setMsg("文件生成失败");
                            task.setUpdateTime(new Date());
                            this.updateTask(task);
                            log.info("#############################离线下载文件异常:",e);


                        }
                    } else {
                        System.out.println("任务执行失败!MSQL更新失败!");
                        log.info(">>>>>>>>>>任务执行失败,taskId=" + task.getTaskId() + ",end");
                        continue;//继续下个任务执行
                    }
                    log.info(">>>>>>>>>>执行离线下载任务,taskId=" + task.getTaskId() + ",end");
                }
            }
            log.info("#######离线下载任务结束");
            //程序运行完后释放锁
            taskFlag = false;
        return csvFile;

    }


	//对数据进行整理
    private List> editCsvDataList(List persons, List fields) {
        List> result = new ArrayList<>();
        for (ESPesornResult person : persons) {
            result.add(cvt2List(person, fields));
        }
        return result;
    }
    
    
    private List cvt2List(ESPesornResult person, List fields) {
        List result = new ArrayList();
        if (person == null|| null ==person.getCustInfos()) return result;
        for (String field : fields) {
            result.add(StringUtil.getNotNullStr(person.getCustInfos().getOrDefault(field, new Attribute()).getValue()));
        }
        return result;
    }



	  /** 
	     * 写一行数据方法
	     * @param row
	     * @param csvWriter
	     * @throws IOException
	     */
	    private static void writeRow(List row, BufferedWriter csvWriter) throws IOException {
	        // 写入文件头部
	        StringBuffer sb = new StringBuffer();;
	        for (String data : row) {
	            sb.append(",").append(data).toString();
	        }
	        String rowStr = sb.toString();
	        csvWriter.write(rowStr.substring(1));
	        csvWriter.newLine();
	    }
	

 

在线下载:

 



	  /**
	     * 导出Excel
	     * @param dataList (数据)
	     * @param headers ()
	     */
	    @Override
	    public void downloadFile(List> dataList, List headers,HttpServletResponse response)throws Exception {
	        try {
	            String excelOrCsv = dmpSysConfigService.getSegmentDownloadExcelOrCsv();
	            if ("1".equals(excelOrCsv)){
	                downloadFileExcel(dataList,headers,response);
	            }else {
	                downloadFileCsv(dataList,headers,response);
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	            throw new ServiceException(e);
	        }
	
	    }
    private void downloadFileExcel(List> dataList, List headers,HttpServletResponse response)throws Exception {
        ServletOutputStream out = null;
        try {
            XSSFWorkbook workbook1 = new XSSFWorkbook();
            SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook1, 100);
            SXSSFSheet sheet = sxssfWorkbook.createSheet();
            CellStyle style = sxssfWorkbook.createCellStyle();
            CellStyle style2 = sxssfWorkbook.createCellStyle();
            exportExcel(sxssfWorkbook, style, style2);
            dataList.add(0,new ArrayList<>());
            for (int i = 0; i < dataList.size(); i++) {
                Row row = sheet.createRow(i);
                for (int j = 0; j < headers.size(); j++) {
                    if(i == 0) {
                        XSSFRichTextString text = new XSSFRichTextString(headers.get(j));
                        // 首行-标题
                        row.createCell(j).setCellValue(text);
                    } else {
                        // 数据
                        List rowArr = dataList.get(i);
                        HSSFRichTextString text = new HSSFRichTextString(rowArr.get(j));
                        CellUtil.createCell(row, j, String.valueOf(text));

                    }
                }
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String name = df.format(new Date())+".xlsx";


            // 导出
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            String codedFileName = URLEncoder.encode(name, "gbk");//文件名支持中文
            response.setHeader("Content-Disposition", "attachment;filename=" + codedFileName);
            out = response.getOutputStream();
            sxssfWorkbook.write(out);
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }

    }
    private void downloadFileCsv(List> dataList, List headers,HttpServletResponse response)throws Exception {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String name = df.format(new Date())+".csv";
            PrintWriter pw = null;

            // 导出
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=gbk");
            String codedFileName = URLEncoder.encode(name, "gbk");//文件名支持中文
            response.setHeader("Content-Disposition", "attachment;filename=" + codedFileName);
            pw = response.getWriter();

            dataList.add(0,new ArrayList<>());
            for (int i = 0; i < dataList.size(); i++) {
                String data = "";
                for (int j = 0; j < headers.size(); j++) {
                    if(i == 0) {
                        data= data+","+headers.get(j);
                    } else {
                        // 数据
                        data= data+","+dataList.get(i).get(j);
                    }
                }
                pw.println(data.substring(1));
            }
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

 

你可能感兴趣的:(java,excel,java,csv,download)