java row.size_Java Row.setHeight方法代碼示例

该代码段展示了如何在Java中使用Apache POI库处理Excel数据的写入操作,包括检查行限制和是否跳过空行。当达到用户设定的行数限制或Excel最大行数时,会自动创建新工作表。同时,它将对象转换为Map并写入到Excel表格中,处理了空行的写入策略。
摘要由CSDN通过智能技术生成

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;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值