2016-10-27 8 views
0

Excelのシートに入るコードがあります。指定された行番号と列名に基づいています。問題は列名がnullの場合残りの部分に値が設定されないことです列のこの中enter image description hereデータがExcelに設定されていない

値が列「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; 
} 

です。 ColumnNameにはnullの場合、事前に おかげ

答えて

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; 
    } 
} 
関連する問題