从特定行和列读取数据,并使用POI和Java写入不同的行和不同的列

时间:2017-12-16 13:54:18

标签: java excel loops multidimensional-array apache-poi

从这种格式中读取excel文件。

RA1 RB1 RC1   IP D1 IP E1   IP F1
RA2 RB2 RC2   IP D2 IP E2   IP F2
RA3 RB3 RC3   IP D3 IP E3   IP F3           

需要将excel写入此格式

RA1 RB1 RC1 IP D1       
RA1 RB1 RC1 IP E1       
RA1 RB1 RC1 IP F1       

RA2 RB2 RC2 IP D2       
RA2 RB2 RC2 IP E2       
RA2 RB2 RC2 IP E3       

RA3 RB3 RC3 IP F1       
RA3 RB3 RC3 IP F2       
RA3 RB3 RC3 IP F3       

有什么办法可以在不保存数组值的情况下完成。以下是我的循环,但没有得到所需的格式。

//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;
                // Loop to write data in seprate excel file in

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

0 个答案:

没有答案