2017-12-16 30 views
0

반복 행이있는 데이터의 단일 행을 여러 출력 행으로 변환하기 위해 Transformer 단계로 전환하려고합니다.단일 행을 여러 행으로 변환

반복 데이터가 포함 된 여러 열이있는 행이 입력 데이터에 포함되어있는 경우 Transformer 단계를 사용하여 여러 출력 행을 생성하는 방법 (반복되는 각 열마다 하나씩). 입력 :

Col1 Col2 Name1 Name2 Name3 

abc  def  Jim  Bob  Tom 

Input

출력 :

Col1 Col2 Name 

abc  def  Jim 

abc  def  Bob 

abc  def  Tom 

output

//loop to read data and store in array 

for(i = 0; i <= Lastrow; i++) { 
    for(j = 0; j <= R_endColumn; j++) { 
     Row readrow = sheet.getRow(i); 
     Cell readcell = readrow.getCell(j); 
     String datavalues = df.formatCellValue(readcell); 
     System.out.println(datavalues); 
     localstore[i][j] = datavalues; 
    } 
} 
int newrow_Number = 0; 

for(i = 0; i <= Lastrow; i++) { 
    for(int k = 0; k <= Lengthof_IP; k++) { 
     newrow_Number = newrow_Number + 1; 
     for(j = 0; j <= requirement_endColumn; j++) { 
      Row readrow = sheet.createRow(newrow_Number); 
      Cell readcell = readrow.getCell(j, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK); 
      String datavalues = df.formatCellValue(readcell); 
      readcell.setCellValue(localstore[i][j]); 
     } 
    } 
} 
FileOutputStream out = new FileOutputStream(new File("C:/DataTransformation/op1.xlsx")); 
workbook.write(out); 
out.close(); 
+1

Excel (2010+)에서는 '전원 쿼리'또는 '가져 오기 및 변환'을 사용하여 이름 열을 unpivot 할 수 있습니다. –

+0

더 많은 코드를 게시 할 수 있습니까? 같은 파일에 출력하고 싶습니까? 원본 텍스트를 바꿔야합니까? 반복되는 처음 두 열만입니까? 코드를 실행할 때 얻은 결과는 무엇입니까? – clinomaniac

답변

0

나는 t를 해결할 수 있었다 그의 코드를 따르면.

 filepath="C:/DataTransformation/Book1_New1.xlsx"; 
      File file = new File(filepath); 

      filepath = file.getPath(); 

      //File file = new File("C:/Input/Programfiles/TestStepTable.xlsx"); 
       System.out.println(filepath); 
       XSSFWorkbook workbook = new XSSFWorkbook(filepath); 
       XSSFSheet sheet = workbook.getSheetAt(0); 



       DataFormatter df = new DataFormatter(); 
       int Lastrow= sheet.getLastRowNum(); 
       int k = requirement_endColumn; 
       System.out.println("Column for requirement: " + k); 
       ArrayList myList = (ArrayList) new ArrayList(); 
       ArrayList myList1 = (ArrayList) new ArrayList(); 

       int lengthofInput= input_endColumn-input_startColumn; 
       int lengthofOutput= output_endColumn-output_startColumn; 
       int Lengthofloop; 

       if(lengthofInput>=lengthofOutput) 
       { 
        Lengthofloop=lengthofInput; 
       } 
       else 
       { 
        Lengthofloop=lengthofOutput; 
       } 
       int i; 
       int j; 
       String[][] localstore= new String[100][100]; 
       //java.util.Iterator<Row> rowIterator = sheet.iterator(); 

       for (i = 0; i <=Lastrow; i++) // Change this max value of i upto total row number in the file 
       { 

        for (int kk = 0; kk <=Lengthofloop; kk++) // Change this max value of i upto total row number in the file 
        { 
        newrow_Number=newrow_Number+1; 
        for (j = 0; j <=output_endColumn; j++) // Change this max value of i upto total row number in the file 
        { 

        Row readrow= sheet.getRow(i); 
        //Row writerow=sheet.getRow(i+j); 
        Cell readcell=readrow.getCell(j, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK); 
        //Cell writecell=writerow.getCell(i+j); 
        String datavalues = df.formatCellValue(readcell); 

        //writecell.setCellValue(""+datavalues); 

       // System.out.println(datavalues); 
        localstore[i][j]= datavalues; 
        System.out.println("I am i"+i + "---"+ "I am J"+j+localstore[i][j]); 

       //readrow= sheet.createRow(newrow_Number+1); 
       //readcell=readrow.getCell(j, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK); 
       //readcell.setCellValue("sanjay"+localstore[i][j]); 
       //Cell readcell=readrow.getCell(j, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK); 

        } 
       } 
       } 


       int jj=0; 

      for (i = 0; i <=Lastrow; i++) // Change this max value of i upto total row number in the file 
       { 

        for (int kk = 0; kk <=Lengthofloop; kk++) // Change this max value of i upto total row number in the file 
        { 
       //newrow_Number=newrow_Number+1; 
       Row readrow=sheet.createRow(newrow_Number); 
        for (j = 0; j <=requirement_endColumn+2; j++) // Change this max value of i upto total row number in the file 
        { 
         System.out.println("------------------------------"); 
         int m = sheet.getPhysicalNumberOfRows(); 
         System.out.println("getPhysicalNumberOfRows"+m); 
         Cell readcell=readrow.getCell(j+output_endColumn, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK); 
         String datavalues = df.formatCellValue(readcell); 
         readcell.setCellValue(""+localstore[i][j]); 
         if(j==requirement_endColumn+1) 
         { 
          //readcell=readrow.getCell(j+jj, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK); 

          readcell.setCellValue(""+localstore[i][j+kk]); 
          jj=jj+1; 
         } 
         else if(j==requirement_endColumn+2) 
         { 
          //readcell=readrow.getCell(j+jj, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK); 

          readcell.setCellValue(""+localstore[i][output_startColumn+kk]); 
          jj=jj+1; 
          //sheet.removeRow(sheet.getRow(newrow_Number)); 

         } 

        } 

       } 
        //newrow_Number=newrow_Number+1; 
       } 
      //sheet.removeRow(sheet.getRow(4)); 

       FileOutputStream out = new FileOutputStream(new File("C:/DataTransformation/op1.xlsx")); 
       workbook.write(out); 
       out.close(); 

}