演示灵活导出数据
🎉🎉🎉🎉🎉🎉【小吴小吴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.wu2020</groupId>
<artifactId>wu-easy-excel-starter</artifactId>
<version>1.2.6-JDK17-SNAPSHOT</version>
</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.snapshots</id>
<name>oss.sonatype.org</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>