1、引入pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bjrhc</groupId>
<artifactId>readExcel</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version> <!-- 根据最新版本进行更新 -->
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
2、创建实体类
如果你不想读sql中的每一个字段,就可以像我这样,加上注解,只声明要读的列:
@Data
public class ExcelPO {
@ExcelProperty("业务对象")
private String obj;
@ExcelProperty("业务部门")
private String bumen;
@ExcelProperty("属性")
private String shuxing;
@ExcelProperty("来源表英文名")
private String tableNameEnglish;
@ExcelProperty("来源表中文名")
private String tableNameChina;
@ExcelProperty("来源字段英文名")
private String ziduan;
}
3、编写工具类
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import lombok.SneakyThrows;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class ExcelListener {
public static void main(String[] args) throws Exception {
//excel路径
String fileName = "doc/excel.xlsx";
//读取
EasyExcel.read(fileName)
//读第0个sheet表
.sheet(0)
//从第二行开始读
.headRowNumber(1)
//对应的实体类
.head(ExcelPO.class)
.registerReadListener(new TransformerDataListener())
.doReadSync();
}
public static class TransformerDataListener implements ReadListener<ExcelPO> {
//用来接受读出来的数据
Map<String, List<Object>> hashMap = new HashMap<>();
@SneakyThrows
@Override
public void invoke(ExcelPO data, AnalysisContext context) {
// 每一行数据在这里处理
if (data.getBumen().equals("选择部门")){
if (data.getObj().contains("选择对象")){
if (data.getShuxing().equals("字段信息") ||
data.getShuxing().equals("字段信息") ||
data.getShuxing().equals("字段信息") ||
data.getShuxing().equals("字段信息") ||
data.getShuxing().equals("字段信息") ||
data.getShuxing().equals("字段信息") ||
data.getShuxing().equals("字段信息") ||
data.getShuxing().equals("字段信息") ||
data.getShuxing().equals("字段信息") ||
data.getShuxing().equals("字段信息") ){
//判断是否已经存储过这张表,如果存储过直接给他的value加上要存储的字段
if (hashMap.containsKey(data.getTableNameEnglish())){
List list = hashMap.get(data.getTableNameEnglish());
list.add(data.getZiduan());
}else {
//如果没有则添加一个新的map
List list1 = new ArrayList<>();
list1.add(data.getZiduan());
hashMap.put(data.getTableNameEnglish(),list1);
}
}
}
};
//创建一个用来存储我们查到并需要的内容的txt
String txt = "doc/text.txt";
File file = new File(txt);
//不存在则创建一个新的txt文档
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//写txt
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for (Map.Entry<String, List<Object>> entry : hashMap.entrySet()) {
String key = entry.getKey();
List<Object> values = entry.getValue();
StringBuilder s = new StringBuilder("select * from "+ key + " where ");
// 输出List中的每个值
for (Object value : values) {
s.append(value+" is null or ");
}
s.replace(s.lastIndexOf("or"), s.lastIndexOf("or")+"or".length(), ";");
bw.write(String.valueOf(s));
bw.newLine();
}
//写操作完成后关闭流
bw.close();
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 所有数据解析完成后的回调
}
// 如果需要处理异常,重写下列方法
@Override
public void onException(Exception exception, AnalysisContext context) {
exception.printStackTrace();
}
}
}
在这里我将生成的sql放入了一个新的txt中,项目目录参考: