|
|
@@ -2,6 +2,7 @@ package cn.tr.plugin.excel.config;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
+import cn.hutool.core.lang.Pair;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.ReflectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
@@ -86,7 +87,7 @@ public class ExcelHelper<T> {
|
|
|
/**
|
|
|
* Excel sheet最大行数,默认65536
|
|
|
*/
|
|
|
- private static final int sheetSize = 200000;
|
|
|
+ private static final int sheetSize = 10000;
|
|
|
|
|
|
private int sheetNo;
|
|
|
/**
|
|
|
@@ -471,17 +472,17 @@ public class ExcelHelper<T> {
|
|
|
*/
|
|
|
public void exportExcel(OutputStream outputStream ,String fileName,List<T> list, String sheetName, String title) throws IOException {
|
|
|
this.init(list, sheetName, title, Type.EXPORT);
|
|
|
- threadPoolExecutor.submit(()->{
|
|
|
- sheets.parallelStream().forEach(sheet -> { exportExcel(sheet,sheetDataMap.get(sheet)); });
|
|
|
- try {
|
|
|
- wb.write(outputStream);
|
|
|
- //表格写入成功回调 将相应的下载任务置为成功 todo
|
|
|
- }catch (Exception e){
|
|
|
- //表格写入失败通知 将相应的下载任务置为失败并重新进入下载队列 todo
|
|
|
- }finally {
|
|
|
- IOUtils.closeQuietly(wb);
|
|
|
- }
|
|
|
- });
|
|
|
+// threadPoolExecutor.submit(()->{
|
|
|
+ sheets.parallelStream().forEach(sheet -> { exportExcel(sheet,sheetDataMap.get(sheet)); });
|
|
|
+ try {
|
|
|
+ wb.write(outputStream);
|
|
|
+ //表格写入成功回调 将相应的下载任务置为成功 todo
|
|
|
+ }catch (Exception e){
|
|
|
+ //表格写入失败通知 将相应的下载任务置为失败并重新进入下载队列 todo
|
|
|
+ }finally {
|
|
|
+ IOUtils.closeQuietly(wb);
|
|
|
+ }
|
|
|
+// });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -547,6 +548,8 @@ public class ExcelHelper<T> {
|
|
|
int subMergedLastRowNum = getSheetIntValue(sheet,subMergedLastRowNumMap);
|
|
|
int subMergedFirstRowNum = getSheetIntValue(sheet,subMergedFirstRowNumMap);
|
|
|
AtomicInteger rowNo = getSheetValue(sheet,rowNumMap);
|
|
|
+ //上一个单元格的值 {"row",{"col",{"value","startRow"}}}
|
|
|
+ Map<Integer,Map<Integer, Pair<String,Integer>>> cellValueMap=new HashMap<>();
|
|
|
for (int i = 0; i < CollectionUtil.size(dataList); i++)
|
|
|
{
|
|
|
if(i>1){
|
|
|
@@ -571,6 +574,10 @@ public class ExcelHelper<T> {
|
|
|
}
|
|
|
}
|
|
|
int column = 0;
|
|
|
+
|
|
|
+ //合并空格的开始列
|
|
|
+ int mergeBlankStartCol=-1;
|
|
|
+ Cell cell=null;
|
|
|
for (Object[] os : fields) {
|
|
|
Field field = (Field) os[0];
|
|
|
Excel excel = (Excel) os[1];
|
|
|
@@ -591,7 +598,7 @@ public class ExcelHelper<T> {
|
|
|
{
|
|
|
subField.setAccessible(true);
|
|
|
Excel attr = subField.getAnnotation(Excel.class);
|
|
|
- this.addCell(sheet,attr, row, (T) obj, subField, column + subIndex);
|
|
|
+ cell=this.addCell(sheet,attr, row, (T) obj, subField, column + subIndex);
|
|
|
}
|
|
|
subIndex++;
|
|
|
}
|
|
|
@@ -601,12 +608,77 @@ public class ExcelHelper<T> {
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- this.addCell(sheet,excel, row, vo, field, column++);
|
|
|
+ cell=this.addCell(sheet,excel, row, vo, field, column++);
|
|
|
+ }
|
|
|
+ String currentValue = cell.getStringCellValue();
|
|
|
+ currentValue=StrUtil.isNullOrUndefined(currentValue)?null:currentValue;
|
|
|
+ //合并空单元格
|
|
|
+ if(excel!=null){
|
|
|
+ //当列相同时,合并列
|
|
|
+ boolean mergeRowWhenSame = excel.mergeRowWhenSame();
|
|
|
+ int rowIndex = cell.getRowIndex();
|
|
|
+ int colIndex=cell.getColumnIndex();
|
|
|
+ int startRowIndex=rowIndex;
|
|
|
+ if(mergeRowWhenSame){
|
|
|
+ //合并单元格
|
|
|
+ //上一个单元格的内容
|
|
|
+ //首个数据不行进行合并单元格计算,仅存储单元格的值
|
|
|
+ if(i>0||i==CollectionUtil.size(dataList)-1){
|
|
|
+ Pair<String, Integer> lastCell = cellValueMap.get(rowIndex - 1).get(colIndex);
|
|
|
+ if(!ObjectUtil.equals(lastCell.getKey(),currentValue)){
|
|
|
+ CellRangeAddress region = new CellRangeAddress(lastCell.getValue(), startRowIndex-1, colIndex, colIndex);
|
|
|
+ sheet.addMergedRegion(region);
|
|
|
+ }else if(i==CollectionUtil.size(dataList)-1){
|
|
|
+ CellRangeAddress region = new CellRangeAddress(lastCell.getValue(), startRowIndex, colIndex, colIndex);
|
|
|
+ sheet.addMergedRegion(region);
|
|
|
+ }else {
|
|
|
+ //内容相同,置换开始行数
|
|
|
+ startRowIndex=lastCell.getValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ //合并列和合并行不能同时出现
|
|
|
+ boolean mergeBlank = excel.mergeBlank();
|
|
|
+ //在同一行合并空格列
|
|
|
+ if(colIndex>0){
|
|
|
+ Pair<String, Integer> valuePair = cellValueMap.get(rowIndex).get(colIndex - 1);
|
|
|
+ if((colIndex==CollectionUtil.size(fields)-1||!isEmptyValue(cell))
|
|
|
+ &&mergeBlankStartCol!=-1){
|
|
|
+
|
|
|
+ if(isEmptyValue(cell)){
|
|
|
+ CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex, mergeBlankStartCol, colIndex);
|
|
|
+ sheet.addMergedRegion(region);
|
|
|
+ mergeBlankStartCol=-1;
|
|
|
+ }
|
|
|
+ //当前单元格非空时,判断前一个单元格是否为空
|
|
|
+ //前一个单元格为空
|
|
|
+ else if(StrUtil.isEmpty(valuePair.getKey())){
|
|
|
+ CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex, mergeBlankStartCol, colIndex-1);
|
|
|
+ sheet.addMergedRegion(region);
|
|
|
+ mergeBlankStartCol=-1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(mergeBlank){
|
|
|
+ mergeBlankStartCol=colIndex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ cellValueMap.compute(rowIndex, (k, colMap) -> {
|
|
|
+ if (colMap == null) {
|
|
|
+ colMap = new HashMap<>();
|
|
|
+ }
|
|
|
+ return colMap;
|
|
|
+ }).put(colIndex, Pair.of(currentValue, startRowIndex));
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ private boolean isEmptyValue(Cell cell){
|
|
|
+ return StrUtil.isEmpty(cell.getStringCellValue())||StrUtil.isNullOrUndefined(cell.getStringCellValue());
|
|
|
+ }
|
|
|
/**
|
|
|
* 创建表格样式
|
|
|
*
|