Java HSSFSheet删除所有空行

Java HSSFSheet删除所有空行,java,apache,excel,apache-poi,poi-hssf,Java,Apache,Excel,Apache Poi,Poi Hssf,我有一个excel工作表,例如3,4和6行是空的(以前我为这些行调用sheet.removeow()。我总共有7行 现在,有了这些空行的索引,我想删除它们(移动它们) 当我调用函数时 shiftRows(行索引+1,lastRowNum,-1) 我越来越 java.lang.IllegalArgumentException: Minumum row number is 0 这是我的实现 List<Integer> rowsToRemove = [2,3,5]; for (In

我有一个excel工作表,例如3,4和6行是空的(以前我为这些行调用sheet.removeow()。我总共有7行

现在,有了这些空行的索引,我想删除它们(移动它们)

当我调用函数时 shiftRows(行索引+1,lastRowNum,-1) 我越来越

 java.lang.IllegalArgumentException: Minumum row number is 0
这是我的实现

 List<Integer> rowsToRemove = [2,3,5];
 for (Integer rowIndex : rowsToRemove) {
     removeRow(sheet, rowIndex);
 }
 int lastRowNum = sheet.getLastRowNum();
 for (Integer rowIndex : rowsToRemove) {
     shiftRow(sheet, rowIndex, lastRowNum);
 }


/**
* Remove a row by its index
* @param sheet a Excel sheet
* @param rowIndex a 0 based index of removing row
*/
public static void removeRow(HSSFSheet sheet, int rowIndex) {

   HSSFRow removingRow = sheet.getRow(rowIndex);
   if(removingRow!=null) {
       sheet.removeRow(removingRow);
   }
}

public static void shiftRow(HSSFSheet sheet, int rowIndex, int lastRowNum) {
   if (rowIndex >= 0 && rowIndex < lastRowNum) {
       sheet.shiftRows(rowIndex+1, lastRowNum, -1);
   }
}
List rowsToRemove=[2,3,5];
for(整数行索引:rowsToRemove){
清除程序(表格、行索引);
}
int lastRowNum=sheet.getLastRowNum();
for(整数行索引:rowsToRemove){
shiftRow(表、行索引、lastRowNum);
}
/**
*按其索引删除行
*@param-sheet一张Excel表格
*@param rowIndex删除行的基于0的索引
*/
公共静态空隙清除器OW(HSSF表,int rowIndex){
HSSFRow removingRow=sheet.getRow(行索引);
如果(removingRow!=null){
表1.removingRow(removingRow);
}
}
公共静态无效移位(HSSFSheet sheet,int rowIndex,int lastRowNum){
如果(rowIndex>=0&&rowIndex
你能帮我移走那些空行吗?? 我需要一个解决方案,将删除所有的空白行从excel文件


谢谢

仅当列中的单元格未合并时,此方案才有效。如果它们合并,poi中将出现异常