2017-12-27 11 views
0

우리는 프로퍼티 파일을 가지고 있습니다. 엑셀 워크 북의 프로퍼티 파일에서 데이터를 얻고 싶습니다! 문제는, 하나의 속성 파일 만 있습니다. 거의 모든 데이터가 있습니다. 이제 데이터를 필터링하고 엑셀 통합 문서의 다른 시트에 저장해야합니다. 가능한가? 나는 5 장이 필요하고, 나는 5 장을 만들었고, 나는 속성 파일을 문자열 배열로 나누었다! 문제는 동일한 코드를 5 번 작성해야한다는 것입니다. 누구든지 나를 도울 수 있습니까? XSSFWorkbook을 사용하고 있습니다!파일을 엑셀 할 속성 파일

while ((str = br.readLine()) != null) { 
    fullData = fullData + "\n" + str; 
    } 
    String[] sheetData = fullData.split("//Split"); 
    int rows = 0, cols = 0; 
    for (String details : sheetData) { 
    String name = "", value = ""; 
    if (details.contains("Home Page")) { 
     lines = details.split("\n"); 
     for (int j = 0; j < lines.length; j++) { 
     if (lines[j].contains("=")) { 
      int lrows = sheet1.getLastRowNum(); 
      row = sheet1.createRow(lrows + 1); 
      name = lines[j].substring(0, lines[j].indexOf("=")); 
      value = lines[j].substring(lines[j].indexOf("=") + 1); 
      for (int i = 0; i < 2; i++) { 
      col = row.createCell(cols); 
      col.setCellValue(name); 
      col = row.createCell(cols + 1); 
      col.setCellValue(value); 
      } 
      cols = 0; 
     } 
     } 
    } 
    else if (details.contains("ManualTest") || details.contains("Expand camera")) { 

     lines = details.split("\n"); 
     for (int j = 0; j < lines.length; j++) { 
     if (lines[j].contains("=")) { 
      int lrows = sheet3.getLastRowNum(); 
      row = sheet3.createRow(lrows + 1); 
      name = lines[j].substring(0, lines[j].indexOf("=")); 
      value = lines[j].substring(lines[j].indexOf("=") + 1); 
      for (int i = 0; i < 2; i++) { 
      col = row.createCell(cols); 
      col.setCellValue(name); 
      col = row.createCell(cols + 1); 
      col.setCellValue(value); 

      } 
      rows++; 
      cols = 0; 
     } 
     } 
    } 
    } 

이것은 2 장, 같은 5 장입니다! 한 줄을 사용 // 데이터를 시트로 분할합니다!

+1

당신은 우리에게 보여해야합니다 당신이 시도한 것, 그리고 당신이 변환하려고하는 것이 탁월한 것. –

답변

1

문제는 동일한 코드를 5 번 작성해야한다는 것입니다.

당신은 당신이 방법을 사용하는 경우, 전체 코드를 다섯 번을 작성할 필요가 없습니다 :

private void fillSheet(String[] lines, HSSFSheet sheet) { 
    for (int j = 0; j < lines.length; j++) { 
     if (lines[j].contains("=")) { 
      int cols = 0; 
      int lrows = sheet.getLastRowNum(); 
      int row = sheet.createRow(lrows + 1); 
      String name = lines[j].substring(0, lines[j].indexOf("=")); 
      String value = lines[j].substring(lines[j].indexOf("=") + 1); 
      for (int i = 0; i < 2; i++) { 
       HSSFCell col = row.createCell(cols); 
       col.setCellValue(name); 
       col = row.createCell(cols + 1); 
       col.setCellValue(value); 
      } 
      cols = 0; 
     } 
    } 
} 

과 같이 사용 :

while ((str = br.readLine()) != null) { 
    fullData = fullData + "\n" + str; 
} 
String[] sheetData = fullData.split("//Split"); 
for (String details : sheetData) { 
     String[] lines = details.split("\n"); 
     if (details.contains("Home Page")) { 
     fillSheet(lines, sheet1); 
    } 
    else if (details.contains("ManualTest") || details.contains("Expand camera")) { 
     fillSheet(lines, sheet3); 
    } 
     // else if .... 
}