将单行转换为多行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将单行转换为多行相关的知识,希望对你有一定的参考价值。

我希望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();
答案

我能够通过以下代码解决这个问题。

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

}

以上是关于将单行转换为多行的主要内容,如果未能解决你的问题,请参考以下文章

将单行转换为多行 Bigquery SQL

使用 Linux 工具将多行 csv 转换为单行

将某一多行的fasta文件转换为单行的fasta文件

将单行转换为多行

Postgres:将单行转换为多行(unpivot)

Spark:如何将多行转换为具有多列的单行?