zoukankan      html  css  js  c++  java
  • EasyExcel Demo

    实体类

        /**
         * 排班日期,主键
         */
        @TableId
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
        @ExcelProperty(value = "值班日期",converter = LocalDateConverter.class)
        private LocalDate dutyTime;
    
        /**
         * 值班人员id
         */
        @ExcelProperty("值班人员id")
        private String dutyUser;
    
        /**
         * 播音人员id
         */
        @ExcelProperty("播音人员id")
        private String broadcastUser;
    
        /**
         * 是否节假日,1是,0否
         */
        @ExcelProperty("是否节假日")
        private Boolean holiday;
    

    类型转换

    public class LocalDateConverter implements Converter<LocalDate> {
    
        @Override
        public Class<LocalDate> supportJavaTypeKey() {
            return LocalDate.class;
        }
    
        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return CellDataTypeEnum.STRING;
        }
    
        @Override
        public LocalDate convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
            return LocalDateTimeUtil.parseDate(cellData.getStringValue(), "yyyy-MM-dd");
        }
    
        @Override
        public CellData<String> convertToExcelData(LocalDate localDate, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
            return new CellData<>(LocalDateTimeUtil.format(localDate, "yyyy-MM-dd"));
        }
    }
    

    excel下载

        @ApiOperation("值班情况按时间段导出(值班)")
        @RequestMapping(value = "excel", method = RequestMethod.GET)
        public void excel(String startDay, String endDay, HttpServletResponse response) {
            List<InfoScheduling> infoSchedulings = infoSchedulingService.findByTimeRange(startDay, endDay);
            String fileName = "值班情况" + startDay + "至" + endDay;
            ExcelWriter excelWriter = null;
            try {
                excelWriter = EasyExcel.write(getOutputStream(fileName, response), InfoScheduling.class).build();
                WriteSheet writeSheet = EasyExcel.writerSheet("值班情况").build();
                excelWriter.write(infoSchedulings, writeSheet);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (excelWriter != null) {
                    excelWriter.finish();
                }
            }
        }
    
        private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
            try {
                fileName = URLEncoder.encode(fileName, "UTF-8");
                response.setContentType("application/vnd.ms-excel");
                response.setCharacterEncoding("utf8");
                response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
                response.setHeader("Pragma", "public");
                response.setHeader("Cache-Control", "no-store");
                response.addHeader("Cache-Control", "max-age=0");
                return response.getOutputStream();
            } catch (IOException e) {
                throw new Exception("导出excel表格失败!", e);
            }
        }
    
  • 相关阅读:
    UVA
    Codeforces 898F 字符串hash
    牛客练习赛11 B trie树+拓扑判环 E 分治求平面最近点对
    Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp
    2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017) D bfs思维 E bfs I Floyd找最小环 K 二分
    Educational Codeforces Round 37 E 补图求连通块 bfs+链表优化 F 线段树套路
    Codeforces 919E 数论,思维
    大数加法(主要是想记住模板)
    基础图论3
    简单并查集2hdu1213
  • 原文地址:https://www.cnblogs.com/unique1319/p/13841794.html
Copyright © 2011-2022 走看看