试题excel导出

hehai
shijie 4 years ago
parent d9c26dcc37
commit 39eb350537
  1. 2
      src/main/java/com/msdw/tms/api/QuestionsControllerApi.java
  2. 19
      src/main/java/com/msdw/tms/common/utils/DownloadUtils.java
  3. 51
      src/main/java/com/msdw/tms/common/utils/poi/ExcelExportUtil.java
  4. 3
      src/main/java/com/msdw/tms/controller/QuestionsController.java
  5. 3
      src/main/java/com/msdw/tms/service/impl/QuestionsServiceImpl.java
  6. 9
      src/test/java/com/msdw/tms/TmsApplicationTests.java

@ -87,6 +87,6 @@ public interface QuestionsControllerApi {
* 通过excel批量导出 * 通过excel批量导出
*/ */
@ApiOperation(value = "通过excel批量导出", notes = "通过excel批量导出") @ApiOperation(value = "通过excel批量导出", notes = "通过excel批量导出")
R exportQuestion(HttpServletResponse response) throws Exception; void exportQuestion(HttpServletResponse response) throws Exception;
} }

@ -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(); //刷数据
}
}

@ -1,10 +1,7 @@
package com.msdw.tms.common.utils.poi; package com.msdw.tms.common.utils.poi;
import lombok.Data; import lombok.Data;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.*;
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.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -77,6 +74,52 @@ public class ExcelExportUtil<T> {
workbook.write(response.getOutputStream()); workbook.write(response.getOutputStream());
} }
/**
* 基于注解导出
* 参数
* response
* objs数据
* fileName生成的文件名
*/
// public void export(HttpServletResponse response, List<T> 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[] getTemplateStyles(Row row) {
CellStyle[] styles = new CellStyle[row.getLastCellNum()]; CellStyle[] styles = new CellStyle[row.getLastCellNum()];
for (int i = 0; i < row.getLastCellNum(); i++) { for (int i = 0; i < row.getLastCellNum(); i++) {

@ -151,9 +151,8 @@ public class QuestionsController implements QuestionsControllerApi {
@Override @Override
@GetMapping("/export") @GetMapping("/export")
public R exportQuestion(HttpServletResponse response) throws Exception { public void exportQuestion(HttpServletResponse response) throws Exception {
questionsService.exportQuestion(response); questionsService.exportQuestion(response);
return R.ok();
} }
} }

@ -429,8 +429,11 @@ public class QuestionsServiceImpl extends ServiceImpl<QuestionsDao, QuestionsEnt
//2.加载模板流数据 //2.加载模板流数据
org.springframework.core.io.Resource resource = new ClassPathResource("excel-template/试题导入模板.xlsx"); org.springframework.core.io.Resource resource = new ClassPathResource("excel-template/试题导入模板.xlsx");
FileInputStream fis = new FileInputStream(resource.getFile()); FileInputStream fis = new FileInputStream(resource.getFile());
//3、通过工具类下载文件
new ExcelExportUtil(QuestionsImportRequest.class, Constant.STARTING_ROW, Constant.STARTING_CELL). new ExcelExportUtil(QuestionsImportRequest.class, Constant.STARTING_ROW, Constant.STARTING_CELL).
export(response, fis, list, "试题表.xlsx"); export(response, fis, list, "试题表.xlsx");
} }
private Set<QuestionsEntity> getRandomList(int len, List<QuestionsEntity> list) { private Set<QuestionsEntity> getRandomList(int len, List<QuestionsEntity> list) {

@ -3,13 +3,18 @@ package com.msdw.tms;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
@SpringBootTest @SpringBootTest
class TmsApplicationTests { class TmsApplicationTests {
@Test @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(); // QuestionsVO questionsVO = new QuestionsVO();

Loading…
Cancel
Save