当前位置: 首页 > news >正文

使用 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"
    }
]
}

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 fileFileOutputStream 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);}}
}

http://www.lryc.cn/news/246790.html

相关文章:

  • 雷达公式实现(matlab)
  • CMake构建一个转换为3d tile的开源代码成功
  • Java线程通信
  • 计算4人队形的最可能分布
  • 如何解决 Java 中的 IllegalArgumentException 异常?
  • Vue 双向数据绑定
  • 电脑开机过程中,程序的启动的顺序是怎么样的?
  • JSON详细教程
  • DSP介绍及CCS
  • 周期串(Periodic Strings)
  • C语言——猜凶手
  • 【TiDB】TiDB离线方式部署
  • android shape绘制半圆
  • 【开源】基于Vue和SpringBoot的个人健康管理系统
  • qt QString字符串常用转换
  • JAVA sql 查询3
  • PHP while 和 do-while 循环 学习资料
  • OpenJudge NOI 1.8 16:矩阵剪刀石头布 c语言
  • mysql 性能参数调优详解
  • 基于.net framework4.0框架下winform项目实现寄宿式web api
  • Vue中项目进行文件压缩与解压缩 (接口返回文件的url压缩包前端解析并展示出来,保存的时候在压缩后放到接口入参进行保存)
  • Linux shell编程学习笔记31:alias 和 unalias 操作 命令别名
  • Django JSONField/HStoreField SQL注入漏洞(CVE-2019-14234)
  • Unity中Shader的Standard材质解析(一)
  • 5.1 Windows驱动开发:判断驱动加载状态
  • Linux之高级IO
  • 进程和线程的关系
  • YOLOv5全网独家改进:NanoDet算法动态标签分配策略(附原创改进代码),公开数据集mAP有效涨点,来打造新颖YOLOv5检测器
  • 原生DOM事件、react16、17和Vue合成事件
  • 基于HTML+CSS+JavaScript的登录注册界面设计