优化二维表格导出,故障修复
This commit is contained in:
@@ -21,4 +21,18 @@ public class ExportExcelFormat {
|
||||
|
||||
@Schema(description = "字典值")
|
||||
private JSONObject dictData;
|
||||
|
||||
@Schema(description = "标记该属性是否已处理")
|
||||
private boolean bUsed = false;
|
||||
|
||||
|
||||
public ExportExcelFormat deepCopy() {
|
||||
|
||||
ExportExcelFormat format = new ExportExcelFormat();
|
||||
format.key = key;
|
||||
format.title = title;
|
||||
format.dictCode = dictCode;
|
||||
format.dictData = dictData;
|
||||
return format;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.sdm.common.entity.ExportExcelFormat;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.poi.ss.formula.functions.T;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
||||
@@ -290,6 +291,8 @@ public class ExcelUtil {
|
||||
{
|
||||
List<String> keyValues = new ArrayList<>();
|
||||
int lines;
|
||||
int firstRow;
|
||||
int lastRow;
|
||||
ParaseData parent;
|
||||
List<ParaseData> children = new ArrayList<>();
|
||||
public void increaseLine()
|
||||
@@ -308,7 +311,21 @@ public class ExcelUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* list 深度拷贝
|
||||
* @param orgList
|
||||
* @return
|
||||
*/
|
||||
private static List<ExportExcelFormat> listDeepCopy(List<ExportExcelFormat> orgList)
|
||||
{
|
||||
List<ExportExcelFormat> list = new ArrayList<>();
|
||||
|
||||
for (ExportExcelFormat item : orgList) {
|
||||
list.add(item.deepCopy());
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
/**
|
||||
* 解析excel字段数据
|
||||
* @param jsonObject
|
||||
@@ -318,8 +335,8 @@ public class ExcelUtil {
|
||||
*/
|
||||
private static ParaseData paraseJsonObject(JSONObject jsonObject,List<ExportExcelFormat> exportExcelFormats,ParaseData parent)
|
||||
{
|
||||
List<ExportExcelFormat> excelFormats = new ArrayList<>(exportExcelFormats);
|
||||
Iterator<ExportExcelFormat> iterator = excelFormats.iterator();
|
||||
List<ExportExcelFormat> formats = listDeepCopy(exportExcelFormats);
|
||||
Iterator<ExportExcelFormat> iterator = formats.iterator();
|
||||
boolean bMatch = false;
|
||||
ParaseData paraseData = null;
|
||||
while (iterator.hasNext()) {
|
||||
@@ -335,16 +352,17 @@ public class ExcelUtil {
|
||||
{
|
||||
parent.addchildren(paraseData);
|
||||
}
|
||||
paraseData.increaseLine();
|
||||
//paraseData.increaseLine();
|
||||
}
|
||||
String value = jsonObject.getString(key);
|
||||
JSONObject dictObj = format.getDictData();
|
||||
if(ObjectUtils.isNotEmpty(dictObj))
|
||||
if(dictObj != null)
|
||||
{
|
||||
value = dictObj.getString(value);
|
||||
}
|
||||
paraseData.keyValues.add(value);
|
||||
iterator.remove();
|
||||
jsonObject.remove(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -357,9 +375,10 @@ public class ExcelUtil {
|
||||
ParaseData parentData = paraseData;
|
||||
if(parentData == null)
|
||||
parentData = parent;
|
||||
paraseJsonObject(child,exportExcelFormats,parentData);
|
||||
paraseJsonObject(child,formats,parentData);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return paraseData;
|
||||
@@ -391,8 +410,8 @@ public class ExcelUtil {
|
||||
ExcelCellValue cellValue = new ExcelCellValue();
|
||||
cellValue.setValue(key);
|
||||
cellValue.setMerge(true);
|
||||
cellValue.setFirstRow(beginRow+rowIndex);
|
||||
cellValue.setLastRow(beginRow+paraseData.lines-1);
|
||||
cellValue.setFirstRow(paraseData.firstRow);
|
||||
cellValue.setLastRow(paraseData.lastRow);
|
||||
cellValues.add(cellValue);
|
||||
}
|
||||
}
|
||||
@@ -404,7 +423,7 @@ public class ExcelUtil {
|
||||
cellValues.add(cellValue);
|
||||
}
|
||||
}
|
||||
int rowNum = 0;
|
||||
int rowNum = beginRow;
|
||||
for(ParaseData paseData : paraseData.children)
|
||||
{
|
||||
combineParaseData(paseData,paraseDataMap,beginColumn+paraseData.keyValues.size(),rowNum);
|
||||
@@ -419,6 +438,32 @@ public class ExcelUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 整理数据中的行数
|
||||
* @param paraseData
|
||||
*/
|
||||
private static void computeParaseData(ParaseData paraseData,int rowIndex)
|
||||
{
|
||||
if(paraseData.children.isEmpty())
|
||||
{
|
||||
paraseData.lines = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int beginRow = rowIndex;
|
||||
for(ParaseData child : paraseData.children)
|
||||
{
|
||||
computeParaseData(child,beginRow);
|
||||
paraseData.lines += child.lines;
|
||||
child.firstRow = beginRow;
|
||||
child.lastRow = beginRow+child.lines-1;
|
||||
beginRow += child.lines;
|
||||
}
|
||||
}
|
||||
paraseData.firstRow = rowIndex;
|
||||
paraseData.lastRow = rowIndex+paraseData.lines-1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出有合并单元格excel
|
||||
* @param dataArray
|
||||
@@ -432,20 +477,17 @@ public class ExcelUtil {
|
||||
List<HeadVO> excelHeader = getExcelHeader(exportExcelFormats);
|
||||
excelSheet.setHeads(excelHeader);
|
||||
Map<Integer,List<ExcelCellValue>> paraseDataMap = new HashMap<>();
|
||||
int rowNum = 0;
|
||||
int rowNum = 1;
|
||||
for(int index=0;index<dataArray.size();index++)
|
||||
{
|
||||
JSONObject jsonObject = dataArray.getJSONObject(index);
|
||||
ParaseData data = paraseJsonObject(jsonObject,exportExcelFormats,null);
|
||||
if(data == null)
|
||||
continue;
|
||||
computeParaseData(data,rowNum);
|
||||
combineParaseData(data,paraseDataMap,0,rowNum);
|
||||
if(data.lines>1)
|
||||
{
|
||||
rowNum += data.lines;
|
||||
}
|
||||
else
|
||||
{
|
||||
rowNum++;
|
||||
}
|
||||
rowNum += data.lines;
|
||||
|
||||
}
|
||||
int columnSize = paraseDataMap.size();
|
||||
int rowIndex = 0;
|
||||
|
||||
Reference in New Issue
Block a user