增加excel导出模块

This commit is contained in:
wangshuai
2021-06-03 11:37:31 +08:00
parent 9fe34a737a
commit 920c5ef9bb
2 changed files with 124 additions and 0 deletions

View File

@@ -67,5 +67,10 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
</dependency>
<dependency>
<groupId>org.jeecgframework</groupId>
<artifactId>autopoi-web</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,119 @@
package org.ssssssss.magicapi.modules;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.config.MagicModule;
import org.ssssssss.script.annotation.Comment;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 对象集合转Excel字节数组
*
* @author 冰点
* @date 2021-6-2 16:42:16
*/
@Component
public class ExportModule implements MagicModule {
private static final Logger log = LoggerFactory.getLogger(ExportModule.class);
/**
* 将对象转换为excel文件
*
* @param columnHeaders
* @param exportObjList
* @param title
* @param sheetName
* @return
* @throws IOException
*/
@Comment("对象转换为Excel文件")
public static byte[] buildExcelByMap(@Comment("表格列头定义") Map<String, String> columnHeaders, @Comment("导出对象集合") List<Object> exportObjList, @Comment("表格title") String title, @Comment("sheet名称") String sheetName) throws IOException {
byte[] bytes;
Workbook workbook = null;
List<Map<String, String>> exportMapList = new ArrayList<>(exportObjList.size());
ObjectMapper mapper = new ObjectMapper();
exportObjList.forEach(item -> {
String itemStr = "";
try {
itemStr = mapper.writeValueAsString(item);
Map map = mapper.readValue(itemStr, Map.class);
exportMapList.add(map);
} catch (IOException e) {
log.error("对象转换为Excel文件,此条数据转换失败itemStr:[{}]", itemStr, e);
}
});
try {
List<ExcelExportEntity> colEntity = new ArrayList<>();
columnHeaders.forEach((key, value) -> colEntity.add(new ExcelExportEntity(value, key)));
ExportParams param = new ExportParams(title, sheetName);
workbook = ExcelExportUtil.exportExcel(param, colEntity, exportMapList);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
bytes = bos.toByteArray();
} catch (IOException e) {
throw new IOException("转换Excel文件异常", e);
} finally {
if (workbook != null) {
workbook.close();
}
}
return bytes;
}
/**
* 将对象集合转成excel并下载
* @param columnHeaders 列头
* @param exportObjList 导出对象
* @param title 文件名
* @param sheetName sheet名
* @return
* @throws IOException
*/
@Comment("Excel文件导出")
public static ResponseEntity<?> excel(@Comment("表格列头定义") Map<String, String> columnHeaders, @Comment("导出对象集合") List<Object> exportObjList, @Comment("表格title") String title, @Comment("sheet名称") String sheetName) throws IOException {
Object value = buildExcelByMap(columnHeaders, exportObjList, title, sheetName);
return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode(title, "UTF-8") + ".xls")
.body(value);
}
/**
* 将对象集合转成excel并下载
* @param columnHeaders 列头
* @param exportObjList 导出对象
* @param title 文件名
* @return
* @throws IOException
*/
@Comment("Excel文件导出")
public static ResponseEntity<?> excel(@Comment("表格列头定义") Map<String, String> columnHeaders, @Comment("导出对象集合") List<Object> exportObjList, @Comment("表格title") String title) throws IOException {
Object value = buildExcelByMap(columnHeaders, exportObjList, title, "");
return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode(title, "UTF-8") + ".xls")
.body(value);
}
/**
* 获取模块名
*/
@Override
public String getModuleName() {
return "export";
}
}