mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-25 06:57:15 +08:00
新增:新增系统管理/部门管理/导出功能(引入 Easy Excel 依赖用于导出 Excel,详情可见 README 介绍。另请注意:测试导出功能时,前端需要关闭 mockjs,否则 responseType 会被 mockjs 设置为 '',导致导出的文件无法打开)
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.util;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
||||
import com.esotericsoftware.minlog.Log;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.config.easyexcel.ExcelBigNumberConverter;
|
||||
import top.charles7c.cnadmin.common.exception.ServiceException;
|
||||
|
||||
/**
|
||||
* Excel 工具类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/2/5 18:00
|
||||
*/
|
||||
@Slf4j
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class ExcelUtils {
|
||||
|
||||
/**
|
||||
* 导出
|
||||
*
|
||||
* @param list
|
||||
* 导出数据集合
|
||||
* @param fileName
|
||||
* 文件名
|
||||
* @param clazz
|
||||
* 导出数据类型
|
||||
* @param response
|
||||
* 响应对象
|
||||
*/
|
||||
public static <V> void export(List<V> list, String fileName, Class<V> clazz, HttpServletResponse response) {
|
||||
export(list, fileName, "Sheet1", clazz, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出
|
||||
*
|
||||
* @param list
|
||||
* 导出数据集合
|
||||
* @param fileName
|
||||
* 文件名
|
||||
* @param sheetName
|
||||
* 工作表名称
|
||||
* @param clazz
|
||||
* 导出数据类型
|
||||
* @param response
|
||||
* 响应对象
|
||||
*/
|
||||
public static <V> void export(List<V> list, String fileName, String sheetName, Class<V> clazz,
|
||||
HttpServletResponse response) {
|
||||
try {
|
||||
fileName = String.format("%s_%s.xlsx", fileName, DateUtil.format(new Date(), "yyyyMMddHHmmss"));
|
||||
fileName = URLUtil.encode(fileName);
|
||||
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
|
||||
EasyExcel.write(response.getOutputStream(), clazz).autoCloseStream(false)
|
||||
// 自动适配宽度
|
||||
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
||||
// 自动转换大数值
|
||||
.registerConverter(new ExcelBigNumberConverter()).sheet(sheetName).doWrite(list);
|
||||
} catch (Exception e) {
|
||||
Log.error("Export excel occurred an error.", e);
|
||||
throw new ServiceException("导出 Excel 出现错误");
|
||||
}
|
||||
}
|
||||
}
|
@@ -91,6 +91,17 @@ public class ExceptionUtils {
|
||||
return exToDefault(supplier, null, exConsumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果有异常,返回空字符串
|
||||
*
|
||||
* @param exSupplier
|
||||
* 可能会出现异常的方法执行
|
||||
* @return /
|
||||
*/
|
||||
public static String exToBlank(ExSupplier<String> exSupplier) {
|
||||
return exToDefault(exSupplier, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果有异常,返回默认值
|
||||
*
|
||||
|
Reference in New Issue
Block a user