hutool Excel导入导出

时间:2025-03-10 14:18:21
@ApiOperation(value = "试题模板下载") @PostMapping("import/template") public void quImportTemplate(HttpServletResponse response) throws IOException { QuExportDTO redioQuExport = new QuExportDTO(); redioQuExport.setRepos("导入题库"); redioQuExport.setQuType("单选题"); redioQuExport.setQuContent("公司总部在哪里"); redioQuExport.setAnswerContent("(A)上海(B)郑州(C)北京(D)苏州"); redioQuExport.setIsRight("A"); redioQuExport.setQuAnalysis("上海"); redioQuExport.setQuScore("6"); QuExportDTO judgeQuExport = new QuExportDTO(); judgeQuExport.setRepos("导入题库"); judgeQuExport.setQuType("判断题"); judgeQuExport.setQuContent("公司的创始人是XX"); judgeQuExport.setAnswerContent("(A)错误(B)正确"); judgeQuExport.setIsRight("B"); judgeQuExport.setQuAnalysis(""); judgeQuExport.setQuScore("6"); //写2行数据 List<QuExportDTO> rows = CollUtil.newArrayList(redioQuExport,judgeQuExport); // 通过工具类创建writer,默认创建xls格式,设置为true,创建xlsx格式 ExcelWriter writer = ExcelUtil.getWriter(true); //第一行,合并单元格,注意行 ,加上\n后,在excel中使用自动换行查看时会自动换行 writer.merge(6, "\n" + "1、系统从第3行开始解析,请不要删除注意行与表头行;\n" + "2、题库名称:必填,必须是后台已创建好的题库名称,否则无法导入,多个以英文逗号分隔\n" + "3、题型:必填,请填写:单选题,多选题,判断题,简答题,填空题中的一个;\n" + "4、题干:必填,填写文字;\n" + "5、选项:简答题不必填,其他题型必填;选项不能少于一个,判断题选项不多于两个;选项字母为大写英文字母,选项字母括号为英文括号;格式如下:\n" + "(A)起重机(B)船坞(C)领航员(D)轮船\n" + "6、答案:填空题简答题不必填,其他题型必填;" +"单选题和判断题格式:A;多选题格式:AC;" +"简答题直接填写答案文字\n" + "7、解析:不必填\n",false); //自定义标题别名,表格头,不加的话默认使用实体类字段属性 writer.addHeaderAlias("repos", "题库名称"); writer.addHeaderAlias("quType", "题型"); writer.addHeaderAlias("quContent", "题干"); writer.addHeaderAlias("quScore", "题目分值"); writer.addHeaderAlias("answerContent", "选项"); writer.addHeaderAlias("isRight", "答案"); writer.addHeaderAlias("quAnalysis", "解析"); // 一次性写出内容,使用默认样式,强制输出标题 writer.write(rows, true); //设置第一行的长和高样式 writer.setCurrentRow(0).setRowHeight(0,100) .setColumnWidth(1,100); //response为HttpServletResponse对象 response.setContentType("application/;charset=utf-8"); //是弹出下载对话框的文件名,不能为中文,中文请自行编码 response.setHeader("Content-Disposition","attachment;filename="); //out为OutputStream,需要写出到的目标流 ServletOutputStream out=response.getOutputStream(); writer.flush(out, true); // 关闭writer,释放内存 writer.close(); //此处记得关闭输出Servlet流 IoUtil.close(out); }