JAVA apache-poi:在迭代中匹配string,需要将最终值写入特定的行

我正在研究读取excel文件的代码(查看列中的单元格),然后写入类似下表的代码:

我有一个excel文件,看起来像这样:

col1 col2 col3 col4 ----------------------------- row1 | 2,3,1 _ 1 w row2 | 3,2,7 _ 2 x row3 | _ _ 3 y row4 | 4,9 _ 4 z 

我在第2列写了一些值(使用Apache POI),如下所示:

  col1 col2 col3 col4 ----------------------------- row1 | 2,3,1 x,y,w 1 w row2 | 3,2,7 y,x 2 x row3 | _ _ 3 y row4 | 4,9 z 4 z 

本质上,第3列和第1列进行比较,如果单元格(1,1)的值与第3列匹配,则第4列的值(对应于第3列)写入第2列。

这是我的代码:重要的区域被散列。

 for (int rowIndex = 0; rowIndex < 50000; rowIndex++) { for (int columnIndex = 0; columnIndex < 30; columnIndex++) { try { XSSFCell COL0 = worksheet.getRow(rowIndex).getCell(0); String buddy = COL0.getStringCellValue(); String[] newton = buddy.split(","); XSSFCell COL1 = worksheet.getRow(rowIndex).getCell(1); String james = COL1.getStringCellValue(); XSSFCell COL2 = worksheet.getRow(rowIndex).getCell(2); String path1 = COL2.getStringCellValue(); //IMPORTANT AREA for (String i : newton) { if (i == james) { System.out.println(path1); //How do I get this (column 3) to only pull out the cells that correlate to col2 and col1 matches. } else { System.out.println(" "); }} } catch (Exception e) { System.out.println(e.toString()); } 

对Java来说真的很陌生,我只是对这个问题的具体语法感到困惑。 通过文档查看,但我一直坚持我应该做的一段时间。 任何build议将真的帮助。 谢谢!

编辑:2组列:

http://i.imgur.com/dhVSYPH.png

 //del if necessary - maps keys from col1 to col4 - MAIN CHANGES public static HashMap<String, String> getColusmnMap(Sheet sheet, int col1, int col4) { HashMap<String, String> hashMasp = new HashMap<String, String>(); for (Row rowie : sheet) { String koop = getStringCellValue(rowie.getCell(col1)); String valeue = getStringCellValue(rowie.getCell(col4)); if (hashMasp.containsKey(koop)) { valeue = hashMasp.get(koop) + valeue; } hashMasp.put(koop, valeue); } return hashMasp; } //del if necessary - Col4 and 5 hashmap - MAIN CHANGES HashMap<String, String> columnMapio = getColumnMap(sheet, 4, 5); for (Row rowie : sheet) { String cell0Valeue = getStringCellValue(rowie.getCell(0)); String[] koops = cell0Valeue.split(","); String cell1Values = ""; for (String koop : koops) { if (columnMapio.containsKey(koop)) { cell1Values += columnMapio.get(koop) + ","; } } if (cell1Values.length()>0) cell1Values = cell1Values.substring(0, cell1Values.length() -1); System.out.println(cell1Values); Cell cell = rowie.getCell(1); if (cell != null) { cell.setCellValue(cell1Values); } else { rowie.createCell(1).setCellValue(cell1Values); } } 

假设我们在第一张Mappe1.xlsx有以下Mappe1.xlsx

在这里输入图像说明

那么下面应该做你想要的:

 import org.apache.poi.ss.usermodel.*; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.FileInputStream; import java.io.InputStream; import java.util.HashMap; class ReadAndWriteTest { // a method for getting all possible cell values as strings public static String getStringCellValue(Cell cell) { String result = new String(); if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: result = cell.getStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { result = (cell.getDateCellValue()).toString(); } else { if(cell.getNumericCellValue() == (long)cell.getNumericCellValue()) result = String.format("%s",(long)cell.getNumericCellValue()); else result = String.format("%s",cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_BOOLEAN: result = new Boolean(cell.getBooleanCellValue()).toString(); break; case Cell.CELL_TYPE_FORMULA: result = cell.getCellFormula(); break; default: } } return result; } // a method for getting a HashMap which maps keys from col1 to values from col2 public static HashMap<String, String> getColumnMap(Sheet sheet, int col1, int col2) { HashMap<String, String> hashMap = new HashMap<String, String>(); for (Row row : sheet) { hashMap.put(getStringCellValue(row.getCell(col1)), getStringCellValue(row.getCell(col2))); } return hashMap; } public static void main(String[] args) { try { InputStream inp = new FileInputStream("Mappe1.xlsx"); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); // get a HashMap with keys from column 2 and values from column 3 HashMap<String, String> columnMap = getColumnMap(sheet, 2, 3); for (Row row : sheet) { String cell0Value = getStringCellValue(row.getCell(0)); String[] keys = cell0Value.split(","); String cell1Value = ""; for (String key : keys) { if (columnMap.containsKey(key)) { cell1Value += columnMap.get(key) + ","; } } if (cell1Value.length()>0) cell1Value = cell1Value.substring(0, cell1Value.length() -1); System.out.println(cell1Value); Cell cell = row.getCell(1); if (cell != null) { cell.setCellValue(cell1Value); } else { row.createCell(1).setCellValue(cell1Value); } } FileOutputStream fileOut = new FileOutputStream("Mappe1.xlsx"); wb.write(fileOut); } catch (InvalidFormatException ifex) { } catch (FileNotFoundException fnfex) { } catch (IOException ioex) { } } } 

你必须远离Python概念,并且打扰Java概念。 在apache-poi中没有像col_values这样的东西。 单元格的值不是默认的string。 所以我们必须进一步研究它们,并且有一个方法来获得所有可能的单元格值。

编辑:

HashMap将键映射到值。 一个优点是每个定义的密钥是唯一的。 所以,如果我们添加多个值到同一个键,只有最后一个值将在地图中。

如果你不想要这个,但想要连接所有添加到同一个键的值,那么你必须在我的例子中改变public static HashMap<String, String> getColumnMap(Sheet sheet, int col1, int col2)

 // a method for getting a HashMap which maps keys from col1 to values from col2 public static HashMap<String, String> getColumnMap(Sheet sheet, int col1, int col2) { HashMap<String, String> hashMap = new HashMap<String, String>(); for (Row row : sheet) { String key = getStringCellValue(row.getCell(col1)); String value = getStringCellValue(row.getCell(col2)); if (hashMap.containsKey(key)) { value = hashMap.get(key) + value; } hashMap.put(key, value); } return hashMap; }