首页 > 其他分享 >基于Hutool技术Excel表格导出

基于Hutool技术Excel表格导出

时间:2024-09-06 15:02:38浏览次数:4  
标签:sList 表格 get question writer Excel Hutool item null

今天分享一下基于Hutool技术Excel表格导出,我们先看看导出Excel表格的样子

第1步:引入maven依赖

  <dependencies>
    <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>5.3.3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>5.2.2</version>
    </dependency>
  </dependencies>

第2步:controller接口

public void exportExcel2(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String questionType = req.getParameter("questionType") == null ? "1" : req.getParameter("questionType");
            List<Map> list = exportDao.exportExcel2(questionType);
            List<Map> choiceList = exportDao.exportChoice();
            ExcelWriter writer = ExcelUtil.getWriter(true);
            writer.writeRow(Arrays.asList("序号","技术领域","考核类型","现有领域","试题类型","试题内容","试题答案","选项1","选项2","选项3","选项4","选项5","选项6","选项7","选项8"));
            String answer = "ABCDEFGH";

            if("1".equals(questionType) || "2".equals(questionType)) { // 多选题,单选题
                list.stream().forEach(item->{
                    List<String> sList = new ArrayList<>();
                    StringBuilder stringBuilder = new StringBuilder();
                    choiceList.stream().forEach(choiceItem->{
                        if(item.get("question_id").equals(choiceItem.get("question_id"))){
                            sList.add((String) choiceItem.get("choice_content"));
                            stringBuilder.append(choiceItem.get("is_right"));
                        }
                    });
                    switch (sList.size()) {
                        case 1:
                            writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),null,null,null,null,null,null,null));
                            break;
                        case 2:
                            writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),null,null,null,null,null,null));
                            break;
                        case 3:
                            writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),null,null,null,null,null));
                            break;
                        case 4:
                            writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),sList.get(3),null,null,null,null));

                            break;
                        case 5:
                            writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),sList.get(3), sList.get(4),null,null,null));

                            break;
                        case 6:
                            writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),sList.get(3), sList.get(4), sList.get(5),null,null));

                            break;
                        case 7:
                            writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),sList.get(3), sList.get(4), sList.get(5), sList.get(6),null));
                            break;
                        case 8:
                            writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),sList.get(3), sList.get(4), sList.get(5), sList.get(6),sList.get(7)));
                            break;
                    }
                });
            } else if ("3".equals(questionType)) { // 填空题
                list.stream().forEach(item->{
                    writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),item.get("question_answer"),null,null,null,null,null,null,null,null));
                });
            } else if("4".equals(questionType)) { // 判断题
                list.stream().forEach(item->{
                    String ans = item.get("question_answer").toString();
                    ans = "1".equals(ans) ? "正确" : "错误";
                    writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),ans,null,null,null,null,null,null,null,null));
                });
            } else if("5".equals(questionType) || "6".equals(questionType)) { // 简答题 或 程序设计题
                list.stream().forEach(item->{
                    writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),item.get("question_answer"),null,null,null,null,null,null,null,null));
                });
            }

            // 设置响应头content-type
            resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");

            // 设置下载文件名
            String fileName= URLEncoder.encode("成绩信息表","UTF-8");
            resp.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");

            ServletOutputStream outputStream= resp.getOutputStream();

            // 输出excel文件
            writer.flush(outputStream, true);
            outputStream.close();
            writer.close();

        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }
    }

关键技术点:

public void exportExcel2(HttpServletRequest req, HttpServletResponse resp) {
        try {
            // 省略数据获取的操作....
            ExcelWriter writer = ExcelUtil.getWriter(true);
            
            // 设置表格列
            writer.writeRow(Arrays.asList("序号","技术领域","考核类型","现有领域","试题类型","试题内容","试题答案","选项1","选项2","选项3","选项4","选项5","选项6","选项7","选项8"));
            String answer = "ABCDEFGH";

            // 数据需要循环写入....
            // 写入数据操作
            writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),null,null,null,null,null,null,null));
            
            // 设置响应头content-type
            resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");

            // 设置下载文件名
            String fileName= URLEncoder.encode("成绩信息表","UTF-8");
            resp.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");

            ServletOutputStream outputStream= resp.getOutputStream();

            // 输出excel文件
            writer.flush(outputStream, true);
            outputStream.close();
            writer.close();

        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }
    }

 

