如何在 java 中使用 Apache POI 从 Excel sheet 中提取数据(查找框架)

How to extract data from Excel sheet using Apache POI in java(lookup framing)

public class Array_Learn {
    public static void main(String[] args) {
        try {
            FileInputStream ExcelFile = new FileInputStream(new File("C:\Users\Anbu.B\Desktop\POI-Test\mediTask.xlsx"));
            XSSFWorkbook book1 = new XSSFWorkbook(ExcelFile);
            XSSFSheet sheet = book1.getSheetAt(0);
            Iterator<Row> rowiter = sheet.iterator();
            while (rowiter.hasNext()) {
                XSSFRow row = (XSSFRow) rowiter.next();
                if (row.getRowNum() == 2) {
                    Iterator cellIterator = row.cellIterator();
                    while (cellIterator.hasNext()) {
                        XSSFCell cell = (XSSFCell) cellIterator.next();
                        if (cell.getStringCellValue().contains("|")) {
                            String split[] = cell.getStringCellValue().split("\|");
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

我需要这个输出:

chest&&pain=J90
lung&&pneumonia=J54.9
lungs&&pneumonia=J54.9
bronchi&&pneumonia=J54.9
bronchus&&pneumonia=J54.9
colon&&ascending&tumor=D12.5
colon&&ascending&carcinoma=D12.5
colon&&ascending&cancer=D12.5
colon&&ascending&&tumor&&resection=D12.6
colon&&descending&&tumor&&resection=D12.6
colon&&ascending&&carcinoma&&resection=D12.6
colon&&descending&&carcinoma&&resection=D12.6
colon&&ascending&&cancer&&resection=D12.6
colon&&descending&&cancer&&resection=D12.6

上面的代码正在读取行并迭代每个单元格并检查单元格是否包含 | 符号条件是否为真拆分语句正在工作但是,我需要上面的确切输出。我在上面的代码中做了什么:

  1. 读取 excel 文件。
  2. 从 excel 文件中读取 sheet。
  3. 然后创建行迭代器。
  4. 创建单元迭代器。
  5. 检查单元格包含 |然后符号拆分该单元格字符串并存储到字符串数组中。

您几乎完成了任务。这里的关键是使用其中一种算法来生成组合。您可以找到此类算法的一般描述 there, or more close examples with strings on java there.


完整代码示例(递归算法):

计算不同组合的ParsedRowclass:

class ParsedRow {
    private final List<List<String>> combinations;
    private final String suffix;

    public ParsedRow(List<List<String>> combinations, String suffix) {
        this.combinations = combinations;
        this.suffix = suffix;
    }

    public List<String> combine() {
        List<String> res = new ArrayList<>();
        combine(res, 0, "");
        return res;
    }


    public void combine(List<String> res, int depth, String current) {
        if (combinations.size() == depth) {
            res.add(current + "=" + suffix);
            return;
        }
        String delimiter = current.isEmpty() ? "" : "&&";
        for (int i = 0; i < combinations.get(depth).size(); i++) {
            combine(res, depth + 1, current + delimiter + combinations.get(depth).get(i));
        }
    }
}

Mainclass读取xlsx文件并打印结果

public class Main {
    public static void main(String[] args) throws IOException {
        try (final FileInputStream file = new FileInputStream("diseases.xlsx");
             XSSFWorkbook workbook = new XSSFWorkbook(file)) {
            List<String> finalResult = new ArrayList<>();
            for (Row row : workbook.getSheetAt(0)) {
                List<List<String>> combinations = new ArrayList<>();
                String suffix = "";
                for (Cell cell : row) {
                    if (cell.getColumnIndex() != 4) {
                        final List<String> strings = Arrays.asList(cell.getStringCellValue().split("\|"));
                        combinations.add(strings);
                    } else {
                        suffix = cell.getStringCellValue();
                    }
                }
                ParsedRow parsedRow = new ParsedRow(combinations, suffix);
                finalResult.addAll(parsedRow.combine());
            }
            for (String line : finalResult) {
                System.out.println(line);
            }
        }
    }
}