小功能实现(十三)使用POI的方式导出Excel表

前言

  • 通过POI的方式将获得的数据转换成Excel表格的形式

步骤

  • 直接上代码,Controller层
  • 笨蛋的我本来还以为HttpServletResponse还要返回,后来发现根本不用,它会自行返回
	//导出电缆信息表
    @RequestMapping(value = "/exportCableLineInfoByExcel.do")
    public void exportCableLineInfoByExcel(EvCableLineInforVo evCableLineInforVo, HttpServletResponse httpServletResponse){
        evCablelineinforService.exportCableLineInfoByExcel(evCableLineInforVo,httpServletResponse);
    }
  • 服务实现层
@Override
public void exportCableLineInfoByExcel(EvCableLineInforVo evCableLineInforVo,HttpServletResponse httpServletResponse) {
    OutputStream outputStream = null;
    try {
        EvCablelineinforExample evCablelineinforExample = new EvCablelineinforExample();
        EvCablelineinforExample.Criteria criteria = evCablelineinforExample.createCriteria();

        if (!evCableLineInforVo.getCableLineName().isEmpty() && !evCableLineInforVo.getCableLineName().equals("")) {
            criteria.andCablelinenameLike("%" + evCableLineInforVo.getCableLineName() + "%");
        }

		//这是准备转换的数据
        List<EvCablelineinfor> evCablelineinforList = evCablelineinforMapper.selectByExample(evCablelineinforExample);

        //创建xlsx文件
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();

        //创建水平居中的样式
        HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);

        //通过hss创建sheet
        HSSFSheet sheet = hssfWorkbook.createSheet("电缆信息表");

        int[] width = {6000, 18000, 7000, 7000, 3000, 4000, 3000, 3000};

        //第一行的内容
        String[] str = {"线路名称", "电缆名称", "起始设备", "终止设备", "电缆长度", "弧线类型", "运行状态", "覆盖类型"};
        //创建第一行的表单内容
        HSSFRow row1 = sheet.createRow(0);
        //创建第一行单元格内容
        for (int i = 0; i < str.length; i++) {
            //设置宽度
            sheet.setColumnWidth(i, width[i]);
            //设置每一列的样式(居中对齐,可选)
//                sheet.setDefaultColumnStyle(i,cellStyle);
            //第一行的内容
            row1.createCell(i).setCellValue(str[i]);
            //设置第一行的居中对齐
            row1.getCell(i).setCellStyle(cellStyle);
        }

        //遍历循环数据进行添加
        for (int i = 0; i < evCablelineinforList.size(); i++) {
            EvCablelineinfor evCablelineinfor = evCablelineinforList.get(i);
            //创建第几行
            HSSFRow row2 = sheet.createRow(i + 1);
            //设置每个单元格内容
            row2.createCell(0).setCellValue(evCablelineinfor.getLoopname());
            row2.createCell(1).setCellValue(evCablelineinfor.getCablelinename());
            row2.createCell(2).setCellValue(evCablelineinfor.getStartdevice());
            row2.createCell(3).setCellValue(evCablelineinfor.getEnddevice());
            row2.createCell(4).setCellValue(String.valueOf(evCablelineinfor.getCablelength()));
            row2.createCell(5).setCellValue(evCablelineinfor.getArctype());
            row2.createCell(6).setCellValue(evCablelineinfor.getRunstatus());
            row2.createCell(7).setCellValue(evCablelineinfor.getCovertype());

        }

        //获取表并写入
        outputStream = httpServletResponse.getOutputStream();
        httpServletResponse.setContentType("application/x-download");
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setHeader("Content-disposition", "attachment;filename=" + new String("电缆信息表".getBytes(), "ISO8859-1") + ".xlsx");
        hssfWorkbook.write(outputStream);
        outputStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

参考

  • https://zhuanlan.zhihu.com/p/147023706

你可能感兴趣的:(小功能,excel,java,开发语言)