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;
}
}
}
}