Commit 63e3bb66 by guojuxing

导出Excel二级表头添加

parent 071ff681
package com.gic.download.qo; package com.gic.download.qo;
import java.io.Serializable; import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 下载导出文件参数列表类 * 下载导出文件参数列表类
...@@ -50,6 +52,21 @@ public class DownloadExcelQO implements Serializable{ ...@@ -50,6 +52,21 @@ public class DownloadExcelQO implements Serializable{
*/ */
private List<Integer> columnWidth = null; private List<Integer> columnWidth = null;
/**
* 二级表头标题中文,父子关联
*/
private LinkedHashMap<String, List<String>> headerMap;
/**
* 二级表头属性名称
*/
private LinkedHashMap<String, List<String>> propertyNameMap;
/**
* 二级表头需要加密的属性名称
*/
private Map<String, List<String>> needEncryptFieldMap;
public String getTempPath() { public String getTempPath() {
return tempPath; return tempPath;
} }
...@@ -130,4 +147,31 @@ public class DownloadExcelQO implements Serializable{ ...@@ -130,4 +147,31 @@ public class DownloadExcelQO implements Serializable{
this.columnWidth = columnWidth; this.columnWidth = columnWidth;
return this; return this;
} }
public LinkedHashMap<String, List<String>> getHeaderMap() {
return headerMap;
}
public DownloadExcelQO setHeaderMap(LinkedHashMap<String, List<String>> headerMap) {
this.headerMap = headerMap;
return this;
}
public LinkedHashMap<String, List<String>> getPropertyNameMap() {
return propertyNameMap;
}
public DownloadExcelQO setPropertyNameMap(LinkedHashMap<String, List<String>> propertyNameMap) {
this.propertyNameMap = propertyNameMap;
return this;
}
public Map<String, List<String>> getNeedEncryptFieldMap() {
return needEncryptFieldMap;
}
public DownloadExcelQO setNeedEncryptFieldMap(Map<String, List<String>> needEncryptFieldMap) {
this.needEncryptFieldMap = needEncryptFieldMap;
return this;
}
} }
...@@ -144,6 +144,7 @@ public class ExcelUtils { ...@@ -144,6 +144,7 @@ public class ExcelUtils {
return true; return true;
} }
@Deprecated
public static boolean setHeaderTitle(CellStyle cellStyle, Sheet sheet, List<Integer> columnWidth, List<HeaderQO> headerList, List<?> content) { public static boolean setHeaderTitle(CellStyle cellStyle, Sheet sheet, List<Integer> columnWidth, List<HeaderQO> headerList, List<?> content) {
if (sheet == null) { if (sheet == null) {
logger.info("sheet is null"); logger.info("sheet is null");
...@@ -193,6 +194,57 @@ public class ExcelUtils { ...@@ -193,6 +194,57 @@ public class ExcelUtils {
return true; return true;
} }
public static boolean doubleHeaderTitle(CellStyle cellStyle, Sheet sheet, List<Integer> columnWidth, LinkedHashMap<String, List<String>> headerMap, List<?> content) {
if (sheet == null) {
logger.info("sheet is null");
return false;
}
// 设置sheet格式
setSheetStyle(sheet, columnWidth);
Row row1 = sheet.createRow(0);
Row row2 = sheet.createRow(1);
int n = 0;
for (Map.Entry<String, List<String>> entry : headerMap.entrySet()) {
Cell cell1 = row1.createCell(n);
List<String> sonHeader = entry.getValue();
String value = entry.getKey();
cell1.setCellValue(value);
cell1.setCellStyle(cellStyle);
//2层标题
if (CollectionUtils.isEmpty(sonHeader)) {
//单标题
Cell cell2 = row2.createCell(n);
cell2.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 1, n, n));
n++;
continue;
}
//创建第一行大标题
sheet.addMergedRegion(new CellRangeAddress(0, 0, n, (n + sonHeader.size() - 1)));
//赋值
for (int j = 0, sonLength = sonHeader.size(); j < sonLength; j ++) {
Cell cell2 = row2.createCell(n++);
cell2.setCellStyle(cellStyle);
cell2.setCellValue(sonHeader.get(j));
}
}
// 如果内容为空 则退出
if (content == null || content.isEmpty()) {
logger.info("content is null,cannot write excel");
return true;
}
int indexRow = 2;
for (Object rowContent : content) {
Row row = sheet.createRow(indexRow++);
setRowInfo(row, rowContent);
}
return true;
}
/** /**
* 導出到excel * 導出到excel
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment