SpringBoot读取有道词典单词释义、音标

发布于:2025-02-19 ⋅ 阅读:(21) ⋅ 点赞:(0)

准备工作

首先准备一个 klmy.xlsx 的文件,文件名可以自定义
文件里内容如下:
在这里插入图片描述

代码准备

所需依赖

<dependency>
	<groupId>org.jsoup</groupId>
	<artifactId>jsoup</artifactId>
	<version>1.11.3</version>
</dependency>

<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>5.2.3</version>
</dependency>

全部代码

package com.jokerbug.blogproject.common.utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class WordPhoneticGenerator {

    // 封装的方法,用于获取单个单词的音标和释义
    public static String[] getWordInfo(String word) {
        String url = "https://www.youdao.com/w/eng/" + word;
        try {
            Document doc = Jsoup.connect(url).get();
            String britishPron = doc.select("#phrsListTab h2 div span:nth-of-type(1) span").text();
            String americanPron = doc.select("#phrsListTab h2 div span:nth-of-type(2) span").text();
            Elements transElements = doc.select("#phrsListTab .trans-container ul");
            String transPron = "";
            if (!transElements.isEmpty()) {
                transPron = transElements.first().text();
            }
            return new String[]{britishPron, americanPron, transPron};
        } catch (IOException e) {
            System.out.println(e.getMessage() + " " + word);
            return new String[]{"", "", ""};
        }
    }

    // 假设这个方法用于获取单词的音标和释义
    public static void main(String[] args) {
        System.out.println("********".repeat(10));
        System.out.println("欢迎来到单词音标批量自动生成小程序!");
        System.out.println("注意事项: \n" +
                "1.请确保你的网络通畅!\n" +
                "2.请确保你的文件后缀格式为xlsx而非csv!\n" +
                "3.请确保你的单词全部在第1列!\n" +
                "4. 运行过程漫长请耐心等待,不要中途退出,否则不会得到任何结果!\n");

        String filePath = "/Users/xiaoyuan/Downloads/klmy.xlsx";
        File file = new File(filePath);
        // 检查文件是否存在
        if (!file.exists()) {
            System.out.println("你输入的文件路径有误,请重新输入! ");
            return;
        }
        // 检查文件是否可读
        if (!file.canRead()) {
            System.out.println("没有权限读取文件,请检查文件权限: " + filePath);
            return;
        }
        // 检查文件是否可写
        if (!file.canWrite()) {
            System.out.println("没有权限写入文件,请检查文件权限: " + filePath);
            return;
        }
        System.out.println("********".repeat(10));
        System.out.println("********文件导入成功!********");

        try (FileInputStream fis = new FileInputStream(file);
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet worksheet = workbook.getSheet("Sheet1");
            // 向右插入两列准备存放数据
            for (int rowIndex = 0; rowIndex <= worksheet.getLastRowNum(); rowIndex++) {
                Row row = worksheet.getRow(rowIndex);
                if (row == null) {
                    row = worksheet.createRow(rowIndex);
                }
                for (int colIndex = row.getLastCellNum(); colIndex >= 2; colIndex--) {
                    Cell cell = row.getCell(colIndex);
                    if (cell != null) {
                        row.createCell(colIndex + 2, cell.getCellType());
                        row.getCell(colIndex + 2).setCellValue(cell.getStringCellValue());
                    }
                }
            }
            // 在顶部插入一行写入注释
            worksheet.shiftRows(0, worksheet.getLastRowNum(), 1);
            Row headerRow = worksheet.createRow(0);
            String[] headers = {"单词", "英音", "美音", "释义"};
            Font font = workbook.createFont();
            font.setFontName("微软雅黑");
            font.setFontHeightInPoints((short) 20);
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
                CellStyle style = workbook.createCellStyle();
                style.setFont(font);
                cell.setCellStyle(style);
            }
            // 设置列宽
            for (int i = 0; i < 4; i++) {
                worksheet.setColumnWidth(i, 30 * 256);
            }

            // 保存修改后的文件
            try (FileOutputStream fos = new FileOutputStream(file)) {
                workbook.write(fos);
            }

            // 遍历工作表的行
            for (Row row : worksheet) {
                if (row.getRowNum() < 1) {
                    continue;
                }
                Cell cell = row.getCell(0);
                if (cell == null) {
                    continue;
                }
                String word = cell.getStringCellValue();
                String[] wordInfo = getWordInfo(word);
                String britishPron = wordInfo[0];
                String americanPron = wordInfo[1];
                String transPron = wordInfo[2];

                System.out.println("正在输出: " + britishPron + " " + americanPron + " " + transPron);

                // 直接使用当前行写入数据
                Cell britishCell = row.createCell(1);
                britishCell.setCellValue(britishPron);
                CellStyle britishStyle = workbook.createCellStyle();
                britishStyle.setFont(font);
                britishCell.setCellStyle(britishStyle);

                Cell americanCell = row.createCell(2);
                americanCell.setCellValue(americanPron);
                CellStyle americanStyle = workbook.createCellStyle();
                americanStyle.setFont(font);
                americanCell.setCellStyle(americanStyle);

                Cell transCell = row.createCell(3);
                transCell.setCellValue(transPron);
                CellStyle transStyle = workbook.createCellStyle();
                transStyle.setFont(font);
                transCell.setCellStyle(transStyle);
            }

            // 再次保存修改后的文件
            try (FileOutputStream fos = new FileOutputStream(file)) {
                workbook.write(fos);
            }
            System.out.println("单词音标已经全部转换完毕!已经成功保存在原文件: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

注意事项

注意要换成自己的文件地址,并确认文件名、后缀是否正确

String filePath = "/Users/admin/Downloads/klmy.xlsx";

效果展示
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到