mirror of
https://github.com/thousmile/molly-multi-tenant.git
synced 2025-12-30 04:32:26 +00:00
1.修改ExcelUtils导出
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package com.xaaef.molly.common.util;
|
package com.xaaef.molly.common.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.date.DatePattern;
|
import cn.hutool.core.date.DatePattern;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
@@ -16,6 +18,7 @@ import org.springframework.http.ResponseEntity;
|
|||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -86,12 +89,7 @@ public class ExcelUtils {
|
|||||||
*/
|
*/
|
||||||
private static <T> void pageExport(HSSFWorkbook workbook, String sheetName, List<T> rangeData) {
|
private static <T> void pageExport(HSSFWorkbook workbook, String sheetName, List<T> rangeData) {
|
||||||
// 反射获取对象属性,和描述名称
|
// 反射获取对象属性,和描述名称
|
||||||
var fieldNames = Arrays.stream(ReflectUtil.getFields(rangeData.getFirst().getClass()))
|
var fieldNames = getFieldSchemaNames(CollectionUtil.getFirst(rangeData));
|
||||||
.map(field -> {
|
|
||||||
var ann = field.getAnnotation(Schema.class);
|
|
||||||
return ann == null ? StrUtil.EMPTY : ann.description();
|
|
||||||
})
|
|
||||||
.collect(Collectors.toCollection(LinkedList::new));
|
|
||||||
//创建工作表,指定工作表名称
|
//创建工作表,指定工作表名称
|
||||||
var sheet1 = workbook.createSheet(sheetName);
|
var sheet1 = workbook.createSheet(sheetName);
|
||||||
sheet1.setDefaultColumnWidth(20);
|
sheet1.setDefaultColumnWidth(20);
|
||||||
@@ -112,9 +110,11 @@ public class ExcelUtils {
|
|||||||
cell0.setCellValue(StrUtil.EMPTY);
|
cell0.setCellValue(StrUtil.EMPTY);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (obj instanceof Long o1) {
|
if (obj instanceof Number o1) {
|
||||||
cell0.setCellValue(o1);
|
cell0.setCellValue(o1.toString());
|
||||||
} else if (obj instanceof Integer o1) {
|
}
|
||||||
|
/*
|
||||||
|
else if (obj instanceof Integer o1) {
|
||||||
cell0.setCellValue(o1);
|
cell0.setCellValue(o1);
|
||||||
} else if (obj instanceof Short o1) {
|
} else if (obj instanceof Short o1) {
|
||||||
cell0.setCellValue(o1);
|
cell0.setCellValue(o1);
|
||||||
@@ -122,10 +122,12 @@ public class ExcelUtils {
|
|||||||
cell0.setCellValue(o1);
|
cell0.setCellValue(o1);
|
||||||
} else if (obj instanceof Double o1) {
|
} else if (obj instanceof Double o1) {
|
||||||
cell0.setCellValue(o1);
|
cell0.setCellValue(o1);
|
||||||
} else if (obj instanceof String o1) {
|
|
||||||
cell0.setCellValue(o1);
|
|
||||||
} else if (obj instanceof Byte o1) {
|
} else if (obj instanceof Byte o1) {
|
||||||
cell0.setCellValue(o1);
|
cell0.setCellValue(o1);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
else if (obj instanceof String o1) {
|
||||||
|
cell0.setCellValue(o1);
|
||||||
} else if (obj instanceof Boolean o1) {
|
} else if (obj instanceof Boolean o1) {
|
||||||
cell0.setCellValue(o1);
|
cell0.setCellValue(o1);
|
||||||
} else if (obj instanceof Date o1) {
|
} else if (obj instanceof Date o1) {
|
||||||
@@ -138,16 +140,58 @@ public class ExcelUtils {
|
|||||||
cell0.setCellValue(o1.format(DatePattern.NORM_DATETIME_FORMATTER));
|
cell0.setCellValue(o1.format(DatePattern.NORM_DATETIME_FORMATTER));
|
||||||
} else if (obj instanceof ZonedDateTime o1) {
|
} else if (obj instanceof ZonedDateTime o1) {
|
||||||
cell0.setCellValue(o1.format(DatePattern.NORM_DATETIME_FORMATTER));
|
cell0.setCellValue(o1.format(DatePattern.NORM_DATETIME_FORMATTER));
|
||||||
} else if (obj instanceof Iterable<?> || obj instanceof Map<?, ?> || obj instanceof Enum<?> || obj.getClass().isArray()) {
|
} else if (obj instanceof Iterable<?> || obj.getClass().isArray()) {
|
||||||
|
cell0.setCellValue(JsonUtils.toFormatJson(obj));
|
||||||
|
} else if (obj instanceof Map<?, ?> || obj instanceof Enum<?>) {
|
||||||
cell0.setCellValue(JsonUtils.toFormatJson(obj));
|
cell0.setCellValue(JsonUtils.toFormatJson(obj));
|
||||||
} else {
|
} else {
|
||||||
cell0.setCellValue(obj.toString());
|
var fieldMaps = getFieldNameAndSchema(obj);
|
||||||
|
var valMaps = BeanUtil.beanToMap(obj);
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
valMaps.forEach((key, val) -> sb.append(StrUtil.format("{} = {} \r\n", fieldMaps.get(key), val)));
|
||||||
|
cell0.setCellValue(sb.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 反射获取对象属性,和描述名称
|
||||||
|
*
|
||||||
|
* @author WangChenChen
|
||||||
|
* @version 2.0
|
||||||
|
* @date 2023/11/29 18:04
|
||||||
|
*/
|
||||||
|
private static List<String> getFieldSchemaNames(Object obj) {
|
||||||
|
return Arrays.stream(ReflectUtil.getFields(obj.getClass()))
|
||||||
|
.map(field -> {
|
||||||
|
var ann = field.getAnnotation(Schema.class);
|
||||||
|
return ann == null ? field.getName() : ann.description();
|
||||||
|
})
|
||||||
|
.collect(Collectors.toCollection(LinkedList::new));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 反射获取对象属性,和描述名称
|
||||||
|
*
|
||||||
|
* @author WangChenChen
|
||||||
|
* @version 2.0
|
||||||
|
* @date 2023/11/29 18:04
|
||||||
|
*/
|
||||||
|
private static Map<String, String> getFieldNameAndSchema(Object obj) {
|
||||||
|
return Arrays.stream(ReflectUtil.getFields(obj.getClass()))
|
||||||
|
.collect(
|
||||||
|
Collectors.toMap(Field::getName, field -> {
|
||||||
|
var ann = field.getAnnotation(Schema.class);
|
||||||
|
return ann == null ? field.getName() : ann.description();
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取一个 Workbook
|
* 获取一个 Workbook
|
||||||
*
|
*
|
||||||
|
|||||||
Binary file not shown.
@@ -10,8 +10,11 @@ import com.xaaef.molly.auth.jwt.JwtSecurityUtils;
|
|||||||
import com.xaaef.molly.common.consts.JwtConst;
|
import com.xaaef.molly.common.consts.JwtConst;
|
||||||
import com.xaaef.molly.common.util.ExcelUtils;
|
import com.xaaef.molly.common.util.ExcelUtils;
|
||||||
import com.xaaef.molly.common.util.JsonUtils;
|
import com.xaaef.molly.common.util.JsonUtils;
|
||||||
import com.xaaef.molly.perms.entity.PmsRole;
|
import com.xaaef.molly.internal.dto.OperateUserDTO;
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import liquibase.integration.spring.MultiTenantSpringLiquibase;
|
import liquibase.integration.spring.MultiTenantSpringLiquibase;
|
||||||
|
import lombok.*;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.core.io.FileSystemResourceLoader;
|
import org.springframework.core.io.FileSystemResourceLoader;
|
||||||
|
|
||||||
@@ -128,15 +131,46 @@ public class NoSpringTests {
|
|||||||
System.out.println(formatJson);
|
System.out.println(formatJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Builder
|
||||||
|
@Accessors(chain = true)
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public static class TestExportEntity implements java.io.Serializable {
|
||||||
|
|
||||||
|
@Schema(description = "ID")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Schema(description = "名称")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Schema(description = "爱好")
|
||||||
|
protected List<String> hobby;
|
||||||
|
|
||||||
|
@Schema(description = "创建日期")
|
||||||
|
protected LocalDate createDate;
|
||||||
|
|
||||||
|
@Schema(description = "创建时间")
|
||||||
|
protected LocalTime createTime;
|
||||||
|
|
||||||
|
@Schema(description = "创建日期时间")
|
||||||
|
protected LocalDateTime createDateTime;
|
||||||
|
|
||||||
|
@Schema(description = "创建人信息")
|
||||||
|
protected OperateUserDTO createUserEntity;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test12() throws IOException {
|
public void test12() throws IOException {
|
||||||
var treeNodes = List.of(
|
var treeNodes = List.of(
|
||||||
new PmsRole(1001L, "角色1", 1L, "角色1问问去"),
|
new TestExportEntity(1001L, "角色1", List.of("A1", "B1"), LocalDate.now(), LocalTime.now(), LocalDateTime.now(), new OperateUserDTO(1L, "a", "张三")),
|
||||||
new PmsRole(1002L, "角色2", 2L, "角色2QBGRBR"),
|
new TestExportEntity(1002L, "角色2", List.of("A2", "B2"), LocalDate.now(), LocalTime.now(), LocalDateTime.now(), new OperateUserDTO(2L, "b", "李四")),
|
||||||
new PmsRole(1003L, "角色3", 3L, "角色3GRNRY"),
|
new TestExportEntity(1003L, "角色3", List.of("A3", "B3"), LocalDate.now(), LocalTime.now(), LocalDateTime.now(), new OperateUserDTO(3L, "c", "王五")),
|
||||||
new PmsRole(1004L, "角色4", 4L, "角色4fwef"),
|
new TestExportEntity(1004L, "角色4", List.of("A4", "B4"), LocalDate.now(), LocalTime.now(), LocalDateTime.now(), new OperateUserDTO(4L, "d", "陈六")),
|
||||||
new PmsRole(1005L, "角色5", 5L, "角色5这个人")
|
new TestExportEntity(1005L, "角色5", List.of("A5", "B5"), LocalDate.now(), LocalTime.now(), LocalDateTime.now(), new OperateUserDTO(5L, "e", "赵七"))
|
||||||
);
|
);
|
||||||
var entity = ExcelUtils.deviceExport("aa.xlsx", treeNodes);
|
var entity = ExcelUtils.deviceExport("aa.xlsx", treeNodes);
|
||||||
var file = Files.createFile(Path.of("aa.xlsx")).toFile();
|
var file = Files.createFile(Path.of("aa.xlsx")).toFile();
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@DependsOn(value = "projectTableRunner")
|
@DependsOn({"projectTableRunner"})
|
||||||
@Order(Integer.MIN_VALUE + 1)
|
@Order(Integer.MIN_VALUE + 1)
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class TenantTableRunner {
|
public class TenantTableRunner {
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ public class LiquibaseConfig {
|
|||||||
liquibase1.setResourceLoader(new DefaultResourceLoader());
|
liquibase1.setResourceLoader(new DefaultResourceLoader());
|
||||||
liquibase1.setChangeLog(multiTenantProperties.getChangeLog());
|
liquibase1.setChangeLog(multiTenantProperties.getChangeLog());
|
||||||
liquibase1.setContexts(multiTenantProperties.getContexts());
|
liquibase1.setContexts(multiTenantProperties.getContexts());
|
||||||
|
liquibase1.setClearCheckSums(false);
|
||||||
return liquibase1;
|
return liquibase1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user