天天看點

POI(excel) - ROW

row : 電子表格中行的進階表現形式,隻有擁有cell的row才應該被添加進sheet(not forbid);

row index 從 0 開始。即,rowNum 從 0 開始。

HSSFSheet sheetFirst = hssfWorkbook.getSheetAt(0);      

(1)擷取實際存在的有效的row 數量

即,row不為null,即使某行row中cell内容為空格,row同樣有效!

int physicalNumberOfRows = sheetFirst.getPhysicalNumberOfRows();      

(2)得到有效的最後一行rowNum

int lastRowNum = sheetFirst.getLastRowNum();      

注意區分​

​getPhysicalNumberOfRows();​

​​和​

​getLastRowNum();​

​。前者為sheet中實際意義上存在的row,後者為sheet中最後一行rowNum。

需要說明的是,如果sheet為空,即沒有實際意義上的row。那麼二者都會傳回0。但是如果隻有一行,前者傳回 1 ,後者傳回0。

故,使用getLastRowNum()的前提是sheet至少有兩行有效資料,才能說明資料的正确性!!!

(3)得到有效的第一行的rowNum

int firstRowNum = sheetFirst.getFirstRowNum();      

得到第一個有效行的rowNum。

和上面擷取最後一行rowNum存在同樣問題:

如果隻有一行且該行在第一行,那麼其傳回 0 ;但是如果sheet為空,其同樣傳回為 0 。

使用需謹慎,判斷sheet為空建議使用​

​getPhysicalNumberOfRows()​

​。

(4)根據rowNum建立row

HSSFRow row = sheetFirst.createRow(rowNum);      

(5)根據rowIndex 擷取row

HSSFRow row = sheetFirst.getRow(ronIndex);      

(6)移除指定row

即,移除該行所有cell和row自身對象。

HSSFRow row = sheetFirst.getRow(1);
sheetFirst.removeRow(row);      

移除後,row将會變成null;getPhysicalNumberOfRows();将會 減 1。

POI(excel) - ROW

(7)擷取表格打開時,可見的第一行rowNum

short topRow = sheetFirst.getTopRow();      

如下圖所示,此時topRow = 1;實際上有效的第一行index為0,隻是被隐藏了。

int firstRowNum = sheetFirst.getFirstRowNum();
//此時 firstRowNum = 0 !

int physicalNumberOfRows = sheetFirst.getPhysicalNumberOfRows();
//此時 physicalNumberOfRows  = 2 !      
POI(excel) - ROW

(8)移動row,移動some row–隻copy 單元格值和單元格樣式,不改變行高

Parameters:

  • startRow the row to start shifting
  • endRow the row to end shifting
  • n the number of rows to shift
  • 最後一句話意思是:rows需要移動的行數,為正,則向下移動;為負則向上移動

區間:完全閉區間[startRow,endRow]。

startRow,endRow均代表index(rowNum),基數為0。

sheetFirst.shiftRows(startRow, endRow, n);
//等價于
sheetFirst.shiftRows(startRow, endRow, n, false, false);
//等價于
sheetFirst.shiftRows(startRow, endRow, n, false, false,true);      

示例一:

如 startRow= 1,endRow = 3,n = 2。則表示将 2-4行向下移動兩行,結果是第二行覆寫第4行,第三行覆寫第5行,第四行覆寫第6行,原第二行變為空行(row!=null ,即,row有效但cell為null),。

int startRow=1;
        int endRow = 3;
        int n=2;
        sheetFirst.shiftRows(startRow, endRow, n);      
POI(excel) - ROW
POI(excel) - ROW

如果移動範圍超出了sheet中row的有效範圍,将會在移動目标行建立新行:

示例二:

int startRow=4;
        int endRow = 6;
        int n=8;
        sheetFirst.shiftRows(startRow, endRow, n);      
POI(excel) - ROW
POI(excel) - ROW

此時 sheet有效row數量仍為7!!

如果移動一不存在的row到sheet有效row範圍外,相當于建立了一cell為null的row(row != null),此時​

​getPhysicalNumberOfRows()​

​+1。

如果移動一不存在的row到sheet有效row範圍内,那麼目标位置row将會被覆寫(cell變為null),此時​

​getPhysicalNumberOfRows()​

​不變。

向上移動時需要注意,rowNum最小值(基數)為0。也就意味着你不能移動到 rowNum = -1的位置。

示例如下:

第2-4行向上移動一行,原第4行變為空行(row !=null ,cell 為 null)。

int startRow=1;
        int endRow = 3;
        int n=-1;
        sheetFirst.shiftRows(startRow, endRow, n);      
POI(excel) - ROW
POI(excel) - ROW

原第四行(rowNum=3)如下圖所示;

POI(excel) - ROW

(9)移動行的時候選擇是否更改行高

Parameters:

  • startRow the row to start shifting
  • endRow the row to end shifting
  • n the number of rows to shift
  • copyRowHeight whether to copy the row height during the shift
  • resetOriginalRowHeight whether to set the original row’s height to the default
sheetFirst.shiftRows(startRow, endRow, n, copyRowHeight, resetOriginalRowHeight);      

示例 :

int startRow=1;
        int endRow = 3;
        int n=-1;
        sheetFirst.shiftRows(startRow, endRow, n, true, true);      
POI(excel) - ROW

如下圖所示,移動過程中附帶了行高,且将原始行(被移動的行)設定了預設行高(因為234行中隻有4行沒有被覆寫,故第4行設定預設行高):

POI(excel) - ROW

(10)移動的時候在9的基礎上增加是否同時移動單元格增加的注釋

sheetFirst.shiftRows(startRow, endRow, n, copyRowHeight, resetOriginalRowHeight, moveComments);      

示例:

int startRow=1;
        int endRow = 3;
        int n=8;
        sheetFirst.shiftRows(startRow, endRow, n, true, true, true);