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();
  })
相关推荐
华仔啊5 分钟前
Java序列化详解:什么情况下必须用它?
java
草莓熊Lotso16 分钟前
【C++】--函数参数传递:传值与传引用的深度解析
c语言·开发语言·c++·其他·算法
Ice__Cai22 分钟前
Flask 路由详解:构建灵活的 URL 映射系统
开发语言·python·flask
l1t44 分钟前
DeepSeek辅助编写的将xlsx格式文件中sheet1.xml按需分别保留或去掉标签的程序
xml·python·excel·wps·xlsx
BillKu1 小时前
Spring Boot Controller 使用 @RequestBody + @ModelAttribute 接收请求
java·spring boot·后端
l1t1 小时前
分析xml标签属性和压缩级别对xlsx文件读取解析的影响
xml·开发语言·python·sql·duckdb
Jenkinscao1 小时前
我从零开始学习C语言(13)- 循环语句 PART2
c语言·开发语言·学习
王伯爵1 小时前
go语言中的select的用法和使用场景
开发语言·数据库·golang
Chandler_Song2 小时前
【Python代码】谷歌专利CSV处理函数
开发语言·python·pandas
chenglin0162 小时前
C#_接口设计:角色与契约的分离
java·前端·c#