增加:综合浏览系统数据表导出增加默认表头

master
Guo XIn 1 year ago
parent f3b10b3ba7
commit 5709386cee
  1. 42
      shandan-system/src/main/java/com/keyware/shandan/bianmu/export/MetaTableExport.java

@ -1,6 +1,7 @@
package com.keyware.shandan.bianmu.export;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSONArray;
@ -9,9 +10,14 @@ import com.keyware.shandan.bianmu.entity.MetadataBasicVo;
import com.keyware.shandan.bianmu.service.MetadataService;
import com.keyware.shandan.common.util.StringUtils;
import com.keyware.shandan.frame.config.component.AppContext;
import com.keyware.shandan.system.entity.SysOrg;
import com.keyware.shandan.system.entity.SysUser;
import com.keyware.shandan.system.service.SysUserService;
import org.apache.poi.ss.usermodel.Font;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
@ -21,6 +27,7 @@ public class MetaTableExport extends ExportProgress {
private final MetadataBasicVo metadata;
private final String fileName;
private final MetadataService metadataService;
private final SysUserService userService;
// 所有行的集合
private final List<List<String>> rowList = new ArrayList<>();
@ -32,6 +39,7 @@ public class MetaTableExport extends ExportProgress {
public MetaTableExport(String userId, MetadataBasicVo metadata) {
super(userId);
this.userService = AppContext.getContext().getBean(SysUserService.class);
this.metadataService = AppContext.getContext().getBean(MetadataService.class);
this.metadata = metadata;
String name = metadata.getMetadataComment();
@ -49,10 +57,13 @@ public class MetaTableExport extends ExportProgress {
// 添加第一行,字段列名注释集合,当做表格的第一行
rowList.add(colsArray.stream().map((json) -> {
JSONObject field = (JSONObject) json;
String comment = field.getString("comment");
String colName = field.getString("columnName");
colNameList.add(colName);
return colName + (StringUtils.hasText(comment) ? "[" + comment + "]" : "");
return colName;
}).collect(Collectors.toList()));
rowList.add(colsArray.stream().map((json) -> {
JSONObject field = (JSONObject) json;
return field.getString("comment");
}).collect(Collectors.toList()));
@ -73,8 +84,10 @@ public class MetaTableExport extends ExportProgress {
this.setTitle("正在写入缓存");
this.autoAddStep();
// 添加表头
addTableHeader(writer);
//一次性写出内容
writer.write(rows);
writer.write(rows, true);
this.setTitle("正在写入文件");
this.autoAddStep();
@ -92,6 +105,29 @@ public class MetaTableExport extends ExportProgress {
}
}
private void addTableHeader(BigExcelWriter writer) {
SysUser user = userService.getById(getUserId());
SysOrg org = user.getOrg();
String tableName = metadata.getMetadataComment();
if (!StringUtils.hasText(tableName)) {
tableName = metadata.getMetadataName();
}
int middleOffset = Math.max(colNameList.size() / 2, 1);
int rowIndex = writer.getCurrentRow();
writer.merge(rowIndex, rowIndex + 1, 0, 0, "时间", true)
.merge(rowIndex, rowIndex + 1, 1, middleOffset - 1, DateUtil.formatDateTime(new Date()), true)
.merge(rowIndex, rowIndex + 1, middleOffset, middleOffset, "表名", true)
.merge(rowIndex, rowIndex + 1, middleOffset + 1, colNameList.size() - 1, tableName, true)
// 另起一行
.passRows(2)
.merge(rowIndex + 2, rowIndex + 3, 0, 0, "姓名", true)
.merge(rowIndex + 2, rowIndex + 3, 1, middleOffset - 1, user.getUserName(), true)
.merge(rowIndex + 2, rowIndex + 3, middleOffset, middleOffset, "部门", true)
.merge(rowIndex + 2, rowIndex + 3, middleOffset + 1, colNameList.size() - 1, org.getOrgName(), true)
.passRows(2);
}
@Override
public long getStepTotal() {
return 4;