利用poi開源jar包操作Excel時刪除行內容與直接刪除行的區別


一般情況下,刪除行時會面臨兩種情況:刪除行內容但保留行位置、整行刪除(刪除后下方單元格上移)。對應的刪除方法分別是:

 

void removeRow(Row row)//Remove a row from this sheet. All cells contained in the row are removed as well
public void shiftRows(int startRow,int endRow,int n)//Shifts rows between startRow and endRow n number of rows. If you use a negative number, it will shift rows up. Code ensures that rows don't wrap around.

 

示例代碼:

以下代碼是使用removeRow()方法刪除行內容但保留行位置。代碼從d:\test.xls中的第一個sheet中刪除了第一行。需要注意的是,改變是需要在workbook.write之后才生效的。

 

[java]  view plain  copy
 
  1. import org.apache.poi.hssf.usermodel.*;  
  2. import java.io.*;  
  3. public class testTools{  
  4.      public static void main(String[] args){  
  5.         try {  
  6.             FileInputStream is = new FileInputStream("d://test.xls");  
  7.             HSSFWorkbook workbook = new HSSFWorkbook(is);  
  8.             HSSFSheet sheet = workbook.getSheetAt(0);  
  9.             HSSFRow row = sheet.getRow(0);  
  10.             sheet.removeRow(row);  
  11.             FileOutputStream os = new FileOutputStream("d://test.xls");  
  12.             workbook.write(os);  
  13.             is.close();  
  14.             os.close();  
  15.         } catch (Exception e) {   
  16.             e.printStackTrace();  
  17.         }  
  18.      }  
  19. }  



 

以下代碼是使用shiftRow實現刪除整行的效果。同樣,也是需要在進行workbook.write后才會生效。

 

 

[java]  view plain  copy
 
  1. import org.apache.poi.hssf.usermodel.*;  
  2. import java.io.*;  
  3. public class testTools{  
  4.      public static void main(String[] args){  
  5.         try {  
  6.             FileInputStream is = new FileInputStream("d://test.xls");  
  7.             HSSFWorkbook workbook = new HSSFWorkbook(is);  
  8.             HSSFSheet sheet = workbook.getSheetAt(0);  
  9.             sheet.shiftRows(1, 4, -1);//刪除第一行到第四行,然后使下方單元格上移  
  10.             FileOutputStream os = new FileOutputStream("d://test.xls");  
  11.             workbook.write(os);  
  12.             is.close();  
  13.             os.close();  
  14.         } catch (Exception e) {   
  15.             e.printStackTrace();  
  16.         }  
  17.      }  
  18. }  

 

自己寫的一個包裝好了的刪除excel行的方法(利用shiftRows上移來刪除行):

 

[java]  view plain  copy
 
    1.  /** 
    2.  * Remove a row by its index 
    3.  * @param sheet a Excel sheet 
    4.  * @param rowIndex a 0 based index of removing row 
    5.  */  
    6. public static void removeRow(HSSFSheet sheet, int rowIndex) {  
    7.     int lastRowNum=sheet.getLastRowNum();  
    8.     if(rowIndex>=0&&rowIndex<lastRowNum)  
    9.         sheet.shiftRows(rowIndex+1,lastRowNum,-1);//將行號為rowIndex+1一直到行號為lastRowNum的單元格全部上移一行,以便刪除rowIndex行  
    10.     if(rowIndex==lastRowNum){  
    11.         HSSFRow removingRow=sheet.getRow(rowIndex);  
    12.         if(removingRow!=null)  
    13.             sheet.removeRow(removingRow);  
    14.     }  
    15. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM