如何使用Apache POI转移特异性细胞? [英] How to shift specific cells using Apache POI?

查看:133
本文介绍了如何使用Apache POI转移特异性细胞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Excel表包含在同一张不同的区域,如:

  REGION1:REGION2:
约翰·2 A 1
约1 B 2
苏1℃3
苏2 D 4
爱丽丝5Ë5
鲍勃·1 F 6

欲添加新项目到这些区域中的一个而不会影响其他地区。我试着用 rowShift()方法,但它也删除整个行。是否有可以上下移动特定的细胞,并且可以将行插入这样的特定区域的方式:在我给出的例子,我想在REGION1增加一个排(也保留了所有旧行),它会变成:

  REGION1:REGION2:
的newval的newval A 1
约翰·2 B 2
约翰·1℃3
苏1个D 4
苏[2] E 5
爱丽丝5 F 6
鲍勃·1


解决方案

如果上面的问题仍然坚持,我可以给你同样的方法。

 公共静态无效shiftCells(INT startCellNo,诠释endCellNo,诠释shiftRowsBy,表板){    INT lastRowNum = sheet.getLastRowNum();
    的System.out.println(lastRowNum); // = 7
    // INT rowNumAfterAdding = lastRowNum + shiftRowsBy;
    对(INT的rowNum = lastRowNum;的rowNum大于0;的rowNum - ){
        行rowNew;
        如果(sheet.getRow((INT)的rowNum + shiftRowsBy)== NULL){
            rowNew = sheet.createRow((INT)的rowNum + shiftRowsBy);
        }
        rowNew = sheet.getRow((INT)的rowNum + shiftRowsBy);
        行rowOld = sheet.getRow(的rowNum);
        的System.out.println(RowNew是+的rowNum +和老行是+(INT)(+的rowNum shiftRowsBy));
        的System.out.println(startCellNo =+ startCellNo +endCellNo =+ endCellNo +shiftRowBy =+ shiftRowsBy);
        对于(INT cellNo = startCellNo; cellNo< = endCellNo; cellNo ++){
            (rowOld.getCell(cellNo).getStringCellValue()的toString())rowNew.createCell(cellNo).setCellValue;
            rowOld.getCell(cellNo).setCellValue();
            的System.out.println(工作+ cellNo);
        }
    }
}

My excel sheet contains different regions on the same sheet like:

region1:                region2:       
John       2            A         1  
John       1            B         2  
Sue        1            C         3  
Sue        2            D         4  
Alice      5            E         5  
Bob        1            F         6  

I want to add new items into one of these regions without affecting the other regions. I tried using rowShift() method but it is also deleting the complete rows. Is there any way that can shift the specific cells down and can insert rows into the specific regions like this: In my given example I want to add one more row in region1 (also retaining all old rows) and it will become:

region1:                region2:       
newval     newval       A         1  
John       2            B         2  
John       1            C         3  
Sue        1            D         4  
Sue        2            E         5  
Alice      5            F         6
Bob        1

解决方案

If the above issue still persisting, I can give you a method for the same.

public static void shiftCells(int startCellNo, int endCellNo, int shiftRowsBy, Sheet sheet){

    int lastRowNum = sheet.getLastRowNum();
    System.out.println(lastRowNum); //=7
    //      int rowNumAfterAdding = lastRowNum+shiftRowsBy;
    for(int rowNum=lastRowNum;rowNum>0;rowNum--){
        Row rowNew;
        if(sheet.getRow((int)rowNum+shiftRowsBy)==null){
            rowNew = sheet.createRow((int)rowNum+shiftRowsBy);
        }
        rowNew = sheet.getRow((int)rowNum+shiftRowsBy);
        Row rowOld = sheet.getRow(rowNum);
        System.out.println("RowNew is "+rowNum+" and Row Old is "+(int)(rowNum+shiftRowsBy));
        System.out.println("startCellNo = "+startCellNo+" endCellNo = "+endCellNo+" shiftRowBy = "+shiftRowsBy);
        for(int cellNo=startCellNo; cellNo<=endCellNo;cellNo++){
            rowNew.createCell(cellNo).setCellValue(rowOld.getCell(cellNo).getStringCellValue().toString());
            rowOld.getCell(cellNo).setCellValue("");
            System.out.println("working on " +cellNo);
        }
    }       
}

这篇关于如何使用Apache POI转移特异性细胞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