繁体   English   中英

在 java 中同时读取两个 excel 行

[英]Reading two excel row simultaneously in java

我有包含列名和值的属性文件。 如何同时读取两行文件并在 has-map 中添加键值对。 此外,如果任何键名没有映射,则应从 map 中省略。

列名和值

    public class ExcelReader {
    public static ArrayList<String> ar = new ArrayList<String>();
    public static ArrayList<Integer> val = new ArrayList<Integer>();
    public static LinkedHashMap<String, Integer> cmMappingHashMap = new 
    LinkedHashMap<String, Integer>();
    public static String str;
    public static Row row;
    public static void main(String[] args)throws IOException{
    String excelFilePath = "C:\\Users\\test\\Downloads\\contacts.xlsx";
    FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet firstSheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = firstSheet.rowIterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Row row2 = rowIterator.next();
                row2.setRowNum(1);
                System.out.println(row.getRowNum());
                // Now let's iterate over the columns of the current row
                Iterator<Cell> cellIterator = row.cellIterator();
                Iterator<Cell> cellIterator1 = row2.cellIterator();
                while (cellIterator.hasNext()&&cellIterator1.hasNext()) {
                    Cell cell = cellIterator.next();
                    Cell cell1 = cellIterator1.next();
                    System.out.println(cell.getColumnIndex());
                      System.out.print(cell.getStringCellValue()+ "--"+cell1.getNumericCellValue());
                }
            }
    }
}

上面的代码在线程“main”中出现异常java.util.NoSuchElementException

我想将第一行的单元格 1 添加到第二行的单元格 1 中,如下所示

 Key ----Value
 Name----1
 ID------2
 Name----3

我根据堆栈上存在的各种输入创建了以下代码来实现上述目标。

 public void readPosition() throws FileNotFoundException, IOException {
 columnNameArray = new String[propertyFilecolumnCount];
 Workbook wb = null;
 wb = new XSSFWorkbook(new FileInputStream(new 
 File("C:\\Users\\aaa\\Downloads\\Mapping_Data_Upload.xlsx")));
 Sheet sheet = wb.getSheetAt(0);
 Iterator<Row> itr = sheet.iterator();
 while(itr.hasNext()){
  Row row = itr.next();
  Iterator<Cell> cellItrator = row.iterator();
    if(row.getRowNum()==0){
      while(cellItrator.hasNext()){
        Cell  cell = cellItrator.next();
        String cellString =cell.toString();
          if(cellString.length()!=0){
            columnNameArray[cell.getColumnIndex()]=cellString;
           }
       }
     }
    if(row.getRowNum()==1){
    columnValueArray=new String[columnNameArray.length];
    int columnIndex=0;
    List<Cell> cells = new ArrayList<Cell>();
    int lastColumn = Math.max(row.getLastCellNum(), 7);
     for (int cn = 0; cn < lastColumn; cn++) {
         Cell c = row.getCell(cn, Row.RETURN_BLANK_AS_NULL);
          if(c==null){
            cellValue="Null";
            columnIndex =columnIndex+1;
            columnValueArray[columnIndex]=cellValue;
        }
        else{
         cells.add(c);
         columnValueArray[c.getColumnIndex()]=c.toString();
                                columnIndex=c.getColumnIndex();
        }
       }
    for (int j=0,int k=0;j<columnNameArray.length && k<columnValueArray.length;j++,k++){
      if(columnValueArray[k].equalsIgnoreCase("Null")){
       }
      else{
           double val=Double.parseDouble(columnValueArray[k]);
          int value=(int) Math.round(val);
          MappingHashMap.put(columnNameArray[j], value);
            }
        }
        columnNameArray=null;
        columnValueArray=null;
        }

因为对于某些列,我没有得到映射,因为数据文件中不存在映射。 所以我必须从 has map 中删除该 columnName。

ID | FName | LName | Address| City| Zip| Degree| Stdt| EndDt| 
1  | 2     |  3    |4       | 5    |    |      |  6 |   7   |

这样我就可以使用上面的代码如下所示。 有什么有效的方法可以得到以下

Key ----Value
ID---1
FName----2
LName----3
Address---4
City------4
Stdt----5
enddt----6

您可以创建一个 model class 属性名称,ID 而不是直接读取这两行,然后您可以实例化 object 并分配值。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM