使用springboot和vue3以及EasyExcel做导出数据(复用)

@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");
        //response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");

        // 查询数据库中的数据
        List<Category> categoryList = categoryMapper.selectAll();
        List<CategoryExcelVo> categoryExcelVoList = new ArrayList<>(categoryList.size());

        // 将从数据库中查询到的Category对象转换成CategoryExcelVo对象
        for(Category category : categoryList) {
            CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
            BeanUtils.copyProperties(category, categoryExcelVo, CategoryExcelVo.class);
            categoryExcelVoList.add(categoryExcelVo);
        }

        // 写出数据到浏览器端
        EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class).sheet("分类数据").doWrite(categoryExcelVoList);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

js

// 导出方法
export const ExportCategoryData = () => {
  return request({
    url: `${api_name}/exportData`,
    method: 'get',
    responseType: 'blob'  // // 这里指定响应类型为blob类型,二进制数据类型,用于表示大量的二进制数据
  })
}

界面

<div class="tools-div">
    <el-button type="success" size="small" @click="exportData">导出</el-button>
    <el-button type="primary" size="small" >导入</el-button>
</div>

<script setup>
import { FindCategoryByParentId , ExportCategoryData} from '@/api/category.js'

const exportData = () => {
  // 调用 ExportCategoryData() 方法获取导出数据
  ExportCategoryData().then(res => {
      // 创建 Blob 对象,用于包含二进制数据
      const blob = new Blob([res]);             
      // 创建 a 标签元素,并将 Blob 对象转换成 URL
      const link = document.createElement('a'); 
      link.href = window.URL.createObjectURL(blob);
      // 设置下载文件的名称
      link.download = '分类数据.xlsx';
      // 模拟点击下载链接
      link.click();
  })  
}
</script>

相关推荐

  1. springboot使用EasyExcel实现Excel导入导出

    2024-03-23 05:34:01       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-23 05:34:01       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-23 05:34:01       74 阅读
  3. 在Django里面运行非项目文件

    2024-03-23 05:34:01       62 阅读
  4. Python语言-面向对象

    2024-03-23 05:34:01       72 阅读

热门阅读

  1. 在 Swift 中, enumerated() 有哪些常用的使用方式 ?

    2024-03-23 05:34:01       43 阅读
  2. nodejs的线程模型和libuv库的基本使用

    2024-03-23 05:34:01       38 阅读
  3. css的background详解

    2024-03-23 05:34:01       36 阅读
  4. Redis(Remote Dictionary Server)

    2024-03-23 05:34:01       33 阅读
  5. 【智能计算系统】神经网络基础&代码实现

    2024-03-23 05:34:01       37 阅读
  6. jupyter | mac jupyter快捷键

    2024-03-23 05:34:01       34 阅读
  7. 云原生相关概念(小白版)

    2024-03-23 05:34:01       32 阅读
  8. 掌握ChatGPT:如何用AI撰写高质量论文

    2024-03-23 05:34:01       39 阅读