From 39eb3505379678a7b977828838f871f3266b8ad3 Mon Sep 17 00:00:00 2001 From: shijie <648688341@qq.com> Date: Mon, 24 Aug 2020 15:10:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=95=E9=A2=98excel=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../msdw/tms/api/QuestionsControllerApi.java | 2 +- .../msdw/tms/common/utils/DownloadUtils.java | 19 +++++++ .../tms/common/utils/poi/ExcelExportUtil.java | 51 +++++++++++++++++-- .../tms/controller/QuestionsController.java | 3 +- .../service/impl/QuestionsServiceImpl.java | 3 ++ .../com/msdw/tms/TmsApplicationTests.java | 9 +++- 6 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/msdw/tms/common/utils/DownloadUtils.java diff --git a/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java b/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java index 670c8cc..e4bc664 100644 --- a/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java +++ b/src/main/java/com/msdw/tms/api/QuestionsControllerApi.java @@ -87,6 +87,6 @@ public interface QuestionsControllerApi { * 通过excel批量导出 */ @ApiOperation(value = "通过excel批量导出", notes = "通过excel批量导出") - R exportQuestion(HttpServletResponse response) throws Exception; + void exportQuestion(HttpServletResponse response) throws Exception; } 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 7c011a5..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,6 +74,52 @@ public class ExcelExportUtil { workbook.write(response.getOutputStream()); } + /** + * 基于注解导出 + * 参数: + * 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++) { diff --git a/src/main/java/com/msdw/tms/controller/QuestionsController.java b/src/main/java/com/msdw/tms/controller/QuestionsController.java index 5d823c9..86b767c 100644 --- a/src/main/java/com/msdw/tms/controller/QuestionsController.java +++ b/src/main/java/com/msdw/tms/controller/QuestionsController.java @@ -151,9 +151,8 @@ public class QuestionsController implements QuestionsControllerApi { @Override @GetMapping("/export") - public R exportQuestion(HttpServletResponse response) throws Exception { + public void exportQuestion(HttpServletResponse response) throws Exception { questionsService.exportQuestion(response); - return R.ok(); } } 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 feba884..049aa69 100644 --- a/src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java +++ b/src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java @@ -429,8 +429,11 @@ public class QuestionsServiceImpl extends ServiceImpl getRandomList(int len, List list) { diff --git a/src/test/java/com/msdw/tms/TmsApplicationTests.java b/src/test/java/com/msdw/tms/TmsApplicationTests.java index 648253d..42125ae 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();