diff --git a/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java index d4e4a57..19da937 100644 --- a/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java @@ -25,9 +25,9 @@ import com.teaching.backend.model.vo.courses.CourseObjectivesTreeVO; import com.teaching.backend.model.vo.courses.CoursesVO; import com.teaching.backend.service.courses.ICoursesService; import com.teaching.backend.utils.CourseCode; -import org.apache.poi.xwpf.usermodel.XWPFDocument; -import org.apache.poi.xwpf.usermodel.XWPFParagraph; -import org.apache.poi.xwpf.usermodel.XWPFRun; +import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.xwpf.usermodel.*; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -38,7 +38,9 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.math.BigDecimal; +import java.math.BigInteger; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; @@ -323,8 +325,9 @@ public class CoursesServiceImpl extends ServiceImpl impl public void down(HttpServletResponse response, String id) throws Exception { File rootPath = new File(ResourceUtils.getURL("classpath:").getPath()); //SpringBoot项目获取根目录的方式 File templatePath = new File(rootPath.getAbsolutePath(),"/templates/courses.docx");//------------------需要模板的地址 - // 加载Word模板 - FileInputStream template = new FileInputStream("/templates/courses.docx"); + + // 加载Word模板------------------路径换成服务器里文件的绝对路径(要是相对路径也行也OK) + FileInputStream template = new FileInputStream("D:\\Users\\Desktop\\teaching-backend\\src\\main\\resources\\templates\\courses.docx"); XWPFDocument document = new XWPFDocument(template); //准备导出数据 @@ -339,50 +342,34 @@ public class CoursesServiceImpl extends ServiceImpl impl Integer course_classhour = courses.getClasshours(); String course_description = courses.getDescription(); - //查询课程目标 List courseObjectivesTreeVO = objectivesService.queryCourseObjectivesTree(id); -// System.out.println("课程目标:"+courseObjectivesTreeVO); //准备数据 //总目标 StringBuilder overall_goal = new StringBuilder(); - for (ObjectiveContents content : courseObjectivesTreeVO.get(0).getContents()) { - overall_goal.append("\r\n ").append(content.getContent()); + List contents = courseObjectivesTreeVO.get(0).getContents(); + for (int i = 0; i < contents.size(); i++) { + ObjectiveContents content = contents.get(i); + overall_goal.append(content.getContent()); } - System.out.println(overall_goal); - //分项目标 - //知识目标 - StringBuilder knowledge_goals = new StringBuilder(); - StringBuilder political_goals = new StringBuilder(); - StringBuilder value_goals = new StringBuilder(); - - + StringBuilder sub_objectives = new StringBuilder(); + //课程目标数 + int course_number = 0; for (int i = 0; i < courseObjectivesTreeVO.get(0).getCourseObjectivesTrees().size(); i++){ CourseObjectivesTreeVO content = courseObjectivesTreeVO.get(0).getCourseObjectivesTrees().get(i); - //知识目标 - if (i == 0){ - for (ObjectiveContents c : content.getContents()) { - knowledge_goals.append("\r\n ").append(c.getContent()); - } - } - //思政目标 - if (i == 1){ - for (ObjectiveContents c : content.getContents()) { - political_goals.append("\r\n ").append(c.getContent()); - } + String name = content.getName(); + sub_objectives.append(name).append(":"); + for (int j = 0; j < content.getContents().size(); j++) { + course_number++; + sub_objectives.append("\r\n ").append("课程目标").append(course_number).append(":").append(content.getContents().get(i).getContent()); + } - //价值目标 - if (i == 2){ - for (ObjectiveContents c : content.getContents()) { - value_goals.append("\r\n ").append(c.getContent()); - } + if (i != courseObjectivesTreeVO.get(0).getCourseObjectivesTrees().size() - 1){ + sub_objectives.append("\r\n "); } } - System.out.println("知识目标:"+knowledge_goals); - System.out.println("思政目标:"+political_goals); - System.out.println("价值目标:"+value_goals); - + System.out.println("课程目标数:"+course_number); Map params = new HashMap(); params.put("course_name",course_name); params.put("course_code",course_code); @@ -393,26 +380,145 @@ public class CoursesServiceImpl extends ServiceImpl impl //课程目标 params.put("overall_goal", overall_goal); - params.put("knowledge_goals", knowledge_goals); - params.put("political_goals", political_goals); - params.put("value_goals", value_goals); - - //导出图片 - params.put("img","wyii\t"); - //根据模板+数据 导出文档 - + params.put("sub_objectives", sub_objectives); - XWPFDocument xwpfDocument = WordExportUtil.exportWord07(templatePath.getPath(), params); + //根据模板+数据 导出文档 + document = WordExportUtil.exportWord07(templatePath.getPath(), params); + + //----------------修改表格模版在导入参数之后 + + //获取第2个表格---------- + XWPFTable table = document.getTables().get(1); +// // 获取表格行 +// List rows = table.getRows(); +// //获取第一行 +// XWPFTableRow row = rows.get(0); +// //获取第一行第一列 +// XWPFTableCell cell = row.getCell(0); + //获取此行的每列数据 +// List cells = row.getTableCells(); +// System.out.println(cells.size());// 打印单元格数量 + for (int i = 0; i < course_number - 1; i++) { + //新增一行单元格 + XWPFTableRow newRow = table.createRow(); + newRow.getCell(0).setText("课程目标" + (i + 2)); +// //垂直居中 +// newRow.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); + } + //获取第5个表格 + XWPFTable table5 = document.getTables().get(4); + + for (int i = 0; i < course_number - 1; i++) { + //新增一行单元格 + XWPFTableRow newRow = table5.createRow(); + //获取第一列里的段落 +// XWPFParagraph newPara = newRow.getCell(0).getParagraphArray(0); +// XWPFRun newRun = newPara.createRun(); + //序号 + newRow.getCell(0).getParagraphArray(0).setIndentationLeft(0); + newRow.getCell(0).getParagraphArray(0).setIndentationHanging(0); + newRow.getCell(0).setText(Integer.valueOf(i+2).toString()); + newRow.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.TOP); +// newRow.getCell(0).setText(Integer.valueOf(i+2).toString().replaceAll("[\\s\\u00A0]+","").trim()); + + // newRun.setText((i + 2) + ""); +// newPara.setAlignment(ParagraphAlignment.CENTER);//水平居中 +// newRow.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);//垂直居中 + + //课程目标 + newRow.getCell(1).setText(" 课程目标" + (i + 2)); + //扩四列 + for (int j = 0; j < 4; j++) { + newRow.createCell(); + } + } + table5.setTableAlignment(TableRowAlign.CENTER); +// //遍历单元格并打印内容 +// for (XWPFTableCell cell : cells) { +// // 假设单元格只包含段落,遍历段落并打印文本 +// for (XWPFParagraph paragraph : cell.getParagraphs()) { +// for (XWPFRun run : paragraph.getRuns()) { +// run.setText("xxx",0); +// System.out.print(run.getText(0)); // 打印文本内容,参数0表示不考虑样式分隔符 +// } +// System.out.println(); // 在每个段落后打印换行符 +// } +// } +// System.out.println("表格行数:"+rows.size()); //文件名=课程名+课程标准 String filename= courses.getName()+"课程标准.docx"; //设置文件的打开方式和mime类型 ServletOutputStream outputStream = response.getOutputStream(); response.setHeader( "Content-Disposition", "attachment;filename=" + new String(filename.getBytes(),"ISO8859-1")); response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); - xwpfDocument.write(outputStream); + document.write(outputStream); } + +// public static void insertRow(XWPFTable table, int copyrowIndex, int newrowIndex) { +// // 在表格中指定的位置新增一行 +// XWPFTableRow targetRow = table.insertNewTableRow(newrowIndex); +// // 获取需要复制行对象 +// XWPFTableRow copyRow = table.getRow(copyrowIndex); +// //复制行对象给新增的行对象 +// targetRow.getCtRow().setTrPr(copyRow.getCtRow().getTrPr()); +// //获取需要复制行对象的列 +// List copyCells = copyRow.getTableCells(); +// //新增的对象的列 +// XWPFTableCell targetCell = null; +// //遍历复制行对象的列 +// for (int i = 0; i < copyCells.size(); i++) { +// //复制行对象的列 +// XWPFTableCell copyCell = copyCells.get(i); +// //新增的行对象创建一列 +// targetCell = targetRow.addNewTableCell(); +// //格式复制 +// targetCell.getCTTc().setTcPr(copyCell.getCTTc().getTcPr()); +// if (copyCell.getParagraphs() != null && copyCell.getParagraphs().size() > 0) { +// targetCell.getParagraphs().get(0).getCTP().setPPr(copyCell.getParagraphs().get(0).getCTP().getPPr()); +// if (copyCell.getParagraphs().get(0).getRuns() != null +// && copyCell.getParagraphs().get(0).getRuns().size() > 0) { +// XWPFRun cellR = targetCell.getParagraphs().get(0).createRun(); +// cellR.setBold(copyCell.getParagraphs().get(0).getRuns().get(0).isBold()); +// } +// } +// } +// +// } +// +// private static void replaceVariable(XWPFDocument document, String variable, String replacement) { +// for (XWPFParagraph paragraph : document.getParagraphs()) { +// for (XWPFRun run : paragraph.getRuns()) { +// String text = run.getText(0); +// if (text != null && text.contains(variable)) { +// text = text.replace(variable, replacement); +// run.setText(text, 0); +// // run.setFontFamily("Arial"); +// // run.setFontSize(14); +// } +// } +// } +// } +// private static void replaceVariableInTable(XWPFTable table, String variable, String replacement) { +// for (XWPFTableRow row : table.getRows()) { +// for (XWPFTableCell cell : row.getTableCells()) { +// for (XWPFParagraph paragraph : cell.getParagraphs()) { +// for (XWPFRun run : paragraph.getRuns()) { +// String text = run.getText(0); +// if (text != null && text.contains(variable)) { +// text = text.replace(variable, replacement); +// run.setText(text, 0); +// } +// } +// } +// } +// } +// } + } + + + diff --git a/src/main/java/com/teaching/backend/service/impl/records/LearningRecordsServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/records/LearningRecordsServiceImpl.java index 59e6607..e030f6a 100644 --- a/src/main/java/com/teaching/backend/service/impl/records/LearningRecordsServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/records/LearningRecordsServiceImpl.java @@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.teaching.backend.common.ErrorCode; +import com.teaching.backend.exception.BusinessException; import com.teaching.backend.mapper.records.LearningRecordsMapper; import com.teaching.backend.model.entity.records.LearningRecords; @@ -130,7 +132,9 @@ public class LearningRecordsServiceImpl extends ServiceImpl