1、合并单元格工具类
public class ExportExcelUtil {
public ExportExcelUtil() {
super();
}
public static void CombineExcel(String[] columnNames, String[] column, List<实体类型> rows, String excelName, int[] mergeIndex, HttpServletResponse response) {
SXSSFWorkbook workbook = new SXSSFWorkbook();
SXSSFSheet sheet = workbook.createSheet("sheet名字");
CellStyle titleStyle = workbook.createCellStyle();
Font titleFont = workbook.createFont();
titleFont.setFontHeightInPoints((short) 12);
titleFont.setFontName("宋体");
titleStyle.setFont(titleFont);
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setWrapText(true);
DataFormat format = workbook.createDataFormat();
headerStyle.setDataFormat(format.getFormat("@"));
Font headerFont = workbook.createFont();
headerFont.setFontHeightInPoints((short) 12);
headerFont.setFontName("宋体");
headerFont.setBold(true);
headerStyle.setFont(headerFont);
CellStyle dataStyle = workbook.createCellStyle();
Font dataFont = workbook.createFont();
dataFont.setFontHeightInPoints((short) 9);
dataFont.setFontName("宋体");
dataStyle.setFont(dataFont);
Row row = null;
Cell cell = null;
row = sheet.createRow(0);
for (int i = 0; i < columnNames.length; i++) {
row.setHeight((short) (2 * 256));
cell = row.createCell(i);
cell.setCellValue(columnNames[i]);
cell.setCellStyle(headerStyle);
}
List<PoiModel> poiModels = Lists.newArrayList();
for (int i = 0; i < rows.size(); i++) {
Row dataRow = sheet.createRow(i + 1);
sheet.trackAllColumnsForAutoSizing();
实体类型 project = rows.get(i);
int index = i + 1;
for (int j = 0; j < column.length; j++) {
for (int mer : mergeIndex) {
if (index == 1) {
String content=null;
String oldContent=null;
if(mer == 0){
oldContent = project.具体的实体类型1() == null ? "" : project.具体的实体类型1()();
content = project.具体的实体类型1() == null ? "" : project.具体的实体类型1()();
}else if(mer == 1){
oldContent = project.具体的实体类型2() == null ? "" : project.具体的实体类型2();
content = project.具体的实体类型2() == null ? "" : project.具体的实体类型2();
}
PoiModel poiModel = PoiModel.builder().oldContent(oldContent).content(content).rowIndex(1).cellIndex(j).build();
poiModels.add(poiModel);
break;
}
if (poiModels != null && poiModels.size() > 0) {
PoiModel poiModel = poiModels.get(j);
String content=null;
if(mer == 0){
content = project.具体的实体类型1() == null ? "" : project.具体的实体类型1();
}else if(mer == 1){
content = project.具体的实体类型2() == null ? "" : project.具体的实体类型2();
}
if (j > 0 && mer == j) {
if (!poiModel.getContent().equals(content)) {
get(poiModel, content, index, j, sheet);
}
}
if (mer == j && j == 0 && !poiModel.getContent().equals(content)) {
get(poiModel, content, index, j, sheet);
}
if (mer == j && index == rows.size() && poiModels.get(j).getRowIndex() != index) {
CellRangeAddress cra = new CellRangeAddress(poiModels.get(j).getRowIndex(), index, poiModels.get(j).getCellIndex(), poiModels.get(j).getCellIndex());
sheet.addMergedRegion(cra);
}
}
}
Cell dataCell = dataRow.createCell(j);
sheet.autoSizeColumn(j, true);
if(column[j] == "实体1"){
dataCell.setCellValue(project.具体的实体类型1());
}else if(column[j] == "实体2"){
dataCell.setCellValue(project.具体的实体类型2());
}else if(column[j] == "实体3"){
dataCell.setCellValue(project.具体的实体类型3());
}else if(column[j] == "实体4"){
dataCell.setCellValue(project.具体的实体类型4());
}else if(column[j] == "实体5"){
dataCell.setCellValue(project.具体的实体类型5());
}
dataCell.setCellStyle(dataStyle);
}
}
OutputStream os = null;
try {
os = response.getOutputStream();
response.reset();
response.setCharacterEncoding("UTF-8");
excelName = new String(excelName.getBytes(), "ISO8859-1");
response.setHeader("Content-Disposition", "attachment; filename=" + excelName);
response.setContentType("application/octet-stream");
workbook.write(os);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
workbook.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void get(PoiModel poiModel, String content, int index, int i, Sheet sheet) {
if (poiModel.getRowIndex() != index - 1) {
CellRangeAddress cra = new CellRangeAddress(poiModel.getRowIndex(), index - 1, poiModel.getCellIndex(), poiModel.getCellIndex());
sheet.addMergedRegion(cra);
}
poiModel.setContent(content);
poiModel.setRowIndex(index);
poiModel.setCellIndex(i);
}
}
2、工具类引用的import:
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
3、合并单元格具体的方法调用:
@GetMapping("/接口名")
public void exportManageExcel1(HttpServletResponse response,String 参数1) throws Exception {
try {
List<实体> list = XXXService.exportExcel(参数1);
String[] headers = {"名称1", "名称2", "名称3","名称4", "名称5"};
String[] column = {"实体1", "实体2", "实体3", "实体4", "实体5"};
String excelName = "表名.xlsx";
ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
int[] mergeIndex = {0,1};
exportExcelUtil.CombineExcel(headers, column, list, excelName, mergeIndex, response);
} catch (Exception e) {
e.printStackTrace();
}
}