Java Excel 导出

public HSSFWorkbook exportNotice(HttpServletResponse response) throws IOException {
        Map<String, String> param = new HashMap<>();
        List<XcNotice> list =  queryForList("XcNoticeMapping.getNoticeList", param, XcNotice.class);

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("题库");
        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 12);
        font.setFontName("新宋体");
        HSSFCellStyle style = wb.createCellStyle();
        style.setFont(font); // 调用字体样式对象
        style.setWrapText(true);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

        HSSFRow row = sheet.createRow(0);

        HSSFCell cell = row.createCell(0);
        cell.setCellStyle(style);
        cell.setCellValue("序号");

        HSSFCell cell1 = row.createCell(1);
        cell1.setCellStyle(style);
        cell1.setCellValue("id");

        HSSFCell cell2 = row.createCell(2);
        cell2.setCellStyle(style);
        cell2.setCellValue("巡察计划id");

        HSSFCell cell3 = row.createCell(3);
        cell3.setCellStyle(style);
        cell3.setCellValue("公告标题");

        sheet.setColumnWidth(0, 3766);
        sheet.setColumnWidth(1, 3766);
        sheet.setColumnWidth(2, 3766);
        sheet.setColumnWidth(3, 3766);

        for (int i = 0; i < list.size(); i++) {
            XcNotice xcNotice = list.get(i);
            if(xcNotice == null){
                continue;
            }

            HSSFRow rowx = sheet.createRow(i + 1);
            rowx.setHeightInPoints(20);

            HSSFCell cell00 = rowx.createCell(0);
            cell00.setCellStyle(style);
            cell00.setCellValue(i + 1);// 序号

            HSSFCell cell01 = rowx.createCell(1);
            cell01.setCellStyle(style);
            cell01.setCellValue(xcNotice.getId());

            HSSFCell cell02 = rowx.createCell(2);
            cell02.setCellStyle(style);
            cell02.setCellValue(xcNotice.getXcjhId());

            HSSFCell cell03 = rowx.createCell(3);
            cell03.setCellStyle(style);
            cell03.setCellValue(xcNotice.getTitle());
        }

        //浏览器下载
        OutputStream output = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=xcNotice.xls");
        response.setContentType("application/msexcel");
        wb.write(output);
        output.close();
        return wb;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值