parent
830ac4832b
commit
656fa3f296
11 changed files with 337 additions and 12 deletions
@ -0,0 +1,65 @@ |
||||
package org.springblade.modules.business.excel; |
||||
|
||||
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; |
||||
import com.alibaba.excel.write.style.row.AbstractRowHeightStyleStrategy; |
||||
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.CellType; |
||||
import org.apache.poi.ss.usermodel.Row; |
||||
import java.util.Iterator; |
||||
|
||||
public class CustomCellWriteHeightConfig extends AbstractRowHeightStyleStrategy { |
||||
|
||||
/** |
||||
* 默认高度 |
||||
*/ |
||||
private static final Integer DEFAULT_HEIGHT = 300; |
||||
|
||||
@Override |
||||
protected void setHeadColumnHeight(Row row, int relativeRowIndex) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
protected void setContentColumnHeight(Row row, int relativeRowIndex) { |
||||
Iterator<Cell> cellIterator = row.cellIterator(); |
||||
if (!cellIterator.hasNext()) { |
||||
return; |
||||
} |
||||
// 默认为 1 行高度
|
||||
int maxHeight = 1; |
||||
while (cellIterator.hasNext()) { |
||||
Cell cell = cellIterator.next(); |
||||
if (cell.getCellType() == CellType.STRING) { |
||||
String value = cell.getStringCellValue(); |
||||
for (int i = 0; i < value.length(); i += 10) { |
||||
if (i + 10 < value.length()) { |
||||
value = value.substring(0, i) + "\n" + value.substring(i, i + 10) + value.substring(i + 10); |
||||
} else { |
||||
value = value.substring(0, i) + "\n" + value.substring(i); |
||||
} |
||||
} |
||||
if (value.contains("\n")) { |
||||
int length = value.split("\n").length; |
||||
maxHeight = Math.max(maxHeight, length); |
||||
} |
||||
} |
||||
} |
||||
row.setHeight((short) (maxHeight * DEFAULT_HEIGHT)); |
||||
} |
||||
|
||||
@Override |
||||
public void afterRowDispose(RowWriteHandlerContext context) { |
||||
if (context.getHead() != null) { |
||||
if (ObjectUtils.isEmpty(context.getRelativeRowIndex())) { |
||||
return; |
||||
} |
||||
if (Boolean.TRUE.equals(context.getHead())) { |
||||
this.setHeadColumnHeight(context.getRow(), context.getRelativeRowIndex()); |
||||
} else { |
||||
this.setContentColumnHeight(context.getRow(), context.getRelativeRowIndex()); |
||||
} |
||||
|
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,77 @@ |
||||
package org.springblade.modules.business.excel; |
||||
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.Head; |
||||
import com.alibaba.excel.metadata.data.CellData; |
||||
import com.alibaba.excel.metadata.data.WriteCellData; |
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
||||
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy; |
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
public class CustomCellWriteWidthConfig extends AbstractColumnWidthStyleStrategy { |
||||
|
||||
private final Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<>(); |
||||
|
||||
@Override |
||||
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer integer, Boolean isHead) { |
||||
boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList); |
||||
if (needSetWidth) { |
||||
Map<Integer, Integer> maxColumnWidthMap = CACHE.computeIfAbsent(writeSheetHolder.getSheetNo(), k -> new HashMap<>()); |
||||
Integer columnWidth = this.dataLength(cellDataList, cell, isHead); |
||||
if (columnWidth > 0) { |
||||
if (columnWidth > 10) { |
||||
columnWidth = 20; |
||||
}else{ |
||||
columnWidth = 10; |
||||
} |
||||
Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex()); |
||||
if (maxColumnWidth == null || columnWidth > maxColumnWidth) { |
||||
maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth); |
||||
Sheet sheet = writeSheetHolder.getSheet(); |
||||
sheet.setColumnWidth(cell.getColumnIndex(), 256 * columnWidth + 184); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 计算长度 |
||||
* |
||||
* @param cellDataList |
||||
* @param cell |
||||
* @param isHead |
||||
* @return |
||||
*/ |
||||
private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) { |
||||
if (isHead) { |
||||
return cell.getStringCellValue().getBytes().length; |
||||
} else { |
||||
CellData<?> cellData = cellDataList.get(0); |
||||
CellDataTypeEnum type = cellData.getType(); |
||||
if (type == null) { |
||||
return -1; |
||||
} else { |
||||
switch (type) { |
||||
case STRING: |
||||
// 换行符(数据需要提前解析好)
|
||||
int index = cellData.getStringValue().indexOf("\n"); |
||||
return index != -1 ? |
||||
cellData.getStringValue().substring(0, index).getBytes().length + 1 : cellData.getStringValue().getBytes().length + 1; |
||||
case BOOLEAN: |
||||
return cellData.getBooleanValue().toString().getBytes().length; |
||||
case NUMBER: |
||||
return cellData.getNumberValue().toString().getBytes().length; |
||||
default: |
||||
return -1; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,37 @@ |
||||
package org.springblade.modules.business.excel; |
||||
|
||||
import com.alibaba.excel.metadata.Head; |
||||
import com.alibaba.excel.metadata.data.WriteCellData; |
||||
import com.alibaba.excel.write.handler.CellWriteHandler; |
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
||||
import com.alibaba.excel.write.metadata.holder.WriteTableHolder; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
import org.apache.poi.ss.util.CellRangeAddress; |
||||
|
||||
import java.util.List; |
||||
|
||||
@Data |
||||
public class MergeColumnStrategy implements CellWriteHandler { |
||||
|
||||
private Integer firstColumnIndex; |
||||
|
||||
private Integer lastColumnIndex; |
||||
|
||||
|
||||
public MergeColumnStrategy(Integer firstColumnIndex, Integer lastColumnIndex) { |
||||
this.firstColumnIndex = firstColumnIndex; |
||||
this.lastColumnIndex = lastColumnIndex; |
||||
} |
||||
|
||||
@Override |
||||
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { |
||||
|
||||
Sheet sheet = writeSheetHolder.getSheet(); |
||||
int lastRowNum = sheet.getLastRowNum(); |
||||
CellRangeAddress cellAddresses = new CellRangeAddress(lastRowNum, lastRowNum, firstColumnIndex, lastColumnIndex); |
||||
sheet.addMergedRegion(cellAddresses); |
||||
} |
||||
} |
||||
@ -0,0 +1,21 @@ |
||||
package org.springblade.modules.business.pojo.entity; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.annotation.write.style.ContentFontStyle; |
||||
import com.alibaba.excel.annotation.write.style.ContentStyle; |
||||
import com.alibaba.excel.enums.poi.BorderStyleEnum; |
||||
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum; |
||||
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum; |
||||
import lombok.Data; |
||||
import lombok.EqualsAndHashCode; |
||||
|
||||
@Data |
||||
@EqualsAndHashCode(callSuper = false) |
||||
public class BigTitle { |
||||
|
||||
@ContentFontStyle(fontHeightInPoints = 15, fontName = "微软雅黑") |
||||
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER, borderTop = BorderStyleEnum.THIN, borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN) |
||||
private String title; |
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,45 @@ |
||||
package org.springblade.modules.business.pojo.entity; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnore; |
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.annotation.write.style.ContentFontStyle; |
||||
import com.alibaba.excel.annotation.write.style.ContentStyle; |
||||
import com.alibaba.excel.enums.poi.BorderStyleEnum; |
||||
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum; |
||||
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum; |
||||
import lombok.Data; |
||||
import lombok.EqualsAndHashCode; |
||||
|
||||
@Data |
||||
@EqualsAndHashCode(callSuper = false) |
||||
@ContentFontStyle(fontHeightInPoints = 10, fontName = "微软雅黑") |
||||
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER, borderTop = BorderStyleEnum.THIN, borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN) |
||||
public class DeviceVO { |
||||
|
||||
@ExcelProperty("楼层") |
||||
private String floor; |
||||
|
||||
@ExcelProperty("房间") |
||||
private String dept; |
||||
|
||||
@ExcelProperty("设备名称") |
||||
private String deviceName; |
||||
|
||||
@ExcelProperty("单位") |
||||
private String unit; |
||||
|
||||
@ExcelProperty("数量") |
||||
private Integer num; |
||||
|
||||
@ExcelProperty("维保内容及频次") |
||||
private String checkContent; |
||||
|
||||
@ExcelProperty("巡检工艺") |
||||
private String craft; |
||||
|
||||
@ExcelProperty("巡检频次") |
||||
private String period; |
||||
|
||||
@ExcelIgnore |
||||
private String type; |
||||
} |
||||
Binary file not shown.
Loading…
Reference in new issue