ECharts与Excel的结合实战

引言:本文是一篇ECharts和Excel实战的记录。将Excel与ECharts产生火花,从Excel读取数据然后在ECharts上展示。

1.柱状图前端代码




    
    echarts-bar
    
    
    



2.饼图前端代码




  
  echarts-pie
  
  


3.后端通用代码

3.1生成Excel表格

 @RequestMapping(value = "excel", method = RequestMethod.GET)
    public R getExcel(){
        File file = new File(path);
        
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.select("name", "count");
        List list = dishService.list(wrapper);
        ArrayList> rows = new ArrayList<>();
        for (Dish dish : list) {
            Map row = new LinkedHashMap<>();
            row.put("菜品", dish.getName());
            row.put("销量", dish.getCount());
            rows.add(row);
        }

        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter(path);
        // 合并单元格后的标题行,使用默认标题样式
        writer.merge(1 ,"菜品销量统计");
        // 一次性写出内容,使用默认样式,强制输出标题
        writer.write(rows,true);
        // 关闭writer,释放内存
        writer.close();
        r.setCode(1);
        r.setData("文件导出成功");
        return r;
    }

3.2Excel表格数据导入ECharts

        public static final String path = "D:\\java\\excel\\test.xlsx"; 
       
        File file = new File(path);

        FileInputStream input = new FileInputStream(file);

        MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
        // 1.获取上传文件输入流
        InputStream inputStream = null;

        inputStream = multipartFile.getInputStream();

        // 2.应用HUtool ExcelUtil获取ExcelReader指定输入流和sheet
        ExcelReader excelReader = ExcelUtil.getReader(inputStream, "sheet1");
        // 可以加上表头验证
        // 3.读取第二行到最后一行数据
        //List> read = excelReader.read(1, excelReader.getRowCount());
        return excelReader.readAll();

你可能感兴趣的:(echarts,excel,前端,java,spring,spring,boot,jvm)