使用 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);
        }
    }
}
相关推荐
oh,huoyuyan1 小时前
火语言RPA--Excel删除内容
excel·rpa
南山十一少1 小时前
Spring Security+JWT+Redis实现项目级前后端分离认证授权
java·spring·bootstrap
软件黑马王子3 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
闲猫3 小时前
go orm GORM
开发语言·后端·golang
427724003 小时前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo3 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦3 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个3 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
李白同学4 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?5 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash