poi导出excel

private static String [] HeaderList4 = { "时间", "小区", "低CQI占比" };

OutputStream output = null;
        try {
            // 设置response头信息
            response.reset();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");// 改成输出excel文件
            response.setHeader("Content-disposition",
                    "attachment;filename="  + URLEncoder.encode("优化指标"+TimeConvertor.todayTime()+".xlsx", "UTF-8"));

            // 创建HSSFWorkbook对象(excel的文档对象)
            Workbook wb = new XSSFWorkbook();
            //获取工参时间与详情时间
            request.setScanStartTime(TimeConvertor.beforeYestodaysDate());
            //上个月1号
            request.setBeforeMonth(TimeConvertor.beforeMonth());
            ArrayList<TwoOptimizationKpiEntity> dataList = mapper.getDataList(request);
            for (TwoOptimizationKpiEntity twoOptimizationKpiEntity : dataList) {
                if(twoOptimizationKpiEntity.getLongitude()==null || twoOptimizationKpiEntity.getLongitude()<100 || twoOptimizationKpiEntity.getLongitude()>200) {
                    Double[] xx = {};
                    twoOptimizationKpiEntity.setCoordinateList(xx);
                }else {
                Double[] xx = { twoOptimizationKpiEntity.getLongitude(), twoOptimizationKpiEntity.getLatitude() };
                twoOptimizationKpiEntity.setCoordinateList(xx);
                }
            }
            List<List<String>> contenets3 = new ArrayList<>();
            for (TwoOptimizationKpiEntity workOrderEntity : dataList) {
                List<String> content = new ArrayList<>();
                content.add(String.valueOf(workOrderEntity.getTimeStamp()));
                content.add(String.valueOf(workOrderEntity.getCellName()));
                content.add(String.valueOf(workOrderEntity.getHdFlag()));
                contenets3.add(content);
            }
            if (request.getHdFlag() == 4) {
                creatSheet(wb, "低CQI占比", HeaderList4, contenets3);
            }
          
            // 输出Excel文件
            output = response.getOutputStream();
            wb.write(output);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                output.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private void creatSheet(Workbook wb, String sheetName, String[] header, List<List<String>> contents) {
        // 建立新的sheet对象(excel的表单)
        Sheet sheet = wb.createSheet(sheetName);
        // 在sheet里创建第一行
        Row row = sheet.createRow(0);
        int i = 0;
        for (String head : header) {
            row.createCell(i++).setCellValue(head);
        }
        // 在sheet里创建行
        int rownum = 1;
        for (List<String> content : contents) {
            i = 0;
            Row row1 = sheet.createRow(rownum++);
            for (String s : content) {
                row1.createCell(i++).setCellValue(s);
            }
        }
    }

原文地址:https://www.cnblogs.com/ymj2018/p/10112151.html