Skip to content

Commit d5d54cb

Browse files
committed
add SQLParser
1 parent c68f537 commit d5d54cb

File tree

3 files changed

+27
-47
lines changed

3 files changed

+27
-47
lines changed

sample-xlsx-file.xlsx

3.73 KB
Binary file not shown.

src/main/java/ExcelReader.java

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111

1212
public class ExcelReader {
13-
public static final String SAMPLE_XLS_FILE_PATH = "./sample-xls-file.xls";
13+
//public static final String SAMPLE_XLS_FILE_PATH = "./sample-xls-file.xls";
1414
public static final String SAMPLE_XLSX_FILE_PATH = "./sample-xlsx-file.xlsx";
1515

1616
public static void main(String[] args) throws IOException, InvalidFormatException {
@@ -21,32 +21,6 @@ public static void main(String[] args) throws IOException, InvalidFormatExceptio
2121
// Retrieving the number of sheets in the Workbook
2222
System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
2323

24-
/*
25-
=============================================================
26-
Iterating over all the sheets in the workbook (Multiple ways)
27-
=============================================================
28-
*/
29-
30-
// 1. You can obtain a sheetIterator and iterate over it
31-
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
32-
System.out.println("Retrieving Sheets using Iterator");
33-
while (sheetIterator.hasNext()) {
34-
Sheet sheet = sheetIterator.next();
35-
System.out.println("=> " + sheet.getSheetName());
36-
}
37-
38-
// 2. Or you can use a for-each loop
39-
System.out.println("Retrieving Sheets using for-each loop");
40-
for(Sheet sheet: workbook) {
41-
System.out.println("=> " + sheet.getSheetName());
42-
}
43-
44-
// 3. Or you can use a Java 8 forEach wih lambda
45-
System.out.println("Retrieving Sheets using Java 8 forEach with lambda");
46-
workbook.forEach(sheet -> {
47-
System.out.println("=> " + sheet.getSheetName());
48-
});
49-
5024
/*
5125
==================================================================
5226
Iterating over all the rows and columns in a Sheet (Multiple ways)
@@ -62,6 +36,7 @@ Iterating over all the rows and columns in a Sheet (Multiple ways)
6236
// 1. You can obtain a rowIterator and columnIterator and iterate over them
6337
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
6438
Iterator<Row> rowIterator = sheet.rowIterator();
39+
StringBuilder sb = new StringBuilder();
6540
while (rowIterator.hasNext()) {
6641
Row row = rowIterator.next();
6742

@@ -71,29 +46,12 @@ Iterating over all the rows and columns in a Sheet (Multiple ways)
7146
while (cellIterator.hasNext()) {
7247
Cell cell = cellIterator.next();
7348
String cellValue = dataFormatter.formatCellValue(cell);
74-
System.out.print(cellValue + "\t");
75-
}
76-
System.out.println();
77-
}
78-
79-
// 2. Or you can use a for-each loop to iterate over the rows and columns
80-
System.out.println("\n\nIterating over Rows and Columns using for-each loop\n");
81-
for (Row row: sheet) {
82-
for(Cell cell: row) {
83-
String cellValue = dataFormatter.formatCellValue(cell);
84-
System.out.print(cellValue + "\t");
49+
sb.append(cellValue);
8550
}
86-
System.out.println();
8751
}
52+
System.out.println(sb.toString());
53+
System.out.println(SQLParser.extractFieldsForTable(sb.toString(),"uf"));
8854

89-
// 3. Or you can use Java 8 forEach loop with lambda
90-
System.out.println("\n\nIterating over Rows and Columns using Java 8 forEach with lambda\n");
91-
sheet.forEach(row -> {
92-
row.forEach(cell -> {
93-
printCellValue(cell);
94-
});
95-
System.out.println();
96-
});
9755

9856
// Closing the workbook
9957
workbook.close();

src/main/java/SQLParser.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.*;
2+
import java.util.regex.Matcher;
3+
import java.util.regex.Pattern;
4+
5+
public class SQLParser {
6+
7+
public static Set<String> extractFieldsForTable(String sql, String tableAlias) {
8+
// 正则表达式用于匹配指定表别名的字段
9+
String regex = tableAlias + "\\.([a-zA-Z_][a-zA-Z0-9_]*)";
10+
Pattern pattern = Pattern.compile(regex);
11+
Matcher matcher = pattern.matcher(sql);
12+
13+
Set<String> fields = new HashSet<>();
14+
15+
// 循环查找匹配字段
16+
while (matcher.find()) {
17+
fields.add(matcher.group(1)); // 去重添加字段
18+
}
19+
20+
return fields;
21+
}
22+
}

0 commit comments

Comments
 (0)