【新增】 监控管理导出Excel
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package pc.exam.pp.framework.excel.core.util;
|
||||
|
||||
import com.alibaba.excel.ExcelWriter;
|
||||
import com.alibaba.excel.write.metadata.WriteSheet;
|
||||
import lombok.Getter;
|
||||
import pc.exam.pp.framework.common.util.http.HttpUtils;
|
||||
import pc.exam.pp.framework.excel.core.handler.SelectSheetWriteHandler;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
@@ -43,6 +46,80 @@ public class ExcelUtils {
|
||||
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
||||
}
|
||||
|
||||
// 多 Sheet 写法
|
||||
public static <T> void writeMulti(HttpServletResponse response,
|
||||
String filename,
|
||||
List<SheetSpec<?>> sheets) throws IOException {
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.addHeader("Content-Disposition", "attachment;filename=" + HttpUtils.encodeUtf8(filename));
|
||||
|
||||
try (ExcelWriter writer = EasyExcel.write(response.getOutputStream())
|
||||
// 你原来注册的 handler/converter 继续加在这里
|
||||
// .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
||||
// .registerConverter(new SecondsStringToTimeConverter())
|
||||
.build()) {
|
||||
|
||||
for (SheetSpec<?> spec : sheets) {
|
||||
if (spec.getBeanClass() != null) {
|
||||
// 实体表头
|
||||
WriteSheet sheet = EasyExcel.writerSheet(spec.getSheetName())
|
||||
.head(spec.getBeanClass())
|
||||
.build();
|
||||
writer.write((List<?>) spec.getBeanData(), sheet);
|
||||
} else {
|
||||
// 动态表头
|
||||
WriteSheet sheet = EasyExcel.writerSheet(spec.getSheetName())
|
||||
.head(spec.getHead())
|
||||
.build();
|
||||
writer.write(spec.getRows(), sheet);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** 每个 Sheet 的描述 */
|
||||
@Getter
|
||||
public static class SheetSpec<T> {
|
||||
private final String sheetName;
|
||||
|
||||
// 实体方式
|
||||
private final Class<T> beanClass; // 非空 => 实体表头
|
||||
private final List<T> beanData;
|
||||
|
||||
// 动态方式
|
||||
private final List<List<String>> head; // 非空 => 动态表头
|
||||
private final List<List<Object>> rows;
|
||||
|
||||
private SheetSpec(String sheetName, Class<T> beanClass, List<T> beanData,
|
||||
List<List<String>> head, List<List<Object>> rows) {
|
||||
this.sheetName = sheetName;
|
||||
this.beanClass = beanClass;
|
||||
this.beanData = beanData;
|
||||
this.head = head;
|
||||
this.rows = rows;
|
||||
}
|
||||
|
||||
/** 实体表头 */
|
||||
public static <T> SheetSpec<T> ofBean(String sheetName, Class<T> beanClass, List<T> data) {
|
||||
return new SheetSpec<>(sheetName, beanClass, data, null, null);
|
||||
}
|
||||
|
||||
/** 动态表头 */
|
||||
public static <T> SheetSpec<T> ofDynamic(String sheetName, List<List<String>> head, List<List<Object>> rows) {
|
||||
return new SheetSpec<>(sheetName, null, null, head, rows);
|
||||
}
|
||||
|
||||
// === getters(关键!)===
|
||||
public String getSheetName() { return sheetName; }
|
||||
public Class<T> getBeanClass() { return beanClass; }
|
||||
public List<T> getBeanData() { return beanData; }
|
||||
public List<List<String>> getHead() { return head; }
|
||||
public List<List<Object>> getRows() { return rows; }
|
||||
}
|
||||
|
||||
|
||||
public static <T> List<T> read(MultipartFile file, Class<T> head) throws IOException {
|
||||
return EasyExcel.read(file.getInputStream(), head, null)
|
||||
.autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
|
||||
|
Reference in New Issue
Block a user