Java_EasyExcel_导入_导出Java-js

easyExcel导入

  • 从easyexcel官网中拷贝过来,使用到的,这是使用监听器的方法。
java 复制代码
EasyExcel.read(file.getInputStream(), BaseStoreDataExcelVo.class, new ReadListener<BaseStoreDataExcelVo>() {
    /**
     * 单次缓存的数据量
     */
    public static final int BATCH_COUNT = 100;
    /**
     *临时存储
     */
    private List<BaseStoreDataExcelVo> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    @Override
    public void invoke(BaseStoreDataExcelVo data, AnalysisContext analysisContext) {
        cachedDataList.add(data);
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    /**
     * 存储数据库
     */
    private void saveData() {
        saveAndUpdate(cachedDataList, result);
    }
}).headRowNumber(3).sheet().doRead();
  • 其中headRowNumber是指表头为第三行,数据从第四行开始读取

使用easyExcel导出数据

  • 封装了一个导出的工具类,也是使用easyexcel
java 复制代码
    /**
     * 方法描述: 浏览器点击导出后导出文件
     *
     * @param response 响应
     * @param list  导出数据集合
     * @param fileName 文件名 不含后缀
     * @param clazz 导出数据的数据类型
     * @return void
     */
    public static void exportExcel(HttpServletResponse response, List<?> list,
                                   String fileName, Class<?> clazz) throws IOException {

        if (CollectionUtils.isEmpty(list)) {
            throw new RuntimeException();
        }
        if (StringUtils.isEmpty(fileName)) {
            fileName = new Date().toString();
        }
        String sheetName = fileName;
        // 使用swagger 会有问题,请直接用浏览器或者用postman
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            //防止中文乱码
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

            //表头字体颜色map 1为user中索引位置
            Map<Integer,Short> colorMap=new HashMap<>();
//            colorMap.put(1, IndexedColors.BLUE.index);
            ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), clazz)
                    .registerWriteHandler(widthStyleStrategy)
                    .registerWriteHandler(new XCellStyleUtils(colorMap))
                    .autoCloseStream(Boolean.FALSE).sheet(sheetName)
                    .doWrite(list);
        } catch (Exception e) {
        }
    }
  • 控制层使用
java 复制代码
    @GetMapping("/downloadStoreSelect")
    @ApiOperation("数据下载")
    public void downloadStoreSelect(HttpServletResponse response) {
        Page<BaseStoreData> baseStoreDataPage = storeDataService.selectPage(null);
        try {
            EasyExcelUtils.exportExcel(response, changeData(baseStoreDataPage.getRecords()), "库存.xlsx", BaseStoreDataExcelVo.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

使用easyExcel下载对应前端代码

js 复制代码
  downloadStoreSelect().then(response => {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'export.xlsx'); // 下载文件名
    document.body.appendChild(link);
    link.click();
  })
相关推荐
凯基迪科技33 分钟前
exe软件壳的分类----加密保护壳
java
尘世闲鱼39 分钟前
解数独(C++版本)
开发语言·c++·算法·解数独
wuxuanok1 小时前
Web后端开发-分层解耦
java·笔记·后端·学习
kyle~1 小时前
C/C++字面量
java·c语言·c++
纨妙1 小时前
python打卡day59
开发语言·python
neoooo1 小时前
别慌,Java只有值传递——一次搞懂“为啥我改了它还不变”!
java·后端·spring
秋难降1 小时前
Python 知识 “八股”:给有 C 和 Java 基础的你😁😁😁
java·python·c
wuxuanok1 小时前
Web后端开发-请求响应
java·开发语言·笔记·学习
livemetee2 小时前
spring-ai 1.0.0 (3)交互增强:Advisor 顾问模块
java
DDDDDouble2 小时前
<二>Sping-AI alibaba 入门-记忆聊天及持久化
java·人工智能