JAVA Excel 批量导出实例

   Map<String, Object> map = new HashMap<>();
//从其它方法获的得集合
        List<PlanDetail> planManageList = planManageService.accordTimeSearch(startDate, stopDate, isComplete);
        // 创建excel
        HSSFWorkbook wk = new HSSFWorkbook();
        // 创建一张工作表
        HSSFSheet sheet = wk.createSheet();
        // 2
        sheet.setColumnWidth(0, 5000);
        HSSFRow row = sheet.createRow(0);
        // 创建第一行的第一个单元格
        // 想单元格写值
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("材质");
        cell = row.createCell((short) 1);
        cell.setCellValue("砖型");
        cell = row.createCell((short) 2);
        cell.setCellValue("重量");
        cell = row.createCell((short) 3);
        cell.setCellValue("总重量 ");
        cell = row.createCell((short) 4);
        cell.setCellValue("实际完成数量");
        cell = row.createCell((short) 5);
        cell.setCellValue("计划结束时间 ");
        cell = row.createCell((short) 6);
        cell.setCellValue("差额 ");
        cell = row.createCell((short) 7);
        cell.setCellValue("派工数量 ");
        cell = row.createCell((short) 8);
        cell.setCellValue("是否结案");
        for (int i = 0; i < planManageList.size(); i++) {
            HSSFRow row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(planManageList.get(i).getMaterialModel());
            row1.createCell(1).setCellValue(planManageList.get(i).getMaterialName());
            row1.createCell(2).setCellValue(planManageList.get(i).getZll());
            row1.createCell(3).setCellValue(planManageList.get(i).getZll() * planManageList.get(i).getDoneQuantity());
            if (planManageList.get(i).getTotalDoneQuantity() == null) {
                row1.createCell(4).setAsActiveCell();
                row1.createCell(6).setCellValue(0 - planManageList.get(i).getDoneQuantity());
            } else {
                row1.createCell(4).setCellValue(planManageList.get(i).getTotalDoneQuantity());
                row1.createCell(6).setCellValue(planManageList.get(i).getTotalDoneQuantity() - planManageList.get(i).getDoneQuantity());
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String planEndTime = simpleDateFormat.format(planManageList.get(i).getPlanEndTime());
            row1.createCell(5).setCellValue(planEndTime);
            if (planManageList.get(i).getTaskNum() == null) {
                row1.createCell(7).setAsActiveCell();
            } else {
                row1.createCell(7).setCellValue(planManageList.get(i).getTaskNum());
            }
            if (planManageList.get(i).getIsComplete().equals("1")) {
                row1.createCell(8).setCellValue("已结案");
            } else {
                row1.createCell(8).setCellValue("未结案");
            }
        }
        String   url=null;
        String datetime=null;
        try {
             datetime=String.valueOf(Calendar.getInstance().getTimeInMillis())+".xls";
             url  =excelPath+datetime;
            FileOutputStream outputStream = new FileOutputStream(url);
            wk.write(outputStream);
            outputStream.close();
        } catch (FileNotFoundException e) {
            map.put("error","导出失败,找不到路径");
            return map;
        } catch (IOException e) {

        }
        if (url==null){
            map.put("error","导出失败");
            return map;
        }
        map.put("success", true);
        map.put("url",datetime);
        return map;

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值