public static void main(String[] args) {
Pattern pattern = Pattern.compile("[a-zA-Z]+"); // 匹配英文字母
List<String> wordList = new ArrayList<>();
String filePath = "C:/english-wordlists-master/english-wordlists-master/柯林斯词频-5星至0星.xlsx";
try (FileInputStream fis = new FileInputStream(new File(filePath))) {
Workbook workbook = WorkbookFactory.create(fis);
// 遍历每个工作表
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i);
// 遍历行
for (Row row : sheet) {
// 遍历单元格
for (Cell cell : row) {
// 获取单元格内容并提取英文单词
String cellValue = cell.toString();
if (cellValue != null) {
Matcher matcher = pattern.matcher(cellValue);
while (matcher.find()) {
String word = matcher.group();
wordList.add(word);
}
}
}
}
}
} catch (IOException | InvalidFormatException e) {
e.printStackTrace();
}
// 打印提取到的英文单词
for (String word : wordList) {
System.out.println(word);
}
}
Java 解析xlsx文件
最新推荐文章于 2024-05-17 20:15:57 发布