如何在apache POI中交换xlsx文件中的列

时间:2018-02-09 14:44:15

标签: java excel apache apache-poi

我正在使用xlsx文件,我必须将下行中的值附加到上一行。我还必须从输出文件中跳过该行。首先我要给出excel文件屏幕

enter image description here

我必须在Name6行下面追加值,即在标题中会有Name1,Name2,Name3 ....... Name6admin | Name6loc,Name7。

现在,自单元格合并后,单元格poi将输出作为合并单元格的一部分,在Name6.i.e Name6 |合并单元格的一部分之后。我想按照我的提法修改它。 现在我的代码工作就像我将拥有从Name1到Name7的所有行以及它们保存的所有下面的值。事情是我在一个文本文件中生成输出,该文件不能包含Name1,Name2下面的行。 ..但是我也必须从中追加价值。我是我的代码

 @Override
public List<String> processSheet(Sheet sheet) {
    ActivityLauncher.logConsoleMsg("Processing sheet " + sheet.getSheetName() + " started");
    List<String> rows = new ArrayList<String>();
    Iterator<Row> rowIterator = sheet.iterator();
    final int startCell=0;
    while (rowIterator.hasNext()) {
        Row currentRow = rowIterator.next();
        StringBuilder row = new StringBuilder();
        for (int i = 0; i < currentRow.getLastCellNum(); i++) {

//skipping the rows which i can not contains in the output 
if(currentRow.getRowNum()==0 || currentRow.getRowNum()==1|| currentRow.getRowNum()==2|| currentRow.getRowNum()==3|| currentRow.getRowNum()==4|| currentRow.getRowNum()==5|| currentRow.getRowNum()==6|| currentRow.getRowNum()==7|| currentRow.getRowNum()==9|| currentRow.getRowNum()==10|| currentRow.getRowNum()==11) {
                continue;
            }

            //removing the last three rows containing total
            if (currentRow.getLastCellNum() < 0 || currentRow.getCell(startCell) == null || "".equals(currentRow.getCell(startCell).getStringCellValue()) || (currentRow.getCell(startCell).getStringCellValue() != null && currentRow.getCell(startCell).getStringCellValue().contains("Total"))) {
                continue;
           }

            else {
            Cell currentCell = currentRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);

            String cellValue = excelManager.evalCell(currentCell);


            if (!cellValue.isEmpty()) {
                row.append(cellValue);
            }


            //adjusting the cell values with the | or BLANK 
            if(currentCell == null || currentCell.getCellType() == Cell.CELL_TYPE_BLANK) {
                row.append("");
            }else {
            row.append(SEPARATOR);
             }
            }
        }

        if (!row.toString().isEmpty()) {
            rows.add(row.toString());
        }
    }
    ActivityLauncher.logConsoleMsg("Processing sheet " + sheet.getSheetName() + " completed");
    return rows;
}

0 个答案:

没有答案