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();
  })
相关推荐
资深web全栈开发1 小时前
[特殊字符]图解 Golang 反射机制:从底层原理看动态类型的秘密
开发语言·后端·golang
西岭千秋雪_1 小时前
Zookeeper实现分布式锁
java·分布式·后端·zookeeper·wpf
MarcoPage2 小时前
Python 字典推导式入门:一行构建键值对映射
java·linux·python
脸大是真的好~2 小时前
黑马JAVAWeb-11 请求参数为数组-XML自动封装-XML手动封装-增删改查-全局异常处理-单独异常分别处理
java
Hello.Reader4 小时前
Data Sink定义、参数与可落地示例
java·前端·网络
2401_837088505 小时前
stringRedisTemplate.opsForHash().entries
java·redis
独隅6 小时前
在 Lua 中,你可以使用 `os.date()` 函数轻松地将时间戳转换为格式化的时间字符串
开发语言·lua
一晌小贪欢6 小时前
【Html模板】电商运营可视化大屏模板 Excel存储 + 一键导出(已上线-可预览)
前端·数据分析·html·excel·数据看板·电商大屏·大屏看板
思麟呀6 小时前
Linux的基础IO流
linux·运维·服务器·开发语言·c++
星释6 小时前
Rust 练习册 :Pythagorean Triplet与数学算法
开发语言·算法·rust