2016-10-27 7 views
0

엑셀 시트에 코드 조각이 있습니다. 행 번호와 열 이름을 기반으로합니다. 내 이름은 열 이름이 null이면 나머지는 설정되지 않습니다. 열의. 이에데이터가 설정되지 않음

enter image description here는 값이 열 'D'

아래는 내 코드

public boolean setExcelData(String sheetName, String colName, int rowNum, String data) throws AutoException { 
    dataFile(); 
    try { 
     if (rowNum <= 0) 
      throw new AutoException(EXCEPTIION); 

     int index = wb.getSheetIndex(sheetName); 
     int colNum = -1; 
     if (index == -1) 
      throw new AutoException(EXCEPTIION); 

     XSSFSheet sheet = wb.getSheetAt(index); 
     Row row = sheet.getRow(0); 
     for (int i = 0; i < row.getLastCellNum(); i++) { 
      System.out.println(row.getCell(i)); 
      if (row.getCell(i) == null) { 
       throw new AutoException(EXCEPTIION); 
      } else if (row.getCell(i).getStringCellValue().trim().equals(colName)) { 
       colNum = i; 
       break; 
      } 
     } 
     if (colNum == -1) 
      throw new AutoException(EXCEPTIION); 

     sheet.autoSizeColumn(colNum); 
     row = sheet.getRow(rowNum - 1); 
     if (row == null) 
      row = sheet.createRow(rowNum - 1); 
     Cell cell = row.getCell(colNum); 
     if (cell == null) 
      cell = row.createCell(colNum); 
     cell.setCellValue(data); 

     FileOutputStream fileOut = new FileOutputStream(excelFilePath); 
     wb.write(fileOut); 
     fileOut.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } finally { 
     try { 
      wb.close(); 
      in.close(); 
     } catch (Exception e) { 
     } 
    } 
    return true; 
} 

나를 도울 수있는 모든 일 후 추가되지 얻을 엑셀. 기둥 이름가 널인 경우 미리 감사

답변

0

루프 반복을 위해 건너

for (int i = 0; i < row.getLastCellNum(); i++) { 
    System.out.println(row.getCell(i)); 
    if (String.IsNullOrEmpty(colName)) 
     continue; 
    if (row.getCell(i) == null) { 
     throw new AutoException(EXCEPTIION); 
    } else if (row.getCell(i).getStringCellValue().trim().equals(colName)) { 
     colNum = i; 
     break; 
    } 
}