使用SpringBoot+EasyExcel+Vue实现excel表格的导入和导出详解

yizhihongxing

使用SpringBoot+EasyExcel+Vue实现excel表格的导入和导出,主要步骤如下:

1.引入EasyExcel依赖

在pom.xml中引入EasyExcel依赖:

<!-- 引入EasyExcel -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>easyexcel</artifactId>
  <version>2.2.10</version>
</dependency>

2.编写导入和导出接口

导入

使用EasyExcel,我们只需要编写读取Excel文件的逻辑即可。在SpringBoot中,我们可以使用@PostMapping注解将上传的文件读取,并将内容写入对应的实体类中。

@RestController
public class ExcelController {
  @PostMapping("/import")
  public void importExcel(MultipartFile file) throws IOException {
    List<ExcelData> dataList = EasyExcel.read(file.getInputStream()).head(ExcelData.class).sheet().doReadSync();
    // ....
  }
}

导出

同样的,我们使用EasyExcel的write方法来导出Excel文件。以下示例中,我们创建了一个名为ExcelData的实体类,其中包含了导出数据的字段。

@RestController
public class ExcelController {
  @GetMapping("/export")
  public void exportExcel(HttpServletResponse response) throws IOException {
    // 创建数据
    List<ExcelData> dataList = new ArrayList<>();
    ExcelData data1 = new ExcelData("张三", 18, "女");
    ExcelData data2 = new ExcelData("李四", 20, "男");
    dataList.add(data1);
    dataList.add(data2);

    // 设置Response Header
    response.setContentType("application/vnd.ms-excel;");
    response.setCharacterEncoding("utf-8");
    response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode("data.xlsx", "utf-8"));

    // 写入Excel文件
    ExcelWriter writer = EasyExcel.write(response.getOutputStream(), ExcelData.class).build();
    writer.write(dataList, EasyExcel.writerSheet("sheet1").build());
    writer.finish();
  }
}

3.编写前端页面

使用Vue来编写前端页面,需要引入axios库来进行文件上传和下载。

上传文件

在Vue页面中,我们可以使用el-upload组件来实现文件上传。以下示例中,我们编写了一个名为Import的组件,用于上传Excel文件。

<template>
  <el-card>
    <el-upload :show-file-list="false" :before-upload="beforeUpload" :on-success="afterImport">
      <el-button slot="trigger" size="small">导入数据</el-button>
    </el-upload>
  </el-card>
</template>

<script>
import axios from 'axios';
export default {
  methods: {
    beforeUpload(file) {
      this.form.file = file;
    },
    afterImport(response) {
      this.$message.success('导入成功');
    }
  }
}
</script>
export default {
  data() {
    return {
      form: {
        file: null
      }
    };
  },
  methods: {
    submit() {
      let formData = new FormData();
      formData.append('file', this.form.file);
      axios.post('/import', formData).then(response => {
        this.$message.success('导入成功');
      }).catch(error => {
        this.$message.error('导入失败');
      });
    }
  }
}

下载文件

同样的,我们可以使用el-button组件来实现文件下载。以下示例中,我们编写了一个名为Export的组件,用于下载Excel文件。

<template>
  <el-card>
    <el-button type="primary" icon="el-icon-download" :loading="loading" @click="download">导出数据</el-button>
  </el-card>
</template>

<script>
import axios from 'axios';
export default {
  data() {
    return {
      loading: false
    };
  },
  methods: {
    download() {
      this.loading = true;
      axios.get('/export', { responseType: 'blob' }).then(response => {
        let filename = response.headers['content-disposition'].split(';')[1].split('=')[1];
        let blob = new Blob([response.data], { type: 'application/vnd.ms-excel' });
        let link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = filename;
        link.click();
      }).catch(error => {
        this.$message.error('导出失败');
      }).finally(() => {
        this.loading = false;
      });
    }
  }
}
</script>

以上即为使用SpringBoot+EasyExcel+Vue实现excel表格的导入和导出的完整攻略。具体示例代码可以参考以下两个链接:

  • SpringBoot+EasyExcel导入Excel示例:https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java
  • Vue+axios实现文件上传和下载示例:https://www.cnblogs.com/djh-sky/p/10408720.html

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:使用SpringBoot+EasyExcel+Vue实现excel表格的导入和导出详解 - Python技术站

(0)
上一篇 2023年5月28日
下一篇 2023年5月28日

相关文章

  • vue中v-for 循环对象中的属性

    当你需要在Vue中使用v-for指令循环一个列表时,有时可能需要访问遍历对象中的属性。以下是一个基本的示例: <ul> <li v-for="item in items">{{ item }}</li> </ul> 在上面的示例中,我们访问了items列表中的每个元素,并将它们渲染为一个li…

    Vue 2023年5月28日
    00
  • 详解Vue3 中的计算属性及侦听器

    详解Vue3 中的计算属性及侦听器 计算属性 计算属性是Vue中一种非常常见的属性类型,可以根据其他数据的变化而自动更新。在Vue3中,计算属性的写法有所变化,需要使用computed方法来定义。 <template> <div> <p>商品价格: {{ price }}</p> <p>打折后价格:…

    Vue 2023年5月28日
    00
  • 浅析vue 函数配置项watch及函数 $watch 源码分享

    浅析 Vue 函数配置项 watch 及函数 $watch 在 Vue.js 中,watch 是一个非常重要的选项。它可以监听数据的变化,从而触发相应的逻辑。本文将从两个方面来介绍 watch:函数配置项 watch 和函数 $watch 的源码分享。 函数配置项 watch watch 是一个对象,它的属性是要监听的数据的名称,值是一个对象或函数。如果值是…

    Vue 2023年5月29日
    00
  • Vue3中的setup语法糖、computed函数、watch函数详解

    当然,下面是详细讲解”Vue3中的setup语法糖、computed函数、watch函数详解”的完整攻略。 Vue3中的setup语法糖 Vue 3中,通过setup函数,我们能够更加灵活地组合组件逻辑,并且避免在组件外部缩小组件范围。同时,setup函数也为我们提供了更多的代码执行选项,使得我们能够在更多的场景下使用Vue。 下面是一个HelloWorld…

    Vue 2023年5月28日
    00
  • Vue生命周期中的八个钩子函数相机

    Vue生命周期中的八个钩子函数是Vue组件在创建、挂载、更新、销毁过程中执行的钩子函数。这些钩子函数在Vue组件中起到了重要的作用,以便开发者在这些组件生命周期的不同时期进行不同的操作。这八个钩子函数分别是: beforeCreate:在Vue实例被创建后,数据观测 (data observer) 和 event/watcher 事件配置之前被调用。 cre…

    Vue 2023年5月28日
    00
  • Vue将页面导出为图片或者PDF

    如果想将 Vue 页面导出为图片或者 PDF,可以使用第三方库html2canvas和jspdf。下面是基本的步骤: 安装依赖库 npm install html2canvas jspdf –save 导入依赖库 import html2canvas from ‘html2canvas’; import jsPDF from ‘jspdf’; 编写导出函数…

    Vue 2023年5月27日
    00
  • Vue绑定对象与数组变量更改后无法渲染问题解决

    当使用Vue绑定对象或数组变量时,如果这些变量在Vue实例创建后被修改,Vue可能无法监测到这些变化,导致无法渲染。 解决这个问题的方法是使用Vue提供的特定方法,以便Vue可以正确地检测到变量的更改。 针对对象变量,我们可以使用Vue.set()方法或vm.$set()方法。这些方法都接受三个参数:对象本身,属性名和新属性值。下面是一个示例说明: <…

    Vue 2023年5月28日
    00
  • vue使用pdf.js预览pdf文件的方法

    下面是“Vue使用PDF.js预览PDF文件的方法”的完整攻略。 步骤一:安装PDF.js库 首先,我们需要在我们的Vue项目中安装pdfjs-dist依赖: npm install pdfjs-dist –save 步骤二:加载PDF.js文件 在我们的Vue组件中,加载PDF.js文件: <template> <div> &lt…

    Vue 2023年5月28日
    00
合作推广
合作推广
分享本页
返回顶部