微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在java中使用Apache POI从Excel工作表中提取数据查找框架

如何解决如何在java中使用Apache POI从Excel工作表中提取数据查找框架

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

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

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

The image is the exact input excel file i need the above output from the excel file.

解决方法

你几乎完成了你的任务。这里的关键是使用其中一种算法来生成组合。您可以在 java there 上找到此类算法的一般描述 there,或者更多带有字符串的示例。


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

用于计算不同组合的ParsedRow类:

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,"");
        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));
        }
    }
}

用于读取Main文件并打印结果的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);
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?