依赖配置:
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel<artifactId> <version>2.2.0-beta2</version> </dependency>
实体类配置:
@Data public class SubjectEeVo { @ExcelProperty(value = "id" ,index = 0) private Long id; @ExcelProperty(value = "课程分类名称" ,index = 1) private String title; @ExcelProperty(value = "上级id" ,index = 2) private Long parentId; @ExcelProperty(value = "排序" ,index = 3) private Integer sort; }
导入时,需要配置监听器
@Component public class SubjectListener extends AnalysisEventListener<SubjectEeVo> { @Resource private SubjectMapper dictMapper; //一行一行读取 @Override public void invoke(SubjectEeVo subjectEeVo, AnalysisContext analysisContext) { //调用方法添加数据库 Subject subject = new Subject(); BeanUtils.copyProperties(subjectEeVo,subject); dictMapper.insert(subject); } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { } }
导入业务:
@PostMapping("importData") public Result importData(MultipartFile file) { subjectService.importDictData(file); return Result.ok(); } /** * 导入 */ @Override public void importDictData(MultipartFile file) { try { EasyExcel.read(file.getInputStream(), SubjectEeVo.class,subjectListener).sheet().doRead(); } catch (IOException e) { e.printStackTrace(); } }
导出业务:
@GetMapping(value = "/exportData") public void exportData(HttpServletResponse response) { subjectService.exportData(response); } /** * 导出课表 * @param response */ @Override public void exportData(HttpServletResponse response) { try { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("课程分类", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx"); List<Subject> dictList = baseMapper.selectList(null); List<SubjectEeVo> dictVoList = dictList.stream().map(sub -> { SubjectEeVo subjectEeVo = new SubjectEeVo(); BeanUtils.copyProperties(sub, subjectEeVo); return subjectEeVo; }).collect(Collectors.toList()); EasyExcel.write(response.getOutputStream(), SubjectEeVo.class) .sheet("课程分类").doWrite(dictVoList); } catch (IOException e) { e.printStackTrace(); } }
曲线图:
<template> <div class="app-container"> <!--表单--> <el-form :inline="true" class="demo-form-inline"> <el-form-item> <el-date-picker v-model="startDate" type="date" placeholder="选择开始日期" value-format="yyyy-MM-dd" /> </el-form-item> <el-form-item> <el-date-picker v-model="endDate" type="date" placeholder="选择截止日期" value-format="yyyy-MM-dd" /> </el-form-item> <el-button :disabled="btnDisabled" type="primary" icon="el-icon-search" @click="showChart()">查询</el-button> </el-form> <div id="chart" class="chart" style="height:500px;" /> </div> </template> <script> import echarts from 'echarts' import api from '@/api/vod/videoVisitor' export default { data() { return { courseId: '', startDate: '', endDate: '', btnDisabled: false } }, created() { this.courseId = this.$route.params.id // 初始化最近十天数据 let currentDate = new Date(); this.startDate = this.dateFormat(new Date(currentDate.getTime()-7*24*3600*1000)) this.endDate = this.dateFormat(currentDate) this.showChart() }, methods: { showChart() { api.findCount(this.courseId, this.startDate, this.endDate).then(response => { this.setChartData(response.data) }) }, setChartData(data) { // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('chart')) // 指定图表的配置项和数据 var option = { title: { text: '观看课程人数统计' }, xAxis: { data: data.xData }, yAxis: { minInterval: 1 }, series: [{ type: 'line', data: data.yData }] } // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option) }, dateFormat(date) { let fmt = 'YYYY-mm-dd' let ret; const opt = { "Y+": date.getFullYear().toString(), // 年 "m+": (date.getMonth() + 1).toString(), // 月 "d+": date.getDate().toString(), // 日 "H+": date.getHours().toString(), // 时 "M+": date.getMinutes().toString(), // 分 "S+": date.getSeconds().toString() // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 }; for (let k in opt) { ret = new RegExp("(" + k + ")").exec(fmt); if (ret) { fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0"))) }; }; return fmt; } } } </script>
标签:EasyExcel,ret,public,toString,date,data,response From: https://www.cnblogs.com/jessi200/p/17573260.html