easypoi export excel file
1, showing the direct implementation: controller layer
@GetMapping ( "/ downloadByRoster" ) public R downLoadTemplateByRoster (Integer sccId, HttpServletResponse the Response, the HttpServletRequest Request) throws Exception { // get the template String excelName = "xxxx xx month in a single system template xx batch .xls" ; TemplateExportParams params = new new TemplateExportParams ( "excel / xxxx xx month in a single system template xx batch .xls" ); // set the template sheetName // params.setSheetName ( "payment schedule"); // set to the middle of the template HashMap <String, Object> = the Map new new HashMap<String, Object>(); List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
//此处数据根据项目情况获得 List<RosterVO> list = rosterDao.findRosterByMobiles(sccId); for (RosterVO roster : list) { HashMap<String, String> lm = new HashMap<String, String>(); lm.put("pdContractor", roster.getRsContractor()); lm.put("pdIdCard", roster.getRsIdCard()); lm.put("pdBankName", roster.getRsBankName()); lm.put("pdBankAccount", roster.getRsBankAccount()); lm.put("pdMobile", roster.getRsMobile()); listMap.add(lm); } map.put("maplist", listMap); // 获取excel Workbook workbook = ExcelExportUtil.exportExcel(params, map); ExcelUtil.exportExcel("excel", workbook, request, response);
2, excel import and export tools: here only to see the need to get the template excel (workbook) according to the export map collections
package io.renren.common.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.text.SimpleDateFormat;import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.web.multipart.MultipartFile; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.ImportParams; import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; /** * @program: ivvdata-security * @description: 文件导入导出工具 * @Author {: HYJ * @Create: 2019-09-25 14:16 * / public class ExcelUtil { / ** * obtained Workbook object * * @param File * @return * @throws IOException * / public static Workbook GetWorkbook (a MultipartFile File) throws {IOException // write excel compatible with 03 and 07 InputStream IS = file.getInputStream (); Workbook HSSFWorkbook = null ; the try HSSFWorkbook = new new HSSFWorkbook (IS); } the catch (Exception EX) { IS = file.getInputStream (); HSSFWorkbook = new new XSSFWorkbook (IS); } return HSSFWorkbook; } / ** * need to obtain templates excel (workbook according to the map set ) export * @param fileName * @param Workbook * @param Request * @param the Response * @throws IOException * / public static voidexportExcel (String fileName, Workbook Workbook, Request the HttpServletRequest, HttpServletResponse the Response) throws IOException { response.reset (); response.setContentType ( "multipart / form-the Data" ); // when addressing the Chinese file name garbled download and download because the path Chinese file name contains garbled, suggesting no file String = userAgent request.getHeader ( "the User-Agent" ); // for IE or IE as the core browser: IF (userAgent.contains ( "MSIE") | | userAgent.contains ( "Trident" )) { fileName = java.net.URLEncoder.encode (fileName, "UTF-. 8" ); } The else non IE browser process:{ // fileName = new new String (fileName.getBytes ( "UTF-. 8"), "the ISO-8859-1" ); } // Analyzing excel file types, download the acquired template and rename IF (workbook.getClass ( ) .getSimpleName (.) the equals ( "HSSFWorkbook" )) { response.setHeader ( "the Content-Disposition" , "Attachment; filename =" + new new String (fileName.getBytes ( "UTF-. 8"), "ISO8859-1" ) + "* .xls" ); } the else { response.setHeader ( "the Content-Disposition" , "Attachment;filename=" + newString (fileName.getBytes ( "UTF-. 8"), "ISO8859-1") + ".xlsx" ); } // output stream workbook.write (response.getOutputStream ()); } / ** * Template does not require export excel annotation mode * @param exportParams * @param fileName * @param pojoClass * @param List * @param the Response * @throws IOException * / public static void exportExcel (exportParams exportParams, fileName String, Class <?> pojoClass, List <?> list, HttpServletResponse the Response) throws IOException { // generates a workbook and deriving the Workbook workbook = ExcelExportUtil.exportExcel (exportParams, pojoClass, List); response.reset (); // Clear the output stream the response.setContentType ( "multipart / form-Data" ); System.out.println (workbook.getClass () getSimpleName ().); // download excel and rename and judge xlsx format or xls format IF (workbook.getClass () getSimpleName () the equals ( "HSSFWorkbook.". ) ) { response.setHeader ( "the Content-Disposition" , "Attachment; filename ="+ newString (fileName.getBytes ( "UTF-. 8"), "ISO8859-1") + "* .xls" xls"); } the else { response.setHeader ( "the Content-Disposition" , "Attachment; filename =" + new new String (fileName.getBytes ( "UTF-. 8"), "ISO8859-1") + ".xlsx" ); } // output stream workbook.write (response.getOutputStream ()); } / ** * derived template does not need to excel embodiment annotations ,Uploaded to the server, returns download path * @param exportParams * @param fileName * @param pojoClass * @param List *@param the Response * @throws IOException * / public static String exportExcel (ExportParams exportParams, fileName String, Class <?> pojoClass, List <?> List, HttpServletResponse the Response, the HttpServletRequest Request) throws IOException { // generate workbook and export Workbook workbook = ExcelExportUtil.exportExcel (exportParams, pojoClass, List); // define the path of the uploaded file stored String path1 = Request.getSession () GetServletContext () the getRealPath ( "/ temporaryUploadExcel /.". ); // definition file upload path Folder name file = Folder new new File (path1); // detect whether the folder is a folder that is not created IF (! Folder.isDirectory ()) { folder.mkdirs (); } // download excel and rename and judge xlsx format or xls format IF (workbook.getClass ( .) .getSimpleName () the equals ( "HSSFWorkbook" )) { String excelName = fileName + "* .xls" ; // written to workbook file stream and uploaded to the server path1: upload file storage path excel: folder name excelName: file name FileOutputStream FileOutputStream = new new FileOutputStream (path1 + excelName); workbook.write (FileOutputStream); String path = request.getScheme () + ": //" + to request.getServerName () + ":" +to request.getServerPort () + "/ ivvdata-ADMIN" + "/ temporaryUploadExcel /" + excelName; return path; } the else { String excelName = fileName + "* .xls" ; // written to the stream files uploaded to the server and path1 : Upload file storage path excel: folder name excelName: file name FileOutputStream FileOutputStream = new new FileOutputStream (path1 + excelName); workbook.write (FileOutputStream); String path = request.getScheme () + ": //" + to request.getServerName () + ":" +to request.getServerPort () + "/ ivvdata-ADMIN" + "/ temporaryUploadExcel /" + excelName; System.out.println ( "Get path" + path); return path; } } / * * All methods below according to its own choice processing use, the presence and the above processing through a method consistent * / public static void exportExcel (List <?> List, String title, String sheetName, Class <?> pojoClass, String fileName, Boolean isCreateHeader, the HttpServletResponse Response) { exportParams exportParams = new new ExportParams (title, sheetName); exportParams.setCreateHeadRows (isCreateHeader); defaultExport(list, pojoClass, fileName, response, exportParams); } public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) { defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName)); } public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) { defaultExport(list, fileName, response); } private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) { Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); if (workbook != null); downLoadExcel(fileName, response, workbook); } private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) { try { response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); } catch (IOException e) { // throw new NormalException(e.getMessage()); } } private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) { Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); if (workbook != null); downLoadExcel(fileName, response, workbook); } public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) { if (StringUtils.isBlank(filePath)) { return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list = null; try { list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); } catch (NoSuchElementException e) { // throw new NormalException("模板不能为空"); } catch (Exception e) { e.printStackTrace(); // throw new NormalException(e.getMessage()); } return list; } public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) { if (file == null) { return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list = null; try { list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params); } catch (NoSuchElementException e) { // throw new NormalException("excel文件不能为空"); } catch (Exception e) { // throw new NormalException(e.getMessage()); System.out.println(e.getMessage()); } return list; } }
3, template Source: resources folder under the newly created folder excel at engineering
4, most importantly, how to set up the template in the end, reference http://easypoi.mydoc.io/ in Excel template version