• 【Java 演示灵活导出任意http、https接口数据】


    【Java 演示灵活导出任意http、https接口数据】
    29/100
    发布文章
    qq_22903677
    未选择任何文件
    new

    演示灵活导出数据

    🎉🎉🎉🎉🎉🎉【小吴小吴bug全无开通公众号】关注公众号获取第一时间博客动态

    在这里插入图片描述

    背景今天临时起兴打开稀土掘金导航栏看到页面推广gitee项目恰巧最近也在学习python了解到python爬虫很厉害,想着能不能用Java把数据爬下来,于是在原先框架wu-easy-excel-starter基础上新增demo进行演示测试

    在这里插入图片描述

    实现过程

    获取需要获取数据的接口
    curl 'https://e.juejin.cn/resources/gitee' \
      -H 'accept: */*' \
      -H 'accept-language: zh-CN,zh;q=0.9' \
      -H 'content-type: application/json' \
      --data-raw '{"lang":"java","offset":0,"limit":30,"cursor":"0"}'
    

    ::: 告诉一个你们都不知道的工具ApiPost 里面有个可以一键将curl命令转换成不同语言的代码

    找到生成代码按钮

    在这里插入图片描述

    选择你想要的语言

    在这里插入图片描述

    安装Java wu-easy-excel-starter 依赖

    这里使用的是快照哦(快照仓库地址放在最下面了)

            <dependency>
                <groupId>top.wu2020groupId>
                <artifactId>wu-easy-excel-starterartifactId>
                <version>1.2.6-JDK17-SNAPSHOTversion>
            dependency>
    
    编写代码
    获取接口数据
            HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://e.juejin.cn/resources/gitee"))
            .header("accept", "*/*")
            .header("accept-language", "zh-CN,zh;q=0.9")
            .header("content-type", "application/json")
            .method("POST", HttpRequest.BodyPublishers.ofString("{\"lang\":\"java\",\"offset\":0,\"limit\":30,\"cursor\":\"0\"}"))
            .build();
            HttpResponse<byte[]> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofByteArray());
            byte[] body = response.body();
            
            // 需要导出的数据
            Map<?,?> map = JsonUtils.parseObject(body, LinkedHashMap.class);
    
            System.out.println(map);
    

    输出格式

    {
      "code": 200,
      "data": [
        {
          "id": 13010970,
          "title": "小诺/Snowy",
          "username": "",
          "url": "https://gitee.com/xiaonuobase/snowy",
          "date": {
            "__type": "Date",
            "iso": "2024-05-10T11:45:16.000Z"
          },
          "summary": "最新:💖国内首个国密前后分离快速开发平台💖,采用Vue3+AntDesignVue3 + Vite+SpringBoot+Mp+HuTool+SaToken。集成国密加解密插件,在前后分离框架中,实现前后分离“密”不可分;同时实现国产化机型、中间件、数据库适配,是您的不二之选;最后官网提供工作流、多租户、多数据源、Vue3表单设计器等丰富插件灵活使用。",
          "language": "Java",
          "category": "",
          "img": [],
          "view": 0,
          "comment": 0,
          "like": 0,
          "hot": 0,
          "collect": 0,
          "langColor": "#b07219"
        }
      ]
    }
    
    导出数据参数配置

    配置导出字段

    • 属性数据导出如:user.id
    • 集合数据导出:userList.$id
    • 字段导出:id

    ::: tip 如下导出数据中的data中集合属性ID为数据ID、title属性为标题
    :::

            // 需要导出的数据设置信息
            List<ExportFieldCommand> exportFieldCommands = new ArrayList<>();
            exportFieldCommands.add(new ExportFieldCommand("data.$id","数据ID"));
            exportFieldCommands.add(new ExportFieldCommand("data.$title","标题"));
            exportFieldCommands.add(new ExportFieldCommand("data.$username","用户"));
            exportFieldCommands.add(new ExportFieldCommand("data.$url","地址"));
            exportFieldCommands.add(new ExportFieldCommand("data.$summary","描述"));
            exportFieldCommands.add(new ExportFieldCommand("data.$language","语言"));
            exportFieldCommands.add(new ExportFieldCommand("data.$category","类型"));
    
    
            List<EasyExcelFiledPoint> easyExcelFiledPointList = ExportFieldCommandUtils.exportFieldCommandList2EasyExcelFiledPointList(exportFieldCommands);
    
    
    导出数据
            // 设置导出数据信息
            DynamicEasyExcelContextHolder.pushOnlyExportField(easyExcelFiledPointList);
    //        easyExcelPoint.setExcelFiledPointList(easyExcelFiledPointList);
            // 声明导出文件地址
            FileOutputStream fileOutputStream = new FileOutputStream(easyExcelWorkbookTest.getPath());
    
            // 执行导出
            excelExcelServiceAdapter.exportExcel(map,easyExcelPoint,fileOutputStream);
    
    查看数据

    在这里插入图片描述

    完整代码

    package com.wu.framework.easy;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.wu.framework.core.utils.FileUtil;
    import org.wu.framework.core.utils.JsonUtils;
    import org.wu.framework.easy.excel.adapter.ExcelExcelServiceAdapter;
    import org.wu.framework.easy.excel.endpoint.EasyExcelFiledPoint;
    import org.wu.framework.easy.excel.endpoint.EasyExcelPoint;
    import org.wu.framework.easy.excel.endpoint.ExportFieldCommand;
    import org.wu.framework.easy.excel.factory.ExcelExcelServiceAdapterFactory;
    import org.wu.framework.easy.excel.toolkit.DynamicEasyExcelContextHolder;
    import org.wu.framework.easy.excel.util.EasyWorkbookTest;
    import org.wu.framework.easy.excel.util.ExportFieldCommandUtils;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URI;
    import java.net.http.HttpClient;
    import java.net.http.HttpRequest;
    import java.net.http.HttpResponse;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * 自定义 表头数据导出
     */
    public class CustomerDataExportTest {
    
        public static void main(String[] args) throws IOException, InterruptedException {
    
    
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create("https://e.juejin.cn/resources/gitee"))
                    .header("accept", "*/*")
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("content-type", "application/json")
                    .method("POST", HttpRequest.BodyPublishers.ofString("{\"lang\":\"java\",\"offset\":0,\"limit\":30,\"cursor\":\"0\"}"))
                    .build();
            HttpResponse<byte[]> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofByteArray());
            byte[] body = response.body();
    
            // 需要导出的数据
            Map<?,?> map = JsonUtils.parseObject(body, LinkedHashMap.class);
    
            System.out.println(map);
    
            // 创建一个导出适配器
            ExcelExcelServiceAdapter excelExcelServiceAdapter = ExcelExcelServiceAdapterFactory.excelExcelServiceAdapter();
    
            // 演示导出文件到本地
            String localClassPath = FileUtil.readLocalClassFolder(CustomerDataExportTest.class);
            File easyExcelWorkbookTest = FileUtil.createFile(localClassPath, "CustomerDataExportTest.xls");
            EasyExcelPoint easyExcelPoint = new EasyExcelPoint();
            easyExcelPoint.setSheetName("导出稀土掘金首页Gitee推荐");
            easyExcelPoint.setUseAnnotation(false);// 不使用注解导出
    
    
            // 需要导出的数据设置信息
            List<ExportFieldCommand> exportFieldCommands = new ArrayList<>();
            exportFieldCommands.add(new ExportFieldCommand("data.$id","数据ID"));
            exportFieldCommands.add(new ExportFieldCommand("data.$title","标题"));
            exportFieldCommands.add(new ExportFieldCommand("data.$username","用户"));
            exportFieldCommands.add(new ExportFieldCommand("data.$url","地址"));
            exportFieldCommands.add(new ExportFieldCommand("data.$summary","描述"));
            exportFieldCommands.add(new ExportFieldCommand("data.$language","语言"));
            exportFieldCommands.add(new ExportFieldCommand("data.$category","类型"));
    
    
            List<EasyExcelFiledPoint> easyExcelFiledPointList = ExportFieldCommandUtils.exportFieldCommandList2EasyExcelFiledPointList(exportFieldCommands);
    
            // 设置导出数据信息
            DynamicEasyExcelContextHolder.pushOnlyExportField(easyExcelFiledPointList);
    //        easyExcelPoint.setExcelFiledPointList(easyExcelFiledPointList);
            // 声明导出文件地址
            FileOutputStream fileOutputStream = new FileOutputStream(easyExcelWorkbookTest.getPath());
    
            // 执行导出
            excelExcelServiceAdapter.exportExcel(map,easyExcelPoint,fileOutputStream);
            System.out.println(map);
        }
    }
    
    

    当前使用框架地址

    快照仓库地址

        <repositories>
            <repository>
                <id>oss.snapshotsid>
                <name>oss.sonatype.orgname>
                <url>https://oss.sonatype.org/content/repositories/snapshots/url>
                <releases>
                    <enabled>falseenabled>
                releases>
                <snapshots>
                    <enabled>trueenabled>
                snapshots>
            repository>
        repositories>
    

    演示灵活导出数据
    🎉🎉🎉🎉🎉🎉【小吴小吴bug全无开通公众号】关注公众号获取第一时间博客动态
    在这里插入图片描述

    背景今天临时起兴打开稀土掘金导航栏看到页面推广gitee项目恰巧最近也在学习python了解到python爬虫很厉害,想着能不能用Java把数据爬下来,于是在原先框架wu-easy-excel-starter基础上新增demo进行演示测试
    在这里插入图片描述

    实现过程
    获取需要获取数据的接口
    curl ‘https://e.juejin.cn/resources/gitee’
    -H ‘accept: /
    -H ‘accept-language: zh-CN,zh;q=0.9’
    -H ‘content-type: application/json’
    –data-raw ‘{“lang”:“java”,“offset”:0,“limit”:30,“cursor”:“0”}’
    ::: 告诉一个你们都不知道的工具ApiPost 里面有个可以一键将curl命令转换成不同语言的代码

    找到生成代码按钮
    在这里插入图片描述

    选择你想要的语言
    在这里插入图片描述

    安装Java wu-easy-excel-starter 依赖
    这里使用的是快照哦(快照仓库地址放在最下面了)

        
            top.wu2020
            wu-easy-excel-starter
            1.2.6-JDK17-SNAPSHOT
        
    

    编写代码
    获取接口数据
    HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create(“https://e.juejin.cn/resources/gitee”))
    .header(“accept”, “/”)
    .header(“accept-language”, “zh-CN,zh;q=0.9”)
    .header(“content-type”, “application/json”)
    .method(“POST”, HttpRequest.BodyPublishers.ofString(“{“lang”:“java”,“offset”:0,“limit”:30,“cursor”:“0”}”))
    .build();
    HttpResponse response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofByteArray());
    byte[] body = response.body();

        // 需要导出的数据
        Map map = JsonUtils.parseObject(body, LinkedHashMap.class);
    
        System.out.println(map);
    

    输出格式

    {
    “code”: 200,
    “data”: [
    {
    “id”: 13010970,
    “title”: “小诺/Snowy”,
    “username”: “”,
    “url”: “https://gitee.com/xiaonuobase/snowy”,
    “date”: {
    “__type”: “Date”,
    “iso”: “2024-05-10T11:45:16.000Z”
    },
    “summary”: “最新:💖国内首个国密前后分离快速开发平台💖,采用Vue3+AntDesignVue3 + Vite+SpringBoot+Mp+HuTool+SaToken。集成国密加解密插件,在前后分离框架中,实现前后分离“密”不可分;同时实现国产化机型、中间件、数据库适配,是您的不二之选;最后官网提供工作流、多租户、多数据源、Vue3表单设计器等丰富插件灵活使用。”,
    “language”: “Java”,
    “category”: “”,
    “img”: [],
    “view”: 0,
    “comment”: 0,
    “like”: 0,
    “hot”: 0,
    “collect”: 0,
    “langColor”: “#b07219”
    }
    ]
    }
    导出数据参数配置
    配置导出字段

    属性数据导出如:user.id
    集合数据导出:userList.$id
    字段导出:id
    ::: tip 如下导出数据中的data中集合属性ID为数据ID、title属性为标题
    :::

        // 需要导出的数据设置信息
        List exportFieldCommands = new ArrayList<>();
        exportFieldCommands.add(new ExportFieldCommand("data.$id","数据ID"));
        exportFieldCommands.add(new ExportFieldCommand("data.$title","标题"));
        exportFieldCommands.add(new ExportFieldCommand("data.$username","用户"));
        exportFieldCommands.add(new ExportFieldCommand("data.$url","地址"));
        exportFieldCommands.add(new ExportFieldCommand("data.$summary","描述"));
        exportFieldCommands.add(new ExportFieldCommand("data.$language","语言"));
        exportFieldCommands.add(new ExportFieldCommand("data.$category","类型"));
    
    
        List easyExcelFiledPointList = ExportFieldCommandUtils.exportFieldCommandList2EasyExcelFiledPointList(exportFieldCommands);
    

    导出数据
    // 设置导出数据信息
    DynamicEasyExcelContextHolder.pushOnlyExportField(easyExcelFiledPointList);
    // easyExcelPoint.setExcelFiledPointList(easyExcelFiledPointList);
    // 声明导出文件地址
    FileOutputStream fileOutputStream = new FileOutputStream(easyExcelWorkbookTest.getPath());

        // 执行导出
        excelExcelServiceAdapter.exportExcel(map,easyExcelPoint,fileOutputStream);
    

    查看数据
    在这里插入图片描述

    完整代码
    package com.wu.framework.easy;

    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.wu.framework.core.utils.FileUtil;
    import org.wu.framework.core.utils.JsonUtils;
    import org.wu.framework.easy.excel.adapter.ExcelExcelServiceAdapter;
    import org.wu.framework.easy.excel.endpoint.EasyExcelFiledPoint;
    import org.wu.framework.easy.excel.endpoint.EasyExcelPoint;
    import org.wu.framework.easy.excel.endpoint.ExportFieldCommand;
    import org.wu.framework.easy.excel.factory.ExcelExcelServiceAdapterFactory;
    import org.wu.framework.easy.excel.toolkit.DynamicEasyExcelContextHolder;
    import org.wu.framework.easy.excel.util.EasyWorkbookTest;
    import org.wu.framework.easy.excel.util.ExportFieldCommandUtils;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URI;
    import java.net.http.HttpClient;
    import java.net.http.HttpRequest;
    import java.net.http.HttpResponse;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;

    /**

    • 自定义 表头数据导出
      */
      public class CustomerDataExportTest {

      public static void main(String[] args) throws IOException, InterruptedException {

       HttpRequest request = HttpRequest.newBuilder()
               .uri(URI.create("https://e.juejin.cn/resources/gitee"))
               .header("accept", "*/*")
               .header("accept-language", "zh-CN,zh;q=0.9")
               .header("content-type", "application/json")
               .method("POST", HttpRequest.BodyPublishers.ofString("{\"lang\":\"java\",\"offset\":0,\"limit\":30,\"cursor\":\"0\"}"))
               .build();
       HttpResponse response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofByteArray());
       byte[] body = response.body();
      
       // 需要导出的数据
       Map map = JsonUtils.parseObject(body, LinkedHashMap.class);
      
       System.out.println(map);
      
       // 创建一个导出适配器
       ExcelExcelServiceAdapter excelExcelServiceAdapter = ExcelExcelServiceAdapterFactory.excelExcelServiceAdapter();
      
       // 演示导出文件到本地
       String localClassPath = FileUtil.readLocalClassFolder(CustomerDataExportTest.class);
       File easyExcelWorkbookTest = FileUtil.createFile(localClassPath, "CustomerDataExportTest.xls");
       EasyExcelPoint easyExcelPoint = new EasyExcelPoint();
       easyExcelPoint.setSheetName("导出稀土掘金首页Gitee推荐");
       easyExcelPoint.setUseAnnotation(false);// 不使用注解导出
      
      
       // 需要导出的数据设置信息
       List exportFieldCommands = new ArrayList<>();
       exportFieldCommands.add(new ExportFieldCommand("data.$id","数据ID"));
       exportFieldCommands.add(new ExportFieldCommand("data.$title","标题"));
       exportFieldCommands.add(new ExportFieldCommand("data.$username","用户"));
       exportFieldCommands.add(new ExportFieldCommand("data.$url","地址"));
       exportFieldCommands.add(new ExportFieldCommand("data.$summary","描述"));
       exportFieldCommands.add(new ExportFieldCommand("data.$language","语言"));
       exportFieldCommands.add(new ExportFieldCommand("data.$category","类型"));
      
      
       List easyExcelFiledPointList = ExportFieldCommandUtils.exportFieldCommandList2EasyExcelFiledPointList(exportFieldCommands);
      
       // 设置导出数据信息
       DynamicEasyExcelContextHolder.pushOnlyExportField(easyExcelFiledPointList);
      

    // easyExcelPoint.setExcelFiledPointList(easyExcelFiledPointList);
    // 声明导出文件地址
    FileOutputStream fileOutputStream = new FileOutputStream(easyExcelWorkbookTest.getPath());

        // 执行导出
        excelExcelServiceAdapter.exportExcel(map,easyExcelPoint,fileOutputStream);
        System.out.println(map);
    }
    

    }

    当前使用框架地址
    快照仓库地址


    oss.snapshots
    oss.sonatype.org
    https://oss.sonatype.org/content/repositories/snapshots/

    false


    true



    Markdown 6851 字数 241 行数 当前行 1, 当前列 0HTML 6228 字数 170 段落
    发布博文获得大额流量券

  • 相关阅读:
    Centos MySQL --skip-grant-tables详解
    ssm青少年航天知识科普网站-计算机毕设 附源码59487
    QT中对话框界面的实现以及事件处理机制(核心机制)
    c++ fstream 文件追加模式
    今日事今日毕,推荐五款无广告的免费软件
    NPU架构分析与应用
    甬矽电子在科创板上市:市值达到122亿元,王顺波为实际控制人
    CentOS 7搭建LittlePaimon原神机器人
    #gStore-weekly | gStore应用之交通气象风险评估与预警平台
    十、实现AOP的三种方式
  • 原文地址:https://blog.csdn.net/qq_22903677/article/details/139039356