使用 Java 来读取 Excel 文件,检查每一行中的 URL,并将不符合条件的行标记为红色

-- 日、时、分、秒,这是计时的单位,惜时就应该惜日、惜时、惜分、惜秒。

用 Java 来读取 Excel 文件,检查每一行中的 URL,并将不符合条件的行标记为红色。以下是一个简单的示例,使用 Apache POI 进行 Excel 操作:

首先,你需要添加 Apache POI 的依赖。如果是 Maven 项目,可以在 pom.xml 文件中添加:

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>4.1.2</version> <!-- 使用适当的版本 -->

</dependency>

json 格式

{

"yourArrayKey":[

{

"url":"http://epaper.jwb.com.cn/jwb"

},

{

"url":"http://epaper.jwb.com.cn/jwb"

}

]

}

java 复制代码
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class ExcelValidator {

    public static void main(String[] args) {
        String excelFilePath = "path/to/your/excel/file.xlsx";

        try {
            markInvalidRows(excelFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void markInvalidRows(String excelFilePath) throws IOException {
        FileInputStream excelFile = new FileInputStream(new File(excelFilePath));
        Workbook workbook = new XSSFWorkbook(excelFile);

        Sheet sheet = workbook.getSheetAt(0);

        for (Row row : sheet) {
            Cell urlCell = row.getCell(0); // Assuming URL is in the first column (index 0)

            if (urlCell != null && urlCell.getCellType() == CellType.STRING) {
                String url = urlCell.getStringCellValue();
                boolean isValid = isValidUrl(url);

                if (!isValid) {
                    markRowAsInvalid(row);
                }
            }
        }

        excelFile.close();

        // Save the changes back to the Excel file
        FileOutputStream outputStream = new FileOutputStream(excelFilePath);
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();
    }

    private static boolean isValidUrl(String urlString) {
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("HEAD");
            connection.connect();

            int responseCode = connection.getResponseCode();
            long responseTime = connection.getHeaderFieldDate("Date", 0) - connection.getHeaderFieldDate("Date", 0);

            return responseCode == HttpURLConnection.HTTP_OK && responseTime <= 20;
        } catch (Exception e) {
            return false;
        }
    }

    private static void markRowAsInvalid(Row row) {
        CellStyle style = row.getSheet().getWorkbook().createCellStyle();
        style.setFillForegroundColor(IndexedColors.RED.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        for (Cell cell : row) {
            cell.setCellStyle(style);
        }
    }
}
相关推荐
步行cgn4 分钟前
Spring 注入内部 Bean 详解
java·spring·rpc
郝学胜-神的一滴10 分钟前
C++20模板元编程 02:吃透模板基础核心四大核心知识点
开发语言·数据结构·c++·程序人生·软件工程·visual studio
troy12815 分钟前
Visual Studio Code 详细教程:从入门到高效开发
开发语言·python
user_admin_god16 分钟前
第 05 篇:结构化输出 —— 让模型稳定返回 JSON
java·人工智能·spring boot·语言模型
土司大王20 分钟前
LeetCode hot100——394.字符串解码:Java 双栈模拟
java·算法·leetcode
邪修king22 分钟前
Re:Linux系统篇(二十五):文件系统(一):磁盘硬件底层原理:从物理结构到 CHS/LBA 寻址,搞懂硬盘数据的定位逻辑
java·linux·运维·gpt
驭渊的小故事24 分钟前
Spring Boot 注解详解01
java·前端·spring boot
泡海椒25 分钟前
jquick-pdf 超详细入门教程:Java 轻量级 HTML 模板生成 PDF 工具
java·开发语言
蛋先生DX29 分钟前
Java和Go都拍胸脯说"内存我包了",可它们到底是怎么下手的?
java·go·编程语言
八年。。29 分钟前
WSL学习(三)——搭建Linux + C++ + SOME/IP 基础开发环境
开发语言·笔记·ubuntu