导出表头宽度自适应
This commit is contained in:
parent
094d27fddf
commit
f1d3fea424
|
@ -11,6 +11,8 @@ import io.renren.modules.processForm.service.TAbilityApplicationService;
|
|||
import io.renren.modules.resource.dao.ResourceDao;
|
||||
import io.renren.modules.resource.excel.census.SelectApplyDeptDetailTypeCountListExcel;
|
||||
import io.renren.modules.resource.excel.census.SelectDeptDetailTypeCountListExcel;
|
||||
import io.renren.modules.resource.excel.census.config.CustomCellWriteHeightConfig;
|
||||
import io.renren.modules.resource.excel.census.config.CustomCellWriteWeightConfig;
|
||||
import io.renren.modules.resource.service.ResourceService;
|
||||
import io.renren.modules.resourceBrowse.service.ResourceBrowseService;
|
||||
import io.renren.modules.sys.dto.SysDeptDTO;
|
||||
|
@ -404,7 +406,10 @@ public class CensusController {
|
|||
response.setCharacterEncoding("utf-8");
|
||||
String fileName = URLEncoder.encode("按照部门_能力类型_组件类型统计能力使用情况_" + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
|
||||
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
||||
EasyExcel.write(response.getOutputStream(), SelectApplyDeptDetailTypeCountListExcel.class).sheet("使用情况").doWrite(date);
|
||||
EasyExcel.write(response.getOutputStream(), SelectApplyDeptDetailTypeCountListExcel.class)
|
||||
.registerWriteHandler(new CustomCellWriteWeightConfig())
|
||||
.registerWriteHandler(new CustomCellWriteHeightConfig())
|
||||
.sheet("使用情况").doWrite(date);
|
||||
}
|
||||
|
||||
@GetMapping("/selectDeptDetailTypeCountList")
|
||||
|
@ -446,7 +451,10 @@ public class CensusController {
|
|||
response.setCharacterEncoding("utf-8");
|
||||
String fileName = URLEncoder.encode("按照部门_能力类型_组件类型统计能力上架情况_" + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
|
||||
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
||||
EasyExcel.write(response.getOutputStream(), SelectDeptDetailTypeCountListExcel.class).sheet("上架情况").doWrite(date);
|
||||
EasyExcel.write(response.getOutputStream(), SelectDeptDetailTypeCountListExcel.class)
|
||||
.registerWriteHandler(new CustomCellWriteWeightConfig())
|
||||
.registerWriteHandler(new CustomCellWriteHeightConfig())
|
||||
.sheet("上架情况").doWrite(date);
|
||||
}
|
||||
|
||||
@GetMapping("/selectCensusResourceTable")
|
||||
|
@ -488,7 +496,10 @@ public class CensusController {
|
|||
response.setCharacterEncoding("utf-8");
|
||||
String fileName = URLEncoder.encode("部门上架资源及审批结果详细信息_" + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
|
||||
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
||||
EasyExcel.write(response.getOutputStream()).head(exportSelectCensusResourceTableHead()).sheet("上架情况").doWrite(date);
|
||||
EasyExcel.write(response.getOutputStream()).head(exportSelectCensusResourceTableHead())
|
||||
.registerWriteHandler(new CustomCellWriteWeightConfig())
|
||||
.registerWriteHandler(new CustomCellWriteHeightConfig())
|
||||
.sheet("上架情况").doWrite(date);
|
||||
}
|
||||
|
||||
private List<List<String>> exportSelectCensusResourceTableHead() {
|
||||
|
@ -551,7 +562,10 @@ public class CensusController {
|
|||
response.setCharacterEncoding("utf-8");
|
||||
String fileName = URLEncoder.encode("部门申请资源及审批结果详细信息_" + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
|
||||
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
||||
EasyExcel.write(response.getOutputStream()).head(exportSelectCensusApplyTableHead()).sheet("申请情况").doWrite(date);
|
||||
EasyExcel.write(response.getOutputStream()).head(exportSelectCensusApplyTableHead())
|
||||
.registerWriteHandler(new CustomCellWriteWeightConfig())
|
||||
.registerWriteHandler(new CustomCellWriteHeightConfig())
|
||||
.sheet("申请情况").doWrite(date);
|
||||
}
|
||||
|
||||
private List<List<String>> exportSelectCensusApplyTableHead() {
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
package io.renren.modules.resource.excel.census.config;
|
||||
|
||||
|
||||
import com.alibaba.excel.write.style.row.AbstractRowHeightStyleStrategy;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
|
||||
public class CustomCellWriteHeightConfig extends AbstractRowHeightStyleStrategy {
|
||||
/**
|
||||
* 默认高度
|
||||
*/
|
||||
private static final Integer DEFAULT_HEIGHT = 300;
|
||||
|
||||
/**
|
||||
* Sets the height of header
|
||||
*
|
||||
* @param row
|
||||
* @param relativeRowIndex
|
||||
*/
|
||||
@Override
|
||||
protected void setHeadColumnHeight(Row row, int relativeRowIndex) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the height of content
|
||||
*
|
||||
* @param row
|
||||
* @param relativeRowIndex
|
||||
*/
|
||||
@Override
|
||||
protected void setContentColumnHeight(Row row, int relativeRowIndex) {
|
||||
Iterator<Cell> cellIterator = row.cellIterator();
|
||||
if (!cellIterator.hasNext()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 默认为 1行高度
|
||||
Integer maxHeight = 1;
|
||||
while (cellIterator.hasNext()) {
|
||||
Cell cell = cellIterator.next();
|
||||
switch (cell.getCellTypeEnum()) {
|
||||
case STRING:
|
||||
if (cell.getStringCellValue().indexOf("\n") != -1) {
|
||||
int length = cell.getStringCellValue().split("\n").length;
|
||||
maxHeight = Math.max(maxHeight, length);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
row.setHeight((short) (maxHeight * DEFAULT_HEIGHT));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package io.renren.modules.resource.excel.census.config;
|
||||
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.CellData;
|
||||
import com.alibaba.excel.metadata.Head;
|
||||
import com.alibaba.excel.util.CollectionUtils;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
|
||||
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
|
||||
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 CustomCellWriteWeightConfig extends AbstractColumnWidthStyleStrategy {
|
||||
private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Sets the column width when head create
|
||||
*
|
||||
* @param writeSheetHolder
|
||||
* @param cellDataList
|
||||
* @param cell
|
||||
* @param head
|
||||
* @param relativeRowIndex
|
||||
* @param isHead
|
||||
*/
|
||||
@Override
|
||||
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
|
||||
boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
|
||||
if (needSetWidth) {
|
||||
Map<Integer, Integer> maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo());
|
||||
if (maxColumnWidthMap == null) {
|
||||
maxColumnWidthMap = new HashMap<>();
|
||||
CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
|
||||
}
|
||||
|
||||
Integer columnWidth = this.dataLength(cellDataList, cell, isHead);
|
||||
if (columnWidth >= 0) {
|
||||
if (columnWidth > 254) {
|
||||
columnWidth = 254;
|
||||
}
|
||||
|
||||
Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
|
||||
if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
|
||||
maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
|
||||
Sheet sheet = writeSheetHolder.getSheet();
|
||||
sheet.setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算长度
|
||||
*
|
||||
* @param cellDataList
|
||||
* @param cell
|
||||
* @param isHead
|
||||
* @return
|
||||
*/
|
||||
private Integer dataLength(List<CellData> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue