[英]How to remove entire row basis of a String in the cell in apache POI
I am working on a xlsx file which contains two last rows in the end , i have to excluse the rows on the basis of the cell values it contains.ie My excel contains Total India(10%) and Total(10%) in its last two rows so i have to exclude the two rows on a basis of Total keyword. 我正在处理一个xlsx文件,该文件最后包含最后两行,我必须根据其包含的单元格值排除行。即我的excel在其其中包含Total India(10%)和Total(10%)最后两行,因此我必须根据Total关键字排除这两行。 I am using the following approach but its not working 我正在使用以下方法,但无法正常工作
@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;
}
}
Can anyone help ?? 谁能帮忙??
This is my little test program. 这是我的小测试程序。 It removes every row that contains the word total
. 它将删除包含单词total
每一行。 You will have to remove the code that you don't need (first 2 and last line probably) 您将必须删除不需要的代码(可能前两行和最后一行)
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));
Updated for your code: 已为您的代码更新:
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());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.