是否可以使用apache poi在Excel中扫描一行并比较其中的字符串?

问题描述 投票:-1回答:1

参见下图,我正在尝试编写一个程序,该程序可以“扫描”给定的行,而没有限制从哪个单元格到哪个单元格,然后查找所有相同的“字符串”。有可能这样做吗?谢谢。

举个例子,这样不会太令人困惑,例如:在H行,您看到客户的名字,有“ Coresystem”,“ Huawei”,“ VIVO”等。现在问题是,如果未将名称分组在一起,它们会被拆分,例如,在H5上将是“ Huawei”,在H9上将是“ VIVO”,依此类推,这与下面提供的图片不同,在H行上,所有名称均被拆分,我希望apache POI查找具有相同名称的所有客户,例如:如果用户输入“ coReSysteM”,则应该能够找到所有的.equalsIgnoreCase H行上的所有Coresystem(顺便说一句,用户应该能够输入他们要输入的客户名称和他们要搜索的行),并从A5,B5,C5,D5,E5,F5,G5, H5至A14,B14,C14,D14,E14,F14,G14,H5,可以吗?我正在考虑设置一个公式来查找所有客户,例如:= CountIf

这些是我目前正在尝试执行的代码,但随后我被困在其中:

Scanner scan = new Scanner(System.in);
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("C:\\Users\\Sonic\\Desktop\\20191223 IMPORTS.xlsx"));

        XSSFSheet sheet = workbook.getSheetAt(0);
        final int rowStart = Math.min(15, sheet.getFirstRowNum()), rowEnd = Math.max(1400, sheet.getLastRowNum());

        System.out.print("Enter the rows that you want to search for: (for ex. the rows that stores customer's name) ");
        int searchRows = scan.nextInt();
        System.out.print("Enter the customer's name that you are looking for: ");
        String name = scan.nextLine();
        //int rowNum;

        // Search given row
        XSSFRow row = sheet.getRow(searchRows);
        try {
            for (int j = 4; j < rowEnd; j++) {
                Row r = sheet.getRow(j);
                if (name.equalsIgnoreCase(name)) {
                    row.getCell(j).getStringCellValue();
                }

                // skip to next iterate if that specific cell is empty
                if (r == null)
                    continue;

            }
        } catch (Exception e){
            System.out.println("Something went wrong.");
        }

ps。我知道这将非常令人困惑,但是请随时提出任何问题,以帮助您摆脱混乱并为我提供帮助,因为这对我来说是个问题。非常感谢您,我将非常感谢您的帮助。当前使用apache poi,vscode,java。

java excel apache-poi
1个回答
0
投票

我将遍历工作表中的行,并从每一行中获取单元格7(H)的字符串内容。如果该字符串满足搜索值equalsIgnoreCase的要求,则该行是结果行之一,否则不行。

一个人可以在List<Row>中收集结果行。然后,此List包含之后的结果行。

示例:

ExcelWorkbook.xlsx

enter image description here

代码:

import org.apache.poi.ss.usermodel.*;

import java.util.List; 
import java.util.ArrayList; 

import java.io.FileInputStream;

public class ExcelReadRowsByColumnValue {

 public static void main(String[] args) throws Exception {

  String filePath = "./ExcelWorkbook.xlsx";

  String toSearch = "coresystem";
  int seachColumn = 7; // column H
  List<Row> results = new ArrayList<Row>();

  DataFormatter dataFormatter = new DataFormatter();
  Workbook workbook = WorkbookFactory.create(new FileInputStream(filePath));
  FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator(); 
  Sheet sheet = workbook.getSheetAt(0);

  for (Row row : sheet) { // iterate over all rows in the sheet
   Cell cellInSearchColumn = row.getCell(seachColumn); // get the cell in seach column (H)
   if (cellInSearchColumn != null) { // if that cell is present
    String cellValue = dataFormatter.formatCellValue(cellInSearchColumn, formulaEvaluator); // get string cell value
    if (toSearch.equalsIgnoreCase(cellValue)) { // if cell value equals the searched value
     results.add(row); // add that row to the results
    }
   }
  }

  // print the results
  System.out.println("Found results:");
  for (Row row : results) {
   int rowNumber = row.getRowNum()+1;
   System.out.print("Row " + rowNumber + ":\t");
   for (Cell cell : row) {
    String cellValue = dataFormatter.formatCellValue(cell, formulaEvaluator);
    System.out.print(cellValue + "\t");
   }
   System.out.println();
  }

  workbook.close();
 }
}

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.