Adding Maven Dependencies
First, configure the necesary dependencies in you're project's POM file:
<?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.example</groupId>
<artifactId>excelToSqlConverter</artifactId>
<version>1.0.0</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.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Creating Data Model Class
Define an entity class that maps to your Excel columns using annotations. This approach allows you to specify exact which columns to read:
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@Data
public class ExcelRecord {
@ExcelProperty("BusinessObject")
private String businessObject;
@ExcelProperty("Department")
private String department;
@ExcelProperty("Attribute")
private String attribute;
@ExcelProperty("SourceTableEN")
private String sourceTableEN;
@ExcelProperty("SourceTableCN")
private String sourceTableCN;
@ExcelProperty("FieldEN")
private String fieldEN;
}
Implementing the Processing Utility
Create a utility class that reads Excel files and generates SQL queries based on the extracted data:
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import lombok.SneakyThrows;
import java.io.*;
import java.util.*;
public class ExcelProcessor {
public static void main(String[] args) throws Exception {
String excelPath = "input/data.xlsx";
EasyExcel.read(excelPath)
.sheet(0)
.headRowNumber(1)
.head(ExcelRecord.class)
.registerReadListener(new SqlGenerationListener())
.doReadSync();
}
public static class SqlGenerationListener implements ReadListener<ExcelRecord> {
private final Map<String, List<String>> tableFieldMap = new HashMap<>();
@SneakyThrows
@Override
public void invoke(ExcelRecord record, AnalysisContext context) {
if (isValidRecord(record)) {
processRecord(record);
}
generateSqlToFile();
}
private boolean isValidRecord(ExcelRecord record) {
return "SelectionDept".equals(record.getDepartment()) &&
record.getBusinessObject().contains("SelectionObject") &&
isValidAttribute(record.getAttribute());
}
private boolean isValidAttribute(String attribute) {
Set<String> validAttributes = Set.of(
"FieldInfo", "FieldInfo", "FieldInfo", "FieldInfo",
"FieldInfo", "FieldInfo", "FieldInfo", "FieldInfo",
"FieldInfo", "FieldInfo"
);
return validAttributes.contains(attribute);
}
private void processRecord(ExcelRecord record) {
String tableName = record.getSourceTableEN();
String fieldName = record.getFieldEN();
tableFieldMap.computeIfAbsent(tableName, k -> new ArrayList<>()).add(fieldName);
}
@SneakyThrows
private void generateSqlToFile() {
String outputPath = "output/generated_queries.sql";
File outputFile = new File(outputPath);
if (!outputFile.exists()) {
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();
}
try (FileWriter fileWriter = new FileWriter(outputFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
for (Map.Entry<String, List<String>> entry : tableFieldMap.entrySet()) {
String tableName = entry.getKey();
List<String> fields = entry.getValue();
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("SELECT * FROM ").append(tableName).append(" WHERE ");
for (int i = 0; i < fields.size(); i++) {
queryBuilder.append(fields.get(i)).append(" IS NULL");
if (i < fields.size() - 1) {
queryBuilder.append(" OR ");
}
}
queryBuilder.append(";");
bufferedWriter.write(queryBuilder.toString());
bufferedWriter.newLine();
}
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
System.out.println("Processing completed successfully");
}
@Override
public void onException(Exception exception, AnalysisContext context) {
System.err.println("Error processing Excel data: " + exception.getMessage());
exception.printStackTrace();
}
}
}
The generated SQL queries will be saved to a text file at the specified output location. The application processes each row of the Excel file according to the filtering criteria and creates SELECT statements with appropriate WHERE clauses based on the nullability conditions.