excel导出支持指定单元格合并

This commit is contained in:
马睿 2020-08-07 17:54:15 +08:00
parent dd9b2cd617
commit ba2494ce7f
2 changed files with 228 additions and 190 deletions

View File

@ -1,46 +1,5 @@
package com.ruoyi.common.utils.poi;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.annotation.Excel.ColumnType;
import com.ruoyi.common.annotation.Excel.Type;
@ -53,6 +12,22 @@ import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.reflect.ReflectUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* Excel相关处理
@ -103,6 +78,11 @@ public class ExcelUtil<T>
*/
private List<Object[]> fields;
/**
* 要合并的单元格列
*/
public ArrayList<Integer> mergeCells = new ArrayList<>();
/**
* 实体对象
*/
@ -411,6 +391,13 @@ public class ExcelUtil<T>
this.addCell(excel, row, vo, field, column++);
}
}
//合并单元格(需要传入要合并的列序号)
if(mergeCells.size() > 0){
for (int mergeCell : mergeCells) {
mergeCells(sheet, mergeCell, 1, sheet.getLastRowNum());
}
}
}
/**
@ -847,6 +834,18 @@ public class ExcelUtil<T>
}
}
this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
//筛选出需要合并的单元格的位置
if(this.fields.size()>0){
int column = 0;
for (Object[] os : fields){
Excel excel = (Excel) os[1];
if(excel.isMerge()){
this.mergeCells.add(column);
}
column++;
}
}
}
/**
@ -948,4 +947,43 @@ public class ExcelUtil<T>
}
return val;
}
/**
* @Author mr
* @Description //TODO 合并单元格
* @param sheet
* @param cellLine 合并的列号
* @param startRow 开始行
* @param endRow 尾行
* @Date 2020-06-29
**/
private void mergeCells(Sheet sheet, int cellLine,int startRow, int endRow) {
// 获取第一行的数据,以便后面进行比较
String s_will = getCellValue(sheet.getRow(startRow),cellLine).toString();
//合并行数
int count = 0;
//数据比较从第二行开始比较
for (int i = 2; i <= endRow; i++) {
//取出下一行的数据
String s_current = getCellValue(sheet.getRow(i),cellLine).toString();
//与相对的第一行的数据进行比较
if (s_will.equals(s_current) && !s_will.equals("")) {
//合并行数加一
count++;
} else {
//如果合并行数大于等于1就合并现将需要合并的行数进行合并
if(count >= 1){
//合并从开始行到合并的行数
sheet.addMergedRegion(new CellRangeAddress( startRow, startRow+count,cellLine , cellLine));
}
//初始化数据进行下一个单元格合并
startRow = i;
s_will = s_current;
count = 0;
}
//最后个单元格合并
if (i == endRow && count > 0) {
sheet.addMergedRegion(new CellRangeAddress(startRow,startRow+count ,cellLine , cellLine));
}
}
}
}