springboot 数据可视化

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot 数据可视化

妙乌   2022-09-27 我要评论

前言:

数据可视化就是将数据转换成图或表等,以一种更直观的方式展现和呈现数据。通过“可视化”的方式,我们看不懂的数据通过图形化的手段进行有效地表达,准确高效、简洁全面地传递某种信息,甚至我们帮助发现某种规律和特征,挖掘数据背后的价值。现在,提出一种方案,基于springboot框架,将excel表格中的数据提取出来,前端使用echarts框架,通过柱形图和饼状图对数据进行直观展示

Excel数据源展示

创建Registration.xlsx表格和fruit_sales页面,同时创建相关水果销售数据

在这里插入图片描述

结构一览

在这里插入图片描述

⚽️ echarts,min.js下载链接

一、读取Excel表格中的数据

本项目使用springboot整合hutool第三方类库实现对excel文件中数据采用流的方式进行读取,详情参看hutool官方文档

Hutool官方文档链接

1.1 创建springboot工程,导入相关依赖

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>

1.2 创建测试类TestExcel

package com.allin.data_view;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.apache.poi.util.IOUtils;
import org.junit.Test;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestExcel {

    @Test
    public  void test() throws Exception{
        File file = new File("D:\\2022learn\\springboot-test\\springboot-office\\data_view\\Registration.xlsx");

        FileInputStream input = new FileInputStream(file);

        MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
        // 1.获取上传文件输入流
        InputStream inputStream = null;
        try{
            inputStream = multipartFile.getInputStream();
        }catch (Exception e){
        }
        // 2.应用HUtool ExcelUtil获取ExcelReader指定输入流和sheet
        ExcelReader excelReader = ExcelUtil.getReader(inputStream, "fruit_sales");
        // 可以加上表头验证
        // 3.读取第二行到最后一行数据
        //List<List<Object>> read = excelReader.read(1, excelReader.getRowCount());
        List<Map<String,Object>> read = excelReader.readAll();
        for (Map<String,Object> objects : read) {
            Set<String> keys = objects.keySet();
            for(String key:keys){
                System.out.println(key + ":" + objects.get(key));
            }
            System.out.println();
        }
    }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们