packagecom.mtoliv.sps.controller;importjava.io.IOException;importjava.io.OutputStream;importjava.net.URLEncoder;importjava.util.ArrayList;importjava.util.LinkedHashMap;importjava.util.List;importjava.util.Map;importjavax.servlet.http.HttpServletResponse;importorg.apache.commons.io.FilenameUtils;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Workbook;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.http.MediaType;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestPart;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.multipart.MultipartFile;importcom.mtoliv.sps.model.MapImportHanlder;importcn.afterturn.easypoi.excel.ExcelImportUtil;importcn.afterturn.easypoi.excel.entity.ExportParams;importcn.afterturn.easypoi.excel.entity.ImportParams;importcn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;importcn.afterturn.easypoi.excel.export.ExcelExportServer;importio.swagger.annotations.Api;importio.swagger.annotations.ApiOperation;
@RestController
@RequestMapping(value= "/api/v1/excel")
@Api(tags= "导入导出相关操作参考实现 ")public classExcelController {private static final Logger logger = LoggerFactory.getLogger(ExcelController.class);
@GetMapping(value= "/exportExcels", produces =MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ApiOperation(value= "导出数据")public void exportExcels(HttpServletResponse response) throwsIOException {
List entityList = new ArrayList<>();
entityList.add(new ExcelExportEntity("用户ID", "id", 15));
entityList.add(new ExcelExportEntity("用户名", "name", 15));
entityList.add(new ExcelExportEntity("用户年龄", "age", 15));
List> dataResult =getData();
ExcelExportServer server= newExcelExportServer();
Workbook workbook= newHSSFWorkbook();
ExportParams exportParams= newExportParams();
exportParams.setSheetName("用户列表");
server.createSheetForMap(workbook, exportParams, entityList, dataResult);
response.setCharacterEncoding("UTF-8");
String filedisplay= "users.xls";
filedisplay= URLEncoder.encode(filedisplay, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" +filedisplay);
OutputStream out=response.getOutputStream();
workbook.write(out);
out.close();
}
@PostMapping(value= "/importUsers")public void importUsers(@RequestPart(value = "file") MultipartFile file) {
logger.info(file.getOriginalFilename());
String originalFilename=file.getOriginalFilename();
String extension=FilenameUtils.getExtension(originalFilename);
logger.info(extension);if (!"xlsx".equals(extension) && !"xls".equals(extension)) return;
ImportParams params= newImportParams();
params.setDataHanlder(newMapImportHanlder());try{
List> list = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);
logger.info(list.size()+ "");
}catch(Exception e) {
e.printStackTrace();
}
}private List>getData() {
List> dataResult = new ArrayList<>();
Map u1 = new LinkedHashMap<>();
u1.put("id", "1");
u1.put("name", "cyf");
u1.put("age", "21");
Map u2 = new LinkedHashMap<>();
u2.put("id", "2");
u2.put("name", "cy");
u2.put("age", "22");
dataResult.add(u1);
dataResult.add(u2);returndataResult;
}
}