标签:sList,表格,get,question,writer,Excel,Hutool,item,null
From: https://www.cnblogs.com/liuyangjava/p/18400266

相关文章

  • JS解析JSON数据到TABLE表格
    效果图:  数据采用JSON,[{"时间段":"上午","XX小学班课表":[{"名称":"教师上班","时间":"8:00","星期一":"&......
  • Angular 导出Excel
    在Angular中导出Excel文件,可以使用第三方库如xlsx来处理Excel的生成和导出操作。以下是一个简单的步骤来实现从Angular项目中导出Excel文件。1.安装xlsx和file-saver在项目根目录下运行以下命令来安装所需的库:npminstallxlsxfile-saver--save2.在组件中......
  • 使用python读取excel数据(详解教程)
    使用Python读取Excel数据通常可以通过pandas库来实现。pandas提供了强大的数据处理功能,并且支持多种数据格式,包括Excel。下面是详细的代码讲解:目录1.安装必要的库2.读取Excel文件3.代码讲解1.导入库:2.指定文件路径和工作表名称:3.读取Excel文件:4.打印数据:......
  • 【VBA基础教程篇】Excel-VBA Debug调试相关操作
    Excel-VBADebug调试相关操作在工作窗口,上方菜单栏中,有一个专门的额菜单:Debug菜单,里面有debug相关操作。除此之外你也需要一些辅助窗口来帮助你更好的进行调试,1.Immediatewindow(立即窗口):类似其他IDE的console控制台。显示快捷键:Ctrl+G,也可以点击菜单栏View->Immediatewin......
  • python——表格在Excle和Word之间的转换
    如何在Python中将Word中的表格写入Excel,以及如何将Excel中的数据写入Word。我们将使用python-docx和openpyxl这两个库来完成这项任务。1.将Word中的表格写入Excel1.1安装所需库首先,确保安装了python-docx和openpyxl:pipinstallpython-docxopenpyxl1.2从Wo......
  • HTML5第二章 列表 表格与媒体元素
    一、列表1.无序列表<ui>:声明无序列表<li>:声明列表无序列表中的每项都是平级的,没有级别之分,并且列表中的内容一般都是相对简单的标题性质的网页内容.<ul>   <li>范冰冰演藏族女孩</li><!--没有顺序,每个<li>标签独占一行(块元素)-->   <li>撞死两个人后自拍<......
  • python电梯厂企业固定资产管理系统excel数据导入 9327d
    目录博主介绍技术栈......
  • Excel制作“非闭合圆环图”稍难点
     ABC1区域增长率辅助列2北京18.39%81.61%3厦门20.89%79.11%4上海32.85%67.15%5山东44.53%55.47%6广州50.02%49.98%7黑龙江73.19%26.81%      (上述表格,是随便打出来的)      稍难点一:做出圆环 如上图,7个地区的圆环只能一个一个复制出来。首先在【插......
  • Navicat如何导出Excel格式的表结构
    第1步:打开Navicat,双击打开你要导出表结构的数据库,点击“查询”——“新建查询”第2步:将以下SQL语句复制粘贴进查询编辑器,并修改数据库名称与表名称SELECTCOLUMN_NAME列名,DATA_TYPE数据类型,CHARACTER_MAXIMUM_LENGTH长度,IS_NULLABLE是否为空,C......
  • 【工具使用】【EasyExcel 】EasyExcel 实现 Excel 作者信息、版本信息等的写入和读取
    1 前言导入的功能,想必大家都做过,大家肯定也都遇到过比如我的模板变化了(比如新增一列、删除一列等),客户在使用的时候可能还是用的老模板进行导入,那么我们在写代码的时候,应该怎么快速识别到呢?比如可以比较客户导入的Excel一列一列的去比较或者列的个数等是可以的。我想的一个......