init课程标准导出word

master
wenyu441069198 5 months ago
parent 44d79e6e36
commit 78cec55647
  1. 6
      pom.xml
  2. 14
      src/main/java/com/teaching/backend/controller/courses/CoursesController.java
  3. 3
      src/main/java/com/teaching/backend/service/courses/ICoursesService.java
  4. 39
      src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java
  5. BIN
      src/main/resources/templates/courses.docx

@ -155,6 +155,12 @@
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--easypoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.1.3</version>
</dependency>
</dependencies>

@ -19,6 +19,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
/**
@ -100,5 +101,16 @@ public class CoursesController {
return ResultUtils.success("删除成功");
}
/**
* 导出word
* @param response
* @throws Exception
*/
@ApiOperation("导出word")
@GetMapping(value = "/down")
public void test( HttpServletResponse response) throws Exception{
//-------------------------查询课程通过课程id查
String id = "de3100cad98f76be3176dd39aa748a9e";
coursesService.down(response, id);
}
}

@ -8,6 +8,7 @@ import com.teaching.backend.model.entity.courses.Courses;
import com.teaching.backend.model.query.CourseQuery;
import com.teaching.backend.model.vo.courses.CoursesVO;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@ -33,4 +34,6 @@ public interface ICoursesService extends IService<Courses> {
List<CoursesVO> getPagePageSize(int page, int pageSize);
void updateCourse(CoursesDTO coursesDTO);
void down(HttpServletResponse response, String id) throws Exception;
}

@ -1,6 +1,7 @@
package com.teaching.backend.service.impl.courses;
import cn.afterturn.easypoi.word.WordExportUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -27,15 +28,18 @@ 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.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -255,5 +259,34 @@ public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> impl
coursesMapper.updateById(course);
}
@Override
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");//------------------需要模板的地址
//准备导出数据
//查询课程通过课程id查--课程简介
Courses courses = this.getById(id);
System.out.println("课程:"+courses);
Map<String,Object> params = new HashMap<String,Object>();
params.put("name",courses.getName());
params.put("code",courses.getCode());
params.put("nature",courses.getNature());
params.put("credit",courses.getCredit());
params.put("classhour",courses.getClasshours());
params.put("description",courses.getDescription());
//查询课程目标
//根据模板+数据 导出文档
XWPFDocument xwpfDocument = WordExportUtil.exportWord07(templatePath.getPath(), params);
//文件名=课程名+课程标准
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);
}
}

Loading…
Cancel
Save