Poi模板导出(设置字体样式)

一.controller查询数据,并调用此方法

/**
     * 试题模板导出
     * @author zhengzx
     * @datetime 2021-11-17 11:48:59
     * @param response
     * @param storeId 题库id
     * @return ResultVo
     */
     @GetMapping("/outQuestions")
     public ResultVo outQuestions(HttpServletResponse response, String storeId) throws IOException {
         eduQuestionStoreService.outQuestions(response,storeId);
         return new ResultVo(ResultCode.OK);
     }

二.Service层代码如下:

 /**
     * 试题模板导出
     *
     * @param storeId
     * @author zhengzx
     * @date 2021/11/17 11:54:12
     */
    public void outQuestions(HttpServletResponse response, String storeId) throws IOException {
        //查询所有题目库对应的题目
        List<EduQuestionEntity> eduQuestList = eduQuestionDao.findAllQuestion(storeId);
        List<List> questionList = new ArrayList<>();
        for (int i = 0; i < eduQuestList.size(); i++) {
            List<String> question = new ArrayList<>();
            question.add(eduQuestList.get(i).getCategoryId());
            question.add(eduQuestList.get(i).getName());
            List<EduOptionEntity> eduOptionList = eduOptionDao.findAllOption(eduQuestList.get(i).getQuestionId());
            for (int j = 0; j < eduOptionList.size(); j++) {
                question.add(eduOptionList.get(j).getName());
            }
            questionList.add(question);
        }
        //创建表头提示信息
        String hint = "注:题型请填写数字。10代表单选题,20代表多选题,30代表判断题,多选题答案逗号隔开,判断题A代表正确,B代表错误";
        //创建表头
        String[] questionsHeader = {"题型", "题目", "A", "B", "C", "D", "E", "正确答案"};
        //1.创建Excel工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //1.1声明一个字体对象
        HSSFFont font = workbook.createFont();
        HSSFFont contentFont = workbook.createFont();
        //1.1.1设置字体样式
        font.setFontName("Cambria");
        contentFont.setFontName("Cambria");
        //1.1.2设置字体大小
        font.setFontHeightInPoints((short) 11);
        contentFont.setFontHeightInPoints((short) 11);
        //1.2设置字体颜色
        font.setColor(IndexedColors.RED.getIndex());
        //1.3将字体对象放入到HSSFCellStyle对象中
        HSSFCellStyle style = workbook.createCellStyle();
        style.setFont(font);
        HSSFCellStyle contentStyle = workbook.createCellStyle();
        contentStyle.setFont(contentFont);
        //1.3.1将文件头部信息设置为文本居中
        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //1.3.2将文件内容设置为垂直居中
        contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        //设置文本超出自动换行
        contentStyle.setWrapText(true);
        //2.创建一个工作表
        HSSFSheet sheet = workbook.createSheet("琅琊榜模板");
        //3.1创建行存储提示信息
        HSSFRow w = sheet.createRow(0);
        //3.2创建行存储文件头部信息
        HSSFRow w1 = sheet.createRow(1);
        //4.创建列
        HSSFCell h1 = null;
        HSSFCell h2 = null;
        //5.设置默认宽度
        sheet.setDefaultColumnWidth(20);
        //6.1创建行,并填入头部提示信息
        h2 = w.createCell(0);
        h2.setCellStyle(style);
        h2.setCellValue(hint);
        //6.2创建行,并填入文件头部信息
        for (int i = 0; i < questionsHeader.length; i++) {
            h1 = w1.createCell(i);
            h1.setCellStyle(contentStyle);
            h1.setCellValue(questionsHeader[i]);
        }
        //遍历List,取出其中的list(获取到单行的数据)
        for (int i = 0; i < questionList.size(); i++) {
            List<String> list = questionList.get(i);
            //创建行,从第三行开始以此类推
            Row row = sheet.createRow(i + 2);
            for (int j = 0; j < list.size(); j++) {
                //创建第三行第一列(3,1)~(3,list.size-1)
                Cell cell1 = row.createCell(j);
                cell1.setCellStyle(contentStyle);
                cell1.setCellValue(list.get(j));
            }

        }
        OutputStream outputStream = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("试题导入模板" + JDateUtils.getDate(), "UTF-8") + ".xls");
        response.setContentType("application/msexcel");
        //8.输出
        workbook.write(outputStream);
        outputStream.close();
        //9.关流
        response.flushBuffer();
    }

三:查询数据步骤略

四.效果图如下:

在这里插入图片描述
ps: pom依赖啥的网上有现成的,就不发出来了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小郑要做干饭人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值