优化树形结构数据导出
This commit is contained in:
@@ -16,6 +16,12 @@ public class ExportExcelFormat {
|
||||
@NotNull(message = "excel表头不能为空")
|
||||
private String title;
|
||||
|
||||
@Schema(description = "表头对应的数据属性类型")
|
||||
private String locateType;
|
||||
|
||||
@Schema(description = "表头对应的数据属性类型数据值")
|
||||
private String locateValue;
|
||||
|
||||
@Schema(description = "属性值对应的字典编码")
|
||||
private String dictCode;
|
||||
|
||||
@@ -31,6 +37,8 @@ public class ExportExcelFormat {
|
||||
ExportExcelFormat format = new ExportExcelFormat();
|
||||
format.key = key;
|
||||
format.title = title;
|
||||
format.locateType = locateType;
|
||||
format.locateValue = locateValue;
|
||||
format.dictCode = dictCode;
|
||||
format.dictData = dictData;
|
||||
return format;
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.sdm.common.entity.ExportExcelFormat;
|
||||
import io.micrometer.common.util.StringUtils;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
@@ -242,8 +243,17 @@ public class ExcelUtil {
|
||||
* @param exportExcelFormats
|
||||
* @return
|
||||
*/
|
||||
private static List<HeadVO> getExcelHeader(List<ExportExcelFormat> exportExcelFormats)
|
||||
private static List<HeadVO> getExcelHeader(List<ExportExcelFormat> exportExcelFormats,List<ExportExcelFormat> filterFormats)
|
||||
{
|
||||
Map<String,ExportExcelFormat> showFormatMap = new HashMap<>();
|
||||
if(filterFormats!=null && !filterFormats.isEmpty()) {
|
||||
for (ExportExcelFormat exportExcelFormat : filterFormats) {
|
||||
String key = exportExcelFormat.getKey();
|
||||
if (showFormatMap.get(key) == null) {
|
||||
showFormatMap.put(key, exportExcelFormat);
|
||||
}
|
||||
}
|
||||
}
|
||||
List<HeadVO> excelHeader = new ArrayList<>();
|
||||
for(ExportExcelFormat exportExcelFormat : exportExcelFormats)
|
||||
{
|
||||
@@ -253,6 +263,10 @@ public class ExcelUtil {
|
||||
excelHeaderInfo.setTitle(exportExcelFormat.getTitle());
|
||||
excelHeaderInfo.setKeyType(exportExcelFormat.getKey());
|
||||
headVO.setExcelHeaderInfo(excelHeaderInfo);
|
||||
if(!showFormatMap.isEmpty() && !showFormatMap.containsKey(exportExcelFormat.getKey()))
|
||||
{
|
||||
excelHeaderInfo.setBShow(false);
|
||||
}
|
||||
excelHeader.add(headVO);
|
||||
}
|
||||
return excelHeader;
|
||||
@@ -268,7 +282,7 @@ public class ExcelUtil {
|
||||
ExcelSheet excelSheet = new ExcelSheet();
|
||||
excelSheet.setSheetName("export sheet1");
|
||||
//获取excel表头
|
||||
List<HeadVO> excelHeader = getExcelHeader(exportExcelFormats);
|
||||
List<HeadVO> excelHeader = getExcelHeader(exportExcelFormats,null);
|
||||
excelSheet.setHeads(excelHeader);
|
||||
|
||||
//获取excel表行数据
|
||||
@@ -363,7 +377,6 @@ public class ExcelUtil {
|
||||
{
|
||||
parent.addchildren(paraseData);
|
||||
}
|
||||
//paraseData.increaseLine();
|
||||
}
|
||||
String value = jsonObject.getString(key);
|
||||
JSONObject dictObj = format.getDictData();
|
||||
@@ -487,17 +500,56 @@ public class ExcelUtil {
|
||||
paraseData.lastRow = rowIndex+paraseData.lines-1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 表头第一个属性有效性检查
|
||||
* @param topObject
|
||||
* @param exportExcelFormats
|
||||
*/
|
||||
private static int filterExcelHeader(JSONObject topObject,List<ExportExcelFormat> exportExcelFormats)
|
||||
{
|
||||
Iterator<ExportExcelFormat> iterator = exportExcelFormats.iterator();
|
||||
int filterCount = 0;
|
||||
while (iterator.hasNext()) {
|
||||
ExportExcelFormat format = iterator.next();
|
||||
String locateType = format.getLocateType();
|
||||
String locateValue = format.getLocateValue();
|
||||
if(locateType!=null && !locateType.isEmpty())
|
||||
{
|
||||
if(!topObject.containsKey(locateType))
|
||||
{
|
||||
iterator.remove();
|
||||
filterCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!topObject.get(locateType).equals(locateValue)) {
|
||||
iterator.remove();
|
||||
filterCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return filterCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出有合并单元格excel
|
||||
* @param dataArray
|
||||
* @param exportExcelFormats
|
||||
* @param response
|
||||
*/
|
||||
public static void exportExcelWithMerge(JSONArray dataArray,List<ExportExcelFormat> exportExcelFormats,HttpServletResponse response)
|
||||
public static void exportExcelWithMerge(JSONArray dataArray,List<ExportExcelFormat> exportExcelFormats,List<ExportExcelFormat> filterFormat,HttpServletResponse response)
|
||||
{
|
||||
ExcelSheet excelSheet = new ExcelSheet();
|
||||
excelSheet.setSheetName("export sheet1");
|
||||
List<HeadVO> excelHeader = getExcelHeader(exportExcelFormats);
|
||||
List<HeadVO> excelHeader = getExcelHeader(exportExcelFormats,filterFormat);
|
||||
excelSheet.setHeads(excelHeader);
|
||||
Map<Integer,List<ExcelCellValue>> paraseDataMap = new HashMap<>();
|
||||
int rowNum = 1;
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
|
||||
Reference in New Issue
Block a user