包头考试bug修复
This commit is contained in:
parent
55d4fb1dbd
commit
f20c3861ae
@ -296,8 +296,6 @@ public class ExamWorkerController {
|
||||
if (CollUtil.isEmpty(recordIds)) {
|
||||
throw new OpenAlertException("无试卷下载");
|
||||
}
|
||||
//List<ExamTrainRecord> trainRecords = examTrainRecordService.list(new LambdaQueryWrapper<ExamTrainRecord>()
|
||||
// .in(ExamTrainRecord::getId, recordIds));
|
||||
List<ExamTrainRecordVo> trainRecords = examTrainRecordService.queryList(param);
|
||||
List<Long> pids = trainRecords.stream().map(ExamTrainRecord::getExamPaperId).collect(Collectors.toList());
|
||||
if (CollUtil.isEmpty(pids)) {
|
||||
@ -394,9 +392,9 @@ public class ExamWorkerController {
|
||||
params.put("score", record.getScore());
|
||||
params.put("startExamTime", DateUtil.format(record.getStartExamTime(), "yyyy年MM月dd日"));
|
||||
|
||||
File tempFile = new File(foldFile, wn + ".docx");
|
||||
File exportFile = new File(foldFile, wn + ".docx");
|
||||
//生成word文档流
|
||||
EasyPoiUtil.exportNewLineWord(params, tempUrl, tempFile);
|
||||
EasyPoiUtil.exportNewLineWordAndPicForExam(params, tempUrl, exportFile);
|
||||
|
||||
} catch (IOException e) {
|
||||
log.error("", e);
|
||||
@ -421,7 +419,9 @@ public class ExamWorkerController {
|
||||
private void addOption(List<ExamQuestionBankVo> list, StringBuilder content, int[] i, HashMap<String, Object> imageMap, Map<String, Object> params) {
|
||||
//遍历题目
|
||||
for (ExamQuestionBankVo question : list) {
|
||||
content.append(" ").append(i[0]).append("、");
|
||||
content.append(" ")
|
||||
.append("##红" + ((question.getOptions().equals(question.getSelectOption())) ? "√" : "×") + "##")//题目前面拼上答对还是错了
|
||||
.append(i[0]).append("、");
|
||||
i[0] += 1;
|
||||
if (StrUtil.isNotBlank(question.getPic())) {
|
||||
JSONArray ja = JSON.parseArray(question.getPic());
|
||||
@ -433,7 +433,7 @@ public class ExamWorkerController {
|
||||
String dest = PathUtil.getBasePath() + "/temp/" + IdUtil.randomUUID() + "." + StrUtil.subAfter(url, ".", true);
|
||||
HttpUtil.downloadFile(url, dest);
|
||||
imageMap.put(imageKey, EasyPoiUtil.imgFormatting(dest, 18, 18));
|
||||
content.append("##" + imageKey + "##");
|
||||
content.append("##图" + imageKey + "##");
|
||||
params.put(imageKey, imageMap.get(imageKey));
|
||||
} catch (Exception e) {
|
||||
log.error("导出word添加题目和选项错误", e);
|
||||
@ -446,7 +446,15 @@ public class ExamWorkerController {
|
||||
} else if (Objects.equals(question.getSelectOption(), "错")) {
|
||||
selectOption = "×";
|
||||
}
|
||||
content.append(question.getQuestionName().replaceAll("[((]\\s*?[))]", "( " + selectOption + " )")).append("\n");
|
||||
String correct;
|
||||
if (question.getType() != 3) {
|
||||
correct = question.getOptions();
|
||||
} else {
|
||||
correct = Objects.equals(question.getOptions(), "对") ? "√" : "×";
|
||||
}
|
||||
content.append(question.getQuestionName().replaceAll("[((]\\s*?[))]", "( " + selectOption + " )"))
|
||||
.append("##红正确答案:" + correct + "##")//题目后面拼上正确答案
|
||||
.append("\n");
|
||||
if (question.getType() == 3) {
|
||||
continue;
|
||||
}
|
||||
@ -591,6 +599,8 @@ public class ExamWorkerController {
|
||||
if (finalErrSingle > ints[0]) {
|
||||
if (options.equals("A")) {
|
||||
options = "B";
|
||||
} else {
|
||||
options = "A";
|
||||
}
|
||||
ints[0] = ints[0] + 1;
|
||||
}
|
||||
@ -598,6 +608,8 @@ public class ExamWorkerController {
|
||||
if (finalErrJudge > ints[1]) {
|
||||
if (options.equals("对")) {
|
||||
options = "错";
|
||||
} else {
|
||||
options = "对";
|
||||
}
|
||||
ints[1] = ints[1] + 1;
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ public interface ExamTrainRecordMapper extends BaseMapper<ExamTrainRecord> {
|
||||
*/
|
||||
Page<ExamTrainRecordVo> pageList(Page page, @Param(Constants.WRAPPER) Wrapper<ExamTrainRecordVo> wrapper, @Param("param") Map<String, Object> param);
|
||||
|
||||
List<ExamTrainRecordVo> pageList(@Param(Constants.WRAPPER) HashMap<String, Object> map, @Param(Constants.WRAPPER) QueryWrapper<ExamTrainRecordVo> queryWrapper);
|
||||
List<ExamTrainRecordVo> pageList(@Param("param") HashMap<String, Object> map, @Param(Constants.WRAPPER) QueryWrapper<ExamTrainRecordVo> queryWrapper);
|
||||
|
||||
/**
|
||||
* 分页列表查询教育培训记录信息
|
||||
|
||||
@ -28,18 +28,45 @@ public class EasyPoiUtil {
|
||||
*
|
||||
* @param response
|
||||
* @param params
|
||||
* @param file
|
||||
* @param file 模板
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void exportNewLineWordAndPicForExam(HttpServletResponse response, Map<String, Object> params, File file) {
|
||||
try {
|
||||
XWPFDocument doc = buildDocumentForExam(params, file);
|
||||
//设置响应体内容类型
|
||||
response.setContentType("application/octet-stream");
|
||||
//添加响应头
|
||||
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("下载试卷.docx", "UTF-8"));
|
||||
//暴露新添加的响应头
|
||||
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
|
||||
//将word文档流输出到输出流中
|
||||
doc.write(response.getOutputStream());
|
||||
//关闭流
|
||||
doc.close();
|
||||
// FileUtil.del(tempPath);
|
||||
} catch (Exception e) {
|
||||
log.error("exportNewLineWord:", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建导出的document
|
||||
*
|
||||
* @param params
|
||||
* @param file 模板
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private static XWPFDocument buildDocumentForExam(Map<String, Object> params, File file) throws Exception {
|
||||
XWPFDocument doc = null;
|
||||
FileOutputStream tempFos = null;
|
||||
try {
|
||||
//导出临时文件
|
||||
doc = WordExportUtil.exportWord07(URLDecoder.decode(file.getPath(), "UTF-8"), params);
|
||||
WordUtils.addBreakInCellForExam(doc.getParagraphs());
|
||||
|
||||
//把######改成占位符{{}}
|
||||
WordUtils.addBreakInCellForExam(doc.getParagraphs());
|
||||
//把####改成占位符{{}}
|
||||
List<XWPFParagraph> paragraphs = doc.getParagraphs();
|
||||
Iterator<XWPFParagraph> itPara = paragraphs.iterator();
|
||||
while (itPara.hasNext()) {
|
||||
@ -47,9 +74,9 @@ public class EasyPoiUtil {
|
||||
List<XWPFRun> run = paragraph.getRuns();
|
||||
for (int i = 0; i < run.size(); i++) {
|
||||
XWPFRun xwpfRun = run.get(i);
|
||||
String text = xwpfRun.getText(xwpfRun.getTextPosition());
|
||||
if (text.contains("##")) {
|
||||
String replacedText = text.replaceAll("##(.*?)##", "{{}}{{$1}}{{}}");
|
||||
String text = xwpfRun.getText(0);
|
||||
if (text.contains("##图")) {
|
||||
String replacedText = text.replaceAll("##图(.*?)##", "{{}}{{$1}}{{}}");
|
||||
// 清空当前 run 的文本
|
||||
xwpfRun.setText("", 0);
|
||||
|
||||
@ -67,36 +94,45 @@ public class EasyPoiUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
itPara = paragraphs.iterator();
|
||||
while (itPara.hasNext()) {
|
||||
XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
|
||||
List<XWPFRun> runs = paragraph.getRuns();
|
||||
//处理正确答案等
|
||||
for (int i = 0; i < runs.size(); i++) {
|
||||
XWPFRun xwpfRun = runs.get(i);
|
||||
String text = xwpfRun.getText(0);
|
||||
if (text.contains("##红")) {
|
||||
//设置红色字体
|
||||
String replacedText = text.replaceAll("##红(.*?)##", "[[[[$1]]]]");
|
||||
// 清空当前 run 的文本
|
||||
xwpfRun.setText("", 0);
|
||||
|
||||
String[] parts = replacedText.split("(?=\\[\\[\\[\\[)|(?<=\\]\\]\\]\\])");
|
||||
for (String part : parts) {
|
||||
XWPFRun newRun = paragraph.insertNewRun(i++); // 插入新的 run
|
||||
newRun.setFontSize(10);
|
||||
newRun.setFontFamily("宋体");
|
||||
if (part.startsWith("[[[[") && part.endsWith("]]]]")) {
|
||||
newRun.setColor("FF0000"); // 设置红色字体
|
||||
part = part.substring(4, part.length() - 4);
|
||||
}
|
||||
newRun.setText(part);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String tempName = IdUtil.randomUUID() + ".docx";
|
||||
String tempPath = PathUtil.getBasePath() + "/temp/" + tempName;
|
||||
tempFos = new FileOutputStream(tempPath);
|
||||
doc.write(tempFos);
|
||||
tempFos.close();
|
||||
doc.close();
|
||||
|
||||
//导出response
|
||||
doc = WordExportUtil.exportWord07(URLDecoder.decode(tempPath, "UTF-8"), params);
|
||||
//设置响应体内容类型
|
||||
response.setContentType("application/octet-stream");
|
||||
//添加响应头
|
||||
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("下载试卷.docx", "UTF-8"));
|
||||
//暴露新添加的响应头
|
||||
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
|
||||
//将word文档流输出到输出流中
|
||||
doc.write(response.getOutputStream());
|
||||
//关闭流
|
||||
doc.close();
|
||||
// FileUtil.del(tempPath);
|
||||
} catch (Exception e) {
|
||||
log.error("exportNewLineWord:", e);
|
||||
return doc;
|
||||
} finally {
|
||||
if (doc != null) {
|
||||
try {
|
||||
doc.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (tempFos != null) {
|
||||
try {
|
||||
tempFos.close();
|
||||
@ -107,6 +143,24 @@ public class EasyPoiUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出word和图片
|
||||
*
|
||||
* @param map
|
||||
* @param url 模版
|
||||
* @param exportFile 导出文件
|
||||
*/
|
||||
public static void exportNewLineWordAndPicForExam(Map<String, Object> map, String url, File exportFile) {
|
||||
try {
|
||||
XWPFDocument doc = buildDocumentForExam(map, new File(url));
|
||||
FileOutputStream fos = new FileOutputStream(exportFile);
|
||||
doc.write(fos);
|
||||
fos.close();
|
||||
} catch (Exception e) {
|
||||
log.error("exportNewLineWord:", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出word
|
||||
*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user