import org.apache.poi.ss.usermodel.Row; //導入方法依賴的package包/類
/**
* 寫入指定數據
*
* @param data 指定被寫入的數據
*/
private void writePerData(T data) {
if (this.limit > 0 && (this.currentRowInSheet + (isSkipBlankRow && isBlankLastRow ? 0 : 1)) >= this.limit) {
//用戶開啟了限製,並且此Sheet已經寫入了允許的最大行數則換頁
this.initSheet();
} else if (this.limit <= 0 &&
(this.currentRowInSheet + (isSkipBlankRow && isBlankLastRow ? 0 : 1)) >= ROW_MOST) {
//若用戶沒有開啟限製,但是此頁寫入已經超過了Excel規定的最大行數,強製換頁
this.initSheet();
}
//將data轉換成Map數據結構
Map mapBean = null;
if (data instanceof Map) {
mapBean = (Map) data;
} else {
mapBean = CommonUtils.toMap(data);
}
if (mapBean == null) {
throw new WriteExcelRuntimeException("Bean轉換為Map時發生錯誤");
}
if (this.properties == null || this.properties.isEmpty()) {
throw new WriteExcelRuntimeException("properties屬性不能為空");
}
if (!this.isSkipBlankRow) {
//未開啟跳過空行設置,即需要寫入空行
this.currentRowInSheet++;
Row row = this.currentSheetPO.createRow(this.currentRowInSheet);
row.setHeight(this.rowHeight < 0 ? -1 : this.rowHeight);
if (this.writePerRow(row, mapBean)) {
this.realRowInSheet++;
this.realRowInExcel++;
}
} else {
//開啟跳過空行設置,即不寫入空行
if (!this.isBlankLastRow) {
//若上一行不是空行則指針下移一行
this.currentRowInSheet++;
//創建一個新的Row
Row thisRow = this.currentSheetPO.createRow(this.currentRowInSheet);
thisRow.setHeight(this.rowHeight < 0 ? -1 : this.rowHeight);
//開始將數據寫入該行
if (this.writePerRow(thisRow, mapBean)) {
//若寫入了數據
this.realRowInSheet++;
this.realRowInExcel++;
//此行不是空行
this.isBlankLastRow = false;
} else {
//若沒有寫入任何數據,此行標記為空行
this.isBlankLastRow = true;
}
this.lastRow = thisRow;
} else {
//若上一行為空行則直接在上一行寫入數據
if (this.writePerRow(this.lastRow, mapBean)) {
//若寫入了數據
this.realRowInSheet++;
this.realRowInExcel++;
this.isBlankLastRow = false;
} else {
//若沒有寫入任何數據,此行還是標記為空行
this.isBlankLastRow = true;
}
}
}
}