将单行转换为多行

时间:2017-12-16 15:11:22

标签: java arrays excel loops apache-poi

我希望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 个答案:

答案 0 :(得分:0)

我可以通过以下代码来解决这个问题。

        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();

}