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;
}
}