使用 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);
        }
    }
}
相关推荐
276695829218 小时前
雷池waf 逆向
java·开发语言·前端·python·wasm·waf·雷池waf
逸Y 仙X18 小时前
Java时间类型入门到实战
java·ide·spring·tomcat
Want59518 小时前
C/C++跳动的爱心③
java·c语言·c++
h***936618 小时前
记录 idea 启动 tomcat 控制台输出乱码问题解决
java·tomcat·intellij-idea
Le1Yu18 小时前
核销优惠券(OpenFeign远程调用、微信小程序滑动分页查询后端实现、ThreadLocal存储用户信息、seata解决分布式事务问题)
java
点点@18 小时前
Java idea运行项目包名过长
java·ide·intellij-idea
Highcharts.js18 小时前
Highcharts 金融图表之“点线图 ”讲解
java·开发语言·highcharts·金融点线图·点线图·模块安装
星轨初途18 小时前
C++的条件判断与循环及数组(算法竞赛类)
开发语言·c++·经验分享·笔记·算法
她说..18 小时前
Spring AOP 操作日志框架(CV可用)
java·jvm·spring·springboot·springcloud
Linux运维技术栈18 小时前
生产环境资源占用过高排查实战:从Heap Dump到全链路优化
java·服务器·网络·数据库·程序