如何解决如何在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 语句正在工作,但是,我需要上面的确切输出。我在上面的代码中做了什么:
解决方法
你几乎完成了你的任务。这里的关键是使用其中一种算法来生成组合。您可以在 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 举报,一经查实,本站将立刻删除。