Regarding the operation experience of deleting empty rows in POI: after the poi method removeRow() is called, the empty rows still exist.

Preface

I received a request to delete redundant blank rows in excel to keep the data compact and rigorous.

I searched the Internet for a technical explanation article about deleting empty rows in poi, which mentioned that the key method removeRow() needs to be called. I was full of joy and immediately used it, but the result after I used it was very bad. The blank line still existed. I searched hard for solutions online, and after several hours of struggle, I still couldn't get a satisfactory result, which was very distressing.

question

After the poi method removeRow() is called, the empty row still exists

solution

Desperate, I couldn't bear it anymore, so I could only hide in the back corridor, smoke a cigarette silently, and think about solutions. When a person is in trouble, the best solution is to relax. As soon as I relax and my mind is solved, I immediately solve the solution from another angle. An interesting phenomenon I discovered before is that the results obtained after filtering the query in Excel seem to be "filtering" out the unnecessary ones. In fact, they are not. In fact, the invalid rows have always existed and have not been deleted. So how does Excel achieve this? What about the filtering effect? Very simple, set the row height to 0. OK, the solution is out

Set the row height to 0 to achieve pseudo deletion of rows. The following is the key code:

	            //删除空行
	            for(int i=0;i<sheet.getLastRowNum();i++) {
	            	Row temp=sheet.getRow(i);
	            	boolean id=true;
	            	if(StringUtils.isNotBlank(temp.getCell(0)) || StringUtils.isNotBlank(temp.getCell(16))) {
	            		id=false;
	            	}
	            	
	            	if(id)
	            		temp.setHeight((short)0);
	            	
	            }

Summarize

Deliver tasks smoothly. Although the task was delivered, the problem was also solved. But from a technical perspective, I did not solve why the corresponding empty row was not deleted after the removeRow() method was called. This problem can only be left to the gods to deal with.

Guess you like

Origin blog.csdn.net/lijinquan2009/article/details/115158503