使用java代码比较excel中两列是否有包含关系,若有包含关系,核对编号是否一致。
excel数据样例如下:
package com.itownet.hg;import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileInputStream;
import java.io.IOException;public class TestExcel1 {public static void main(String[] args) {try {// 创建工作簿对象XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("C:\\Users\\yang\\Desktop\\123.xlsx"));// 获取工作簿下sheet的个数int sheetNum = xssfWorkbook.getNumberOfSheets();System.out.println("该excel文件中总共有:" + sheetNum + "个sheet");// 遍历工作簿中的所有数据for (int i = 0; i < sheetNum; i++) {// 读取第i个工作表System.out.println("读取第" + (i + 1) + "个sheet");XSSFSheet sheet = xssfWorkbook.getSheetAt(i);// 获取最后一行的num,即总行数。此处从0开始int maxRow = sheet.getLastRowNum();for (int j = 0; j < maxRow; j++) {String as = sheet.getRow(j).getCell(2) + "";if (as != null && as.length() > 0) {for (int k = 0; k < maxRow; k++) {String a1 = sheet.getRow(k).getCell(0) + "";String a2 = sheet.getRow(j).getCell(1) + "";if (a1.endsWith(as)) {if (a1.indexOf(a2) <= 0 && a1.indexOf("需求分析方案") > 0) {System.out.print(a2);System.out.println(as);System.out.println(a1);System.out.println("------------");}}}}}System.out.println("=================================");for (int j = 0; j < maxRow; j++) {String as = sheet.getRow(j).getCell(4) + "";if (as != null && as.length() > 0) {for (int k = 0; k < maxRow; k++) {String a1 = sheet.getRow(k).getCell(0) + "";String a2 = sheet.getRow(j).getCell(3) + "";if (a1.endsWith(as)) {if (a1.indexOf(a2) <= 0 && a1.indexOf("总体技术方案") > 0) {System.out.print(a2);System.out.println(as);System.out.println(a1);System.out.println("------------");}}}}}for (int row = 0; row <= -1; row++) {// 获取最后单元格num,即总单元格数 ***注意:此处从1开始计数***int maxRol = sheet.getRow(row).getLastCellNum();System.out.println("--------第" + row + "行的数据如下--------");for (int rol = 0; rol < maxRol; rol++) {System.out.print(sheet.getRow(row).getCell(rol) + " ");}System.out.println();}}} catch (IOException e) {e.printStackTrace();}}}
所需jar包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
运行结果:
该excel文件中总共有:1个sheet
读取第1个sheet
6.2项目实施过程文档管理分析
《需求分析方案》->6.1.2项目实施过程文档管理分析
------------
6.3项目实施组织架构分析
《需求分析方案》->6.1.3项目实施组织架构分析
------------
6.3项目实施组织架构分析
《需求分析方案》->6.1.3项目实施组织架构分析
------------