diff --git a/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java b/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java index bf4d9e7..e4bc664 100644 --- a/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java +++ b/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java @@ -82,4 +82,11 @@ public interface QuestionsControllerApi { */ @ApiOperation(value = "抽题测评", notes = "抽题测评") R evaluation (); + + /** + * 通过excel批量导出 + */ + @ApiOperation(value = "通过excel批量导出", notes = "通过excel批量导出") + void exportQuestion(HttpServletResponse response) throws Exception; + } diff --git a/src/main/java/com/msdw/tms/common/utils/Constant.java b/src/main/java/com/msdw/tms/common/utils/Constant.java index 831d575..934830b 100644 --- a/src/main/java/com/msdw/tms/common/utils/Constant.java +++ b/src/main/java/com/msdw/tms/common/utils/Constant.java @@ -218,17 +218,17 @@ public class Constant { /** - * 是否删除 + * 是否删除:0使用,1删除,默认0使用 */ public enum IsDel { /** * 删除 */ - DEL(0, "删除"), + DEL(1, "删除"), /** * 不删除 */ - NOT_DEL(1, "不删除"); + NOT_DEL(0, "不删除"); private Integer type; //类型 private String desc; //描述 diff --git a/src/main/java/com/msdw/tms/common/utils/DownloadUtils.java b/src/main/java/com/msdw/tms/common/utils/DownloadUtils.java new file mode 100644 index 0000000..57c0099 --- /dev/null +++ b/src/main/java/com/msdw/tms/common/utils/DownloadUtils.java @@ -0,0 +1,19 @@ +package com.msdw.tms.common.utils; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +public class DownloadUtils { + public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException { + response.setContentType("application/octet-stream"); + returnName = response.encodeURL(new String(returnName.getBytes(), "iso8859-1")); //保存的文件名,必须和页面编码一致,否则乱码 + response.addHeader("content-disposition", "attachment;filename=" + returnName); + response.setContentLength(byteArrayOutputStream.size()); + ServletOutputStream outputstream = response.getOutputStream(); //取得输出流 + byteArrayOutputStream.writeTo(outputstream); //写到输出流 + byteArrayOutputStream.close(); //关闭 + outputstream.flush(); //刷数据 + } +} \ No newline at end of file diff --git a/src/main/java/com/msdw/tms/common/utils/poi/ExcelExportUtil.java b/src/main/java/com/msdw/tms/common/utils/poi/ExcelExportUtil.java index 6da79eb..d4d9551 100644 --- a/src/main/java/com/msdw/tms/common/utils/poi/ExcelExportUtil.java +++ b/src/main/java/com/msdw/tms/common/utils/poi/ExcelExportUtil.java @@ -1,10 +1,7 @@ package com.msdw.tms.common.utils.poi; import lombok.Data; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.servlet.http.HttpServletResponse; @@ -77,7 +74,53 @@ public class ExcelExportUtil { workbook.write(response.getOutputStream()); } - public CellStyle[] getTemplateStyles(Row row) { + /** + * 基于注解导出 + * 参数: + * response: + * objs:数据 + * fileName:生成的文件名 + */ +// public void export(HttpServletResponse response, List objs, String fileName) throws Exception { +// +// //1.根据模板创建工作簿 +// Workbook workbook = new XSSFWorkbook(); //2007版本 +// +// fileName = URLEncoder.encode(fileName, "UTF-8"); +// +// Sheet sheet = workbook.createSheet(fileName); +// +// //3.提取公共的样式 +// // CellStyle[] styles = getTemplateStyles(sheet.getRow(styleIndex)); +// //4.根据数据创建每一行和每一个单元格的数据2 +// AtomicInteger datasAi = new AtomicInteger(rowIndex); //数字 +// for (T t : objs) { +// //datasAi.getAndIncrement() :获取数字,并++ i++ +// Row row = sheet.createRow(datasAi.getAndIncrement()); +// for (int i = 0; i < styles.length; i++) { +// Cell cell = row.createCell(i); +// cell.setCellStyle(styles[i]); +// for (Field field : fields) { +// if (field.isAnnotationPresent(ExcelAttribute.class)) { +// field.setAccessible(true); +// ExcelAttribute ea = field.getAnnotation(ExcelAttribute.class); +// if (i == ea.sort()) { +// if (field.get(t) != null) { +// cell.setCellValue(field.get(t).toString()); +// } +// } +// } +// } +// } +// } +// +// response.setContentType("application/octet-stream"); +// response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("ISO8859-1"))); +// response.setHeader("filename", fileName); +// workbook.write(response.getOutputStream()); +// } + + CellStyle[] getTemplateStyles(Row row) { CellStyle[] styles = new CellStyle[row.getLastCellNum()]; for (int i = 0; i < row.getLastCellNum(); i++) { styles[i] = row.getCell(i).getCellStyle(); diff --git a/src/main/java/com/msdw/tms/config/CorsConfig.java b/src/main/java/com/msdw/tms/config/CorsConfig.java new file mode 100644 index 0000000..5250d87 --- /dev/null +++ b/src/main/java/com/msdw/tms/config/CorsConfig.java @@ -0,0 +1,19 @@ +package com.msdw.tms.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class CorsConfig implements WebMvcConfigurer { + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("*") + .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS") + .allowCredentials(true) + .maxAge(3600) + .allowedHeaders("*"); + } +} diff --git a/src/main/java/com/msdw/tms/controller/QuestionsController.java b/src/main/java/com/msdw/tms/controller/QuestionsController.java index 036b0de..86b767c 100644 --- a/src/main/java/com/msdw/tms/controller/QuestionsController.java +++ b/src/main/java/com/msdw/tms/controller/QuestionsController.java @@ -149,4 +149,10 @@ public class QuestionsController implements QuestionsControllerApi { return R.ok().put("data", evaluation); } + @Override + @GetMapping("/export") + public void exportQuestion(HttpServletResponse response) throws Exception { + questionsService.exportQuestion(response); + } + } diff --git a/src/main/java/com/msdw/tms/entity/QuestionsEntity.java b/src/main/java/com/msdw/tms/entity/QuestionsEntity.java index 03600ef..20cc204 100644 --- a/src/main/java/com/msdw/tms/entity/QuestionsEntity.java +++ b/src/main/java/com/msdw/tms/entity/QuestionsEntity.java @@ -71,7 +71,7 @@ public class QuestionsEntity implements Serializable { */ private Integer isEnable; /** - * 是否删除:1使用,0删除,默认1使用 + * 是否删除:0使用,1删除,默认0使用 */ private Integer isDel; /** diff --git a/src/main/java/com/msdw/tms/entity/vo/QuestionsDetailVO.java b/src/main/java/com/msdw/tms/entity/vo/QuestionsDetailVO.java index 9d32676..9851391 100644 --- a/src/main/java/com/msdw/tms/entity/vo/QuestionsDetailVO.java +++ b/src/main/java/com/msdw/tms/entity/vo/QuestionsDetailVO.java @@ -89,7 +89,7 @@ public class QuestionsDetailVO implements Serializable { */ private Integer isEnable; /** - * 是否删除:1使用,0删除,默认1使用 + * 是否删除:0使用,1删除,默认0使用 */ private Integer isDel; /** diff --git a/src/main/java/com/msdw/tms/entity/vo/QuestionsVO.java b/src/main/java/com/msdw/tms/entity/vo/QuestionsVO.java index 64a5c14..9c7cf9c 100644 --- a/src/main/java/com/msdw/tms/entity/vo/QuestionsVO.java +++ b/src/main/java/com/msdw/tms/entity/vo/QuestionsVO.java @@ -67,7 +67,7 @@ public class QuestionsVO implements Serializable { */ private Integer isEnable; /** - * 是否删除:1使用,0删除,默认1使用 + * 是否删除:0使用,1删除,默认0使用 */ private Integer isDel; /** diff --git a/src/main/java/com/msdw/tms/service/QuestionsService.java b/src/main/java/com/msdw/tms/service/QuestionsService.java index f3d4544..b1f5fec 100644 --- a/src/main/java/com/msdw/tms/service/QuestionsService.java +++ b/src/main/java/com/msdw/tms/service/QuestionsService.java @@ -9,6 +9,7 @@ import com.msdw.tms.entity.request.QuestionsQueryRequest; import com.msdw.tms.entity.request.QuestionsUpdateRequest; import com.msdw.tms.entity.vo.EvaluationVO; import com.msdw.tms.entity.vo.QuestionsDetailVO; +import com.msdw.tms.entity.vo.QuestionsVO; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; @@ -44,5 +45,7 @@ public interface QuestionsService extends IService { void downloadFiles(HttpServletResponse response) throws IOException; EvaluationVO evaluation(); + + void exportQuestion(HttpServletResponse response) throws Exception; } diff --git a/src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java b/src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java index 6ece057..049aa69 100644 --- a/src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java +++ b/src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java @@ -8,6 +8,7 @@ import com.msdw.tms.common.utils.Constant; import com.msdw.tms.common.utils.FilesResult; import com.msdw.tms.common.utils.PageUtils; import com.msdw.tms.common.utils.Query; +import com.msdw.tms.common.utils.poi.ExcelExportUtil; import com.msdw.tms.common.utils.poi.ExcelImportUtil; import com.msdw.tms.dao.QuestionsDao; import com.msdw.tms.entity.QuestionsEntity; @@ -27,12 +28,14 @@ import com.msdw.tms.service.QuestionsService; import com.msdw.tms.service.XlsxTemplateService; import org.apache.commons.lang.StringUtils; import org.springframework.beans.BeanUtils; +import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; +import java.io.FileInputStream; import java.io.IOException; import java.time.Instant; import java.time.LocalDateTime; @@ -278,7 +281,8 @@ public class QuestionsServiceImpl extends ServiceImpl list = new ExcelImportUtil(QuestionsImportRequest.class).readExcel(file.getInputStream(), Constant.STARTING_ROW, Constant.STARTING_CELL); + List list = new ExcelImportUtil(QuestionsImportRequest.class) + .readExcel(file.getInputStream(), Constant.STARTING_ROW, Constant.STARTING_CELL); if (list == null || list.size() == 0) { ExceptionCast.cast(CommonCode.INVALID_PARAM); @@ -413,6 +417,25 @@ public class QuestionsServiceImpl extends ServiceImpl list = this.list().stream().map(item -> { + QuestionsImportRequest importRequest = new QuestionsImportRequest(); + BeanUtils.copyProperties(item, importRequest); + return importRequest; + }).collect(Collectors.toList()); + + //2.加载模板流数据 + org.springframework.core.io.Resource resource = new ClassPathResource("excel-template/试题导入模板.xlsx"); + FileInputStream fis = new FileInputStream(resource.getFile()); + + //3、通过工具类下载文件 + new ExcelExportUtil(QuestionsImportRequest.class, Constant.STARTING_ROW, Constant.STARTING_CELL). + export(response, fis, list, "试题表.xlsx"); + + } + private Set getRandomList(int len, List list) { Set set = new HashSet<>(); Random random = new Random(); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8a03b75..a3214e8 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,6 +1,6 @@ spring: datasource: - url: jdbc:mysql://www.liuwanr.cn:3306/msdw_tms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai + url: jdbc:mysql://www.liuwanr.cn:3306/huoran?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai username: super password: huoran888 driver-class-name: com.mysql.jdbc.Driver diff --git a/src/main/resources/excel-template/试题导入模板.xlsx b/src/main/resources/excel-template/试题导入模板.xlsx index 30c5e14..7cc7230 100644 Binary files a/src/main/resources/excel-template/试题导入模板.xlsx and b/src/main/resources/excel-template/试题导入模板.xlsx differ diff --git a/src/test/java/com/msdw/tms/TmsApplicationTests.java b/src/test/java/com/msdw/tms/TmsApplicationTests.java index b6f6ef6..61960a6 100644 --- a/src/test/java/com/msdw/tms/TmsApplicationTests.java +++ b/src/test/java/com/msdw/tms/TmsApplicationTests.java @@ -3,13 +3,18 @@ package com.msdw.tms; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; + @SpringBootTest class TmsApplicationTests { @Test - void contextLoads() { + void contextLoads() throws UnsupportedEncodingException { + + System.out.println(URLDecoder.decode("%E8%AF%95%E9%A2%98%E8%A1%A8", "UTF-8")); - printMsg(1, 2, 3, "哈哈哈"); + //printMsg(1, 2, 3, "哈哈哈"); // ------------------------------------------------------------------------------------------ // QuestionsVO questionsVO = new QuestionsVO();