我正在处理xlsx文件,其中包含最后两行,我必须根据它包含的单元格值排除行。我的excel包含Total India(10%)和Total(10%) )在最后两行中,所以我必须在Total关键字的基础上排除这两行。我使用以下方法,但它无法正常工作
@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();
while (rowIterator.hasNext()) {
Row currentRow = rowIterator.next();
StringBuilder row = new StringBuilder();
for (int i = 0; i < currentRow.getLastCellNum(); i++) {
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;
}else {
Cell currentCell = currentRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String cellValue = excelManager.evalCell(currentCell);
if (!cellValue.isEmpty()) {
row.append(cellValue);
}
// trying to remove the rows containing total --------------------------------------->
int rowNums = sheet.getPhysicalNumberOfRows();
for(int k =0;k<rowNums;k++) {
currentRow = sheet.getRow(k);
if(currentRow !=null) {
for(int j=0;j<currentRow.getLastCellNum();j++) {
currentCell = currentRow.getCell(j);
if(currentCell !=null && currentCell.getCellTypeEnum().equals(CellType.STRING) && currentCell.getStringCellValue().toLowerCase().contains("total")){
sheet.removeRow(currentRow);
rowNums++;
}
}
}
}
//---------------------------------------------------------->
//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 :(得分:2)
这是我的小测试程序。它会删除包含单词total
的每一行。您将不得不删除不需要的代码(可能是前2行和最后一行)
XSSFWorkbook wb = new XSSFWorkbook(new File("test.xlsx"));
XSSFSheet sheet = wb.getSheetAt(0);
int rowNums = sheet.getPhysicalNumberOfRows();
for (int i = 0; i < rowNums; i++) {
XSSFRow r = sheet.getRow(i);
if (r != null) {
for (int j = 0; j < r.getLastCellNum(); j++) {
XSSFCell c = r.getCell(j);
if (c != null && c.getCellTypeEnum().equals(CellType.STRING)
&& c.getStringCellValue().toLowerCase().contains("total")) {
sheet.removeRow(r);
rowNums++;
}
}
}
}
wb.write(Files.newOutputStream(Paths.get("test2.xlsx"), StandardOpenOption.CREATE_NEW));
更新了您的代码:
List<String> rows = new ArrayList<String>();
int rowNums = sheet.getPhysicalNumberOfRows();
for (int k = 0; k < rowNums; k++) {
Row currentRow = sheet.getRow(k);
if (currentRow != null) {
StringBuilder row = new StringBuilder();
for (int i = 0; i < currentRow.getLastCellNum(); i++) {
if (currentRow.getRowNum() <= 11 && currentRow.getRowNum() != 8) {
continue;
} else {
Cell currentCell = currentRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String cellValue = excelManager.evalCell(currentCell);
if (!cellValue.isEmpty()) {
row.append(cellValue);
}
if (currentCell != null && currentCell.getCellTypeEnum().equals(CellType.STRING)
&& currentCell.getStringCellValue().toLowerCase().contains("total")) {
sheet.removeRow(currentRow);
rowNums++;
}
if (currentCell == null || currentCell.getCellType() == Cell.CELL_TYPE_BLANK) {
row.append("");
} else {
row.append(SEPARATOR);
}
}
}
if (!row.toString().isEmpty()) {
rows.add(row.toString());
}
}
}