簡體   English   中英

如何使用 apache poi 讀取 .xlsx 文件?

[英]How to read .xlsx file with apache poi?

請參見下圖,我正在嘗試編寫一個程序,該程序可以“掃描”給定的行,而不受從哪個單元格到哪個單元格的限制,然后,找到所有相同的“字符串”。 有可能這樣做嗎? 謝謝你。

舉個例子,這樣就不會很混亂,例如:在H行,你看到有客戶的名字,有“Coresystem”,“華為”,“VIVO”等......現在問題是,如果名稱不組合在一起,它們都分開了,例如,在H5上,將是“華為”,在H9上,將是“VIVO”等,就像下面提供的圖片不同,在第 H 行所有名稱都被拆分了,我希望 apache POI 找到所有具有相同名稱的客戶,例如:如果用戶輸入“coReSysteM”,它應該能夠找到所有.equalsIgnoreCase的所有 .equalsIgnoreCase H行(順便說一句,用戶應該能夠輸入他們想要輸入的客戶姓名和他們想要搜索的行),並從A5、B5、C5、D5、E5、F5、G5、H5到A14顯示、B14、C14、D14、E14、F14、G14、H5,有可能嗎? 我正在考慮設置一個公式來查找所有客戶,例如:=CountIf

這些是我目前正在嘗試執行的代碼,但后來我堅持使用它:

package excel_reader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader2d {
    ExcelReader link = new ExcelReader();
    Scanner scan = new Scanner(System.in);

    // instance data
    private static int numberGrid[][] = null;
    private static String stringGrid[][] = null;

    // constructor
    public ExcelReader2d(String desLink) {

    }

    // methods
    public void ExeScan() throws FileNotFoundException, IOException {
        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.");
        }



    }

}

附: 我知道這會非常令人困惑,但請隨時提出任何問題,以幫助您擺脫困惑並幫助我,因為這對我來說一直是個問題。 非常感謝,我將非常感謝您的幫助。 目前使用apache poi、vscode、java。

我將遍歷工作表中的行並從每一行獲取單元格 7 ( H ) 的字符串內容。 如果該字符串滿足equalsIgnoreCase搜索值的要求,則該行是結果行之一,否則不是。

可以在List<Row>收集結果行。 然后這個List包含之后的結果行。

例子:

ExcelWorkbook.xlsx

在此處輸入圖片說明

代碼:

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 searchColumn = 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(searchColumn); // 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();
 }
}

結果:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM