一.创建实体类
- package com.siact.product.jwp.module.report.dto;
-
- import com.alibaba.excel.annotation.ExcelProperty;
- import com.alibaba.excel.annotation.write.style.ColumnWidth;
- import com.alibaba.excel.annotation.write.style.ContentRowHeight;
- import com.alibaba.excel.annotation.write.style.HeadRowHeight;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- /**
- * @Description:
- */
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- @ContentRowHeight(30)
- @HeadRowHeight(25)
- @ColumnWidth(25)
- public class PatrolProcessDTO {
-
- @ExcelProperty(index = 0,value = "序号")
- private int orderNum;
-
- @ExcelProperty(index = 1,value = "工单名称")
- private String workOrderName;
-
- @ExcelProperty(index = 2,value = "巡检路线")
- private String patrolRoute;
-
- @ExcelProperty(index = 3,value = "维修技师")
- private String maintenanceUserName;
-
- @ExcelProperty(index = 4,value = "打点情况")
- private String content;
-
- @ExcelProperty(index = 5,value = "处理结果(工单状态)")
- private String workOrderSolveStatusName;
-
- @ExcelProperty(index = 6,value = "维修技师处理时长")
- private String maintenanceTime;
-
- @ExcelProperty(index = 7,value = "维修班长确认时长")
- private String confirmTime;
-
- @ExcelProperty(index = 8,value = "总用时")
- private String workOrderSpendTime;
-
- }
二、引入合并策略
- package com.siact.product.jwp.module.report.service.impl.StyleUtils;
-
- import com.alibaba.excel.metadata.Head;
- import com.alibaba.excel.metadata.data.CellData;
- import com.alibaba.excel.metadata.data.WriteCellData;
- import com.alibaba.excel.write.handler.CellWriteHandler;
- import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
- import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellType;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.util.CellRangeAddress;
-
- import java.util.List;
-
- /**
- * @Description:
- */
-
- public class ExcelFillCellMergeStrategy implements CellWriteHandler {
- // /*
- // * 要合并的列 (下表也是从0开始)
- // */
- // private Set
mergeColumnIndex; - // /*
- // * 用第几行开始合并 ,默认为1,因为第0行是标题,EasyExcel 的默认也是
- // */
- // private int mergeBeginRowIndex = 1;
- //
- // public ExcelFillCellMergeStrategy(Set
mergeColumnIndex) { - // this.mergeColumnIndex = mergeColumnIndex;
- // }
- //
- //
- // /*
- // * 在创建单元格之前调用
- // */
- // @Override
- // public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
- // }
- //
- // /*
- // * 在创建单元格之后调用
- // */
- // @Override
- // public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
- //
- // }
- //
- //
- //
- // /*
- // * 在对单元格的所有操作完成后调用
- // */
- // @Override
- // public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List
> list, Cell cell, Head head, Integer integer, Boolean aBoolean) { - // //当前行
- // int curRowIndex = cell.getRowIndex();
- // //当前列
- // int curColIndex = cell.getColumnIndex();
- //
- // if (curRowIndex > mergeBeginRowIndex) {
- // if (mergeColumnIndex.contains(curColIndex)) {
- // mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
- // }
- // }
- // }
- //
- // /**
- // * 当前单元格向上合并
- // *
- // * @param writeSheetHolder
- // * @param cell 当前单元格
- // * @param curRowIndex 当前行
- // * @param curColIndex 当前列
- // */
- // private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
- // //获取当前行的当前列的数据和上一行的当前列列数据,通过上一行数据是否相同进行合并
- // Object curData = cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
- // Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
- // Object preData = preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();
- // // 比较当前行的第一列的单元格与上一行是否相同,相同合并当前单元格与上一行
- // if (curData.equals(preData)) {
- // Sheet sheet = writeSheetHolder.getSheet();
- // // 获取合并信息
- // List
mergeRegions = sheet.getMergedRegions(); - // int size = mergeRegions.size();
- // CellRangeAddress cellRangeAddr;
- // if (size > 0) {
- // cellRangeAddr = mergeRegions.get(size - 1);
- // // 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
- // if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
- // // 移除当前合并信息
- // sheet.removeMergedRegion(size - 1);
- // // 重新设置当前结束行
- // cellRangeAddr.setLastRow(curRowIndex);
- // } else {
- // // 若上一个单元格未被合并,则新增合并单元
- // cellRangeAddr = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
- // }
- // } else {
- // // 若上一个单元格未被合并,则新增合并单元
- // cellRangeAddr = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
- // }
- // // 添加新的合并信息
- // sheet.addMergedRegion(cellRangeAddr);
- // }
- // }
- // 需要从第几行开始合并,0表示第1行
- private final int mergeRowIndex;
- // 合并的哪些列,比如为4时,当前行id和上一行id相同则合并前五列
- private final int mergeColumnRegion;
-
- private final List
ignoreColumn; -
- public ExcelFillCellMergeStrategy(int mergeRowIndex, int mergeColumnRegion, List
ignoreColumn) { - this.mergeRowIndex = mergeRowIndex;
- this.mergeColumnRegion = mergeColumnRegion;
- this.ignoreColumn = ignoreColumn;
- }
-
- @Override
- public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
-
- }
-
- // @Override
- // public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
- // // 隐藏id列
- writeSheetHolder.getSheet().setColumnHidden(0, true);
- // }
-
- @Override
- public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List
> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { - //当前行
- int curRowIndex = cell.getRowIndex();
- //当前列
- int curColIndex = cell.getColumnIndex();
-
- if (!ignoreColumn.contains(curColIndex) && curRowIndex > mergeRowIndex) {
- for (int i = 0; i < mergeColumnRegion; i++) {
- if (curColIndex <= mergeColumnRegion) {
- mergeWithPreviousRow(writeSheetHolder, cell, curRowIndex, curColIndex);
- break;
- }
- }
- }
- }
-
- /**
- * 当前单元格向上合并:当前行的id和上一行的id相同则合并前面(mergeColumnRegion+1)列
- *
- * @param cell 当前单元格
- * @param curRowIndex 当前行
- * @param curColIndex 当前列
- */
- private void mergeWithPreviousRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
- // 当前行的第一个Cell
- Cell curFirstCell = cell.getSheet().getRow(curRowIndex).getCell(0);
- Object curFirstData = curFirstCell.getCellType().getCode() == CellType.STRING.getCode() ? curFirstCell.getStringCellValue() : curFirstCell.getNumericCellValue();
- // 上一行的第一个Cell
- Cell preFirstCell = cell.getSheet().getRow(curRowIndex - 1).getCell(0);
- Object preFirstData = preFirstCell.getCellType().getCode() == CellType.STRING.getCode() ? preFirstCell.getStringCellValue() : preFirstCell.getNumericCellValue();
-
- // 当前cell
- Object data = cell.getCellType().getCode() == CellType.STRING.getCode() ? cell.getStringCellValue() : cell.getNumericCellValue();
- // 上面的Cell
- Cell upCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
- Object upData = upCell.getCellType().getCode() == CellType.STRING.getCode() ? upCell.getStringCellValue() : upCell.getNumericCellValue();
-
- // 当前行的id和上一行的id相同则合并前面(mergeColumnRegion+1)列 且上一行值相同
- if (curFirstData.equals(preFirstData) && data.equals(upData)) {
- Sheet sheet = writeSheetHolder.getSheet();
- List
mergeRegions = sheet.getMergedRegions(); - boolean isMerged = false;
- for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
- CellRangeAddress cellRangeAddr = mergeRegions.get(i);
- // 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
- if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
- sheet.removeMergedRegion(i);
- cellRangeAddr.setLastRow(curRowIndex);
- sheet.addMergedRegion(cellRangeAddr);
- isMerged = true;
- }
- }
- // 若上一个单元格未被合并,则新增合并单元
- if (!isMerged) {
- CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
- sheet.addMergedRegion(cellRangeAddress);
- }
- }
- }
-
- @Override
- public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
-
- }
-
- }
-
三、引入其他样式
- package com.siact.product.jwp.module.report.service.impl.StyleUtils;
-
- import cn.hutool.core.util.StrUtil;
- import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
- import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
- import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
- import com.siact.product.jwp.module.report.service.impl.StyleUtils.CellStyleModel;
- import org.apache.poi.ss.usermodel.*;
- import org.apache.poi.xssf.usermodel.XSSFCellStyle;
- import org.apache.poi.xssf.usermodel.XSSFColor;
- import org.apache.poi.xssf.usermodel.XSSFFont;
- import org.camunda.bpm.engine.impl.util.CollectionUtil;
- import org.springframework.stereotype.Component;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
-
- /**
- * @Author: 刘 旺
- * @CreateTime: 2023-08-04 17:50
- * @Description: 自定义单元格样式处理器(支持字体样式、背景颜色、边框样式、对齐方式、自动换行)
- */
-
- @Component
- public class CustomCellStyleHandler extends AbstractRowWriteHandler {
- /**
- * sheet页名称列表
- */
- private List
sheetNameList; - /**
- * 样式信息
- */
- private List
cellStyleList = new ArrayList<>(); - /**
- * 自定义样式适配器构造方法
- *
- * @param cellStyleList 样式信息
- */
- public CustomCellStyleHandler(List
cellStyleList) { - if (CollectionUtil.isEmpty(cellStyleList)) {
- return;
- }
- cellStyleList = cellStyleList.stream().filter(x -> x != null
- //判断sheet名称KEY是否存在
- && StrUtil.isNotBlank(x.getSheetName())
- //字体样式
- //判断字体颜色KEY是否存在
- && (x.getFontColor() == null || x.getFontColor() instanceof IndexedColors
- || x.getFontColor() instanceof XSSFColor)
- //判断背景颜色KEY是否存在
- && (x.getBackgroundColor() == null || x.getBackgroundColor() instanceof IndexedColors
- || x.getBackgroundColor() instanceof XSSFColor)
- //边框样式
- // 判断上边框线条颜色KEY是否存在
- && (x.getTopBorderColor() == null || x.getTopBorderColor() instanceof IndexedColors
- || x.getTopBorderColor() instanceof XSSFColor)
- // 判断右边框线条颜色KEY是否存在
- && (x.getRightBorderColor() == null || x.getRightBorderColor() instanceof IndexedColors
- || x.getRightBorderColor() instanceof XSSFColor)
- // 判断下边框线条颜色KEY是否存在
- && (x.getBottomBorderColor() == null || x.getBottomBorderColor() instanceof IndexedColors
- || x.getBottomBorderColor() instanceof XSSFColor)
- // 判断左边框线条颜色KEY是否存在
- && (x.getLeftBorderColor() == null || x.getLeftBorderColor() instanceof IndexedColors
- || x.getLeftBorderColor() instanceof XSSFColor)
- ).collect(Collectors.toList());
- this.cellStyleList = cellStyleList;
- sheetNameList = this.cellStyleList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList());
- }
- @Override
- public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row
- , Integer relativeRowIndex, Boolean isHead) {
- Sheet sheet = writeSheetHolder.getSheet();
- //不需要添加样式,或者当前sheet页不需要添加样式
- if (cellStyleList == null || cellStyleList.size() <= 0 || sheetNameList.contains(sheet.getSheetName()) == false) {
- return;
- }
- //获取当前行的样式信息
- List
rowCellStyleList = cellStyleList.stream().filter(x -> - StrUtil.equals(x.getSheetName(), sheet.getSheetName()) && x.getRowIndex() == relativeRowIndex).collect(Collectors.toList());
- //该行不需要设置样式
- if (rowCellStyleList == null || rowCellStyleList.size() <= 0) {
- return;
- }
- for (CellStyleModel cellStyleModel : rowCellStyleList) {
- //设置单元格样式
- setCellStyle(cellStyleModel, row);
- }
- //删除已添加的样式信息
- cellStyleList.removeAll(rowCellStyleList);
- //重新获取要添加的sheet页姓名
- sheetNameList = cellStyleList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList());
- }
- /**
- * 给单元格设置样式
- *
- * @param cellStyleModel 样式信息
- * @param row 行对象
- */
- private void setCellStyle(CellStyleModel cellStyleModel, Row row) {
- //背景颜色
- Object backgroundColor = cellStyleModel.getBackgroundColor();
- //自动换行
- Boolean wrapText = cellStyleModel.getWrapText();
- //列索引
- int colIndex = cellStyleModel.getColIndex();
- //边框样式
- Cell cell = row.getCell(colIndex);
- if (cell == null) {
- cell = row.createCell(colIndex);
- }
- XSSFCellStyle style = (XSSFCellStyle) cell.getRow().getSheet().getWorkbook().createCellStyle();
- // 克隆出一个 style
- style.cloneStyleFrom(cell.getCellStyle());
- //设置背景颜色
- if (backgroundColor != null) {
- //使用IndexedColors定义的颜色
- if (backgroundColor instanceof IndexedColors) {
- style.setFillForegroundColor(((IndexedColors) backgroundColor).getIndex());
- }
- //使用自定义的RGB颜色
- else if (backgroundColor instanceof XSSFColor) {
- style.setFillForegroundColor((XSSFColor) backgroundColor);
- }
- style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
- }
- //设置自动换行
- if (wrapText != null) {
- style.setWrapText(wrapText);
- }
- //设置字体样式
- setFontStyle(row, style, cellStyleModel);
- //设置边框样式
- setBorderStyle(style, cellStyleModel);
- //设置对齐方式
- setAlignmentStyle(style, cellStyleModel);
- cell.setCellStyle(style);
- }
- /**
- * 设置字体样式
- *
- * @param row 行对象
- * @param style 单元格样式
- * @param cellStyleModel 样式信息
- */
- private void setFontStyle(Row row, XSSFCellStyle style, CellStyleModel cellStyleModel) {
- //字体名称
- String fontName = cellStyleModel.getFontName();
- //字体大小
- Double fontHeight = cellStyleModel.getFontHeight();
- //字体颜色
- Object fontColor = cellStyleModel.getFontColor();
- //字体加粗
- Boolean fontBold = cellStyleModel.getFontBold();
- //字体斜体
- Boolean fontItalic = cellStyleModel.getFontItalic();
- //字体下划线
- Byte fontUnderLine = cellStyleModel.getFontUnderLine();
- //字体上标下标
- Short fontTypeOffset = cellStyleModel.getFontTypeOffset();
- //字体删除线
- Boolean fontStrikeout = cellStyleModel.getFontStrikeout();
- //不需要设置字体样式
- if (fontName == null && fontHeight == null && fontColor == null && fontBold == null && fontItalic == null
- && fontUnderLine == null && fontTypeOffset == null && fontStrikeout == null) {
- return;
- }
- XSSFFont font = null;
- //样式存在字体对象时,使用原有的字体对象
- if (style.getFontIndex() != 0) {
- font = style.getFont();
- }
- //样式不存在字体对象时,创建字体对象
- else {
- font = (XSSFFont) row.getSheet().getWorkbook().createFont();
- //默认字体为宋体
- font.setFontName("宋体");
- }
- //设置字体名称
- if (fontName != null) {
- font.setFontName(fontName);
- }
- //设置字体大小
- if (fontHeight != null) {
- font.setFontHeight(fontHeight);
- }
- //设置字体颜色
- if (fontColor != null) {
- //使用IndexedColors定义的颜色
- if (fontColor instanceof IndexedColors) {
- font.setColor(((IndexedColors) fontColor).getIndex());
- }
- //使用自定义的RGB颜色
- else if (fontColor instanceof XSSFColor) {
- font.setColor((XSSFColor) fontColor);
- }
- }
- //设置字体加粗
- if (fontBold != null) {
- font.setBold(fontBold);
- }
- //设置字体斜体
- if (fontItalic != null) {
- font.setItalic(fontItalic);
- }
- //设置字体下划线
- if (fontUnderLine != null) {
- font.setUnderline(fontUnderLine);
- }
- //设置字体上标下标
- if (fontTypeOffset != null) {
- font.setTypeOffset(fontTypeOffset);
- }
- //设置字体删除线
- if (fontStrikeout != null) {
- font.setStrikeout(fontStrikeout);
- }
- style.setFont(font);
- }
- /**
- * 设置边框样式
- *
- * @param style 单元格样式
- * @param cellStyleModel 样式信息
- */
- private void setBorderStyle(XSSFCellStyle style, CellStyleModel cellStyleModel) {
- //上边框线条类型
- BorderStyle borderTop = cellStyleModel.getBorderTop();
- //右边框线条类型
- BorderStyle borderRight = cellStyleModel.getBorderRight();
- //下边框线条类型
- BorderStyle borderBottom = cellStyleModel.getBorderBottom();
- //左边框线条类型
- BorderStyle borderLeft = cellStyleModel.getBorderLeft();
- //上边框颜色类型
- Object topBorderColor = cellStyleModel.getTopBorderColor();
- //右边框颜色类型
- Object rightBorderColor = cellStyleModel.getRightBorderColor();
- //下边框颜色类型
- Object bottomBorderColor = cellStyleModel.getBottomBorderColor();
- //左边框颜色类型
- Object leftBorderColor = cellStyleModel.getLeftBorderColor();
- //不需要设置边框样式
- if (borderTop == null && borderRight == null && borderBottom == null && borderLeft == null && topBorderColor == null
- && rightBorderColor == null && bottomBorderColor == null && leftBorderColor == null) {
- return;
- }
- //设置上边框线条类型
- if (borderTop != null) {
- style.setBorderTop(borderTop);
- }
- //设置右边框线条类型
- if (borderRight != null) {
- style.setBorderRight(borderRight);
- }
- //设置下边框线条类型
- if (borderBottom != null) {
- style.setBorderBottom(borderBottom);
- }
- //设置左边框线条类型
- if (borderLeft != null) {
- style.setBorderLeft(borderLeft);
- }
- //设置上边框线条颜色
- if (topBorderColor != null) {
- //使用IndexedColors定义的颜色
- if (topBorderColor instanceof IndexedColors) {
- style.setTopBorderColor(((IndexedColors) topBorderColor).getIndex());
- }
- //使用自定义的RGB颜色
- else if (topBorderColor instanceof XSSFColor) {
- style.setTopBorderColor((XSSFColor) topBorderColor);
- }
- }
- //设置右边框线条颜色
- if (rightBorderColor != null) {
- //使用IndexedColors定义的颜色
- if (rightBorderColor instanceof IndexedColors) {
- style.setRightBorderColor(((IndexedColors) rightBorderColor).getIndex());
- }
- //使用自定义的RGB颜色
- else if (rightBorderColor instanceof XSSFColor) {
- style.setRightBorderColor((XSSFColor) rightBorderColor);
- }
- }
- //设置下边框线条颜色
- if (bottomBorderColor != null) {
- //使用IndexedColors定义的颜色
- if (bottomBorderColor instanceof IndexedColors) {
- style.setBottomBorderColor(((IndexedColors) bottomBorderColor).getIndex());
- }
- //使用自定义的RGB颜色
- else if (bottomBorderColor instanceof XSSFColor) {
- style.setBottomBorderColor((XSSFColor) bottomBorderColor);
- }
- }
- //设置左边框线条颜色
- if (leftBorderColor != null) {
- //使用IndexedColors定义的颜色
- if (leftBorderColor instanceof IndexedColors) {
- style.setLeftBorderColor(((IndexedColors) leftBorderColor).getIndex());
- }
- //使用自定义的RGB颜色
- else if (topBorderColor instanceof XSSFColor) {
- style.setLeftBorderColor((XSSFColor) leftBorderColor);
- }
- }
- }
- /**
- * 设置对齐方式
- *
- * @param style 单元格样式
- * @param cellStyleModel 样式信息
- */
- private void setAlignmentStyle(XSSFCellStyle style, CellStyleModel cellStyleModel) {
- //水平对齐方式
- HorizontalAlignment horizontalAlignment = cellStyleModel.getHorizontalAlignment();
- //垂直对齐方式
- VerticalAlignment verticalAlignment = cellStyleModel.getVerticalAlignment();
- //不需要设置对齐方式
- if (horizontalAlignment == null && verticalAlignment == null) {
- return;
- }
- //设置水平对齐方式
- if (horizontalAlignment != null) {
- style.setAlignment(horizontalAlignment);
- }
- //设置垂直对齐方式
- if (verticalAlignment != null) {
- style.setVerticalAlignment(verticalAlignment);
- }
- }
-
- }
四、如有换行及其他背景字体样式,需引入换行样式(换行识别标识‘\n’)
- package com.siact.product.jwp.module.report.service.impl.StyleUtils;
-
- import cn.hutool.core.util.StrUtil;
- import lombok.Data;
- import org.apache.poi.ss.usermodel.*;
- import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
- import org.apache.poi.xssf.usermodel.XSSFColor;
-
- /**
- * @Description:
- */
- @Data
- public class CellStyleModel {
- /**
- * sheet名称
- */
- private String sheetName;
- /**
- * 列索引
- */
- private int colIndex;
- /**
- * 行索引
- */
- private int rowIndex;
- /**
- * 字体名称
- */
- private String fontName;
- /**
- * 字体大小
- */
- private Double fontHeight;
- /**
- * 字体颜色
- */
- private Object fontColor;
- /**
- * 字体加粗
- */
- private Boolean fontBold;
- /**
- * 字体斜体
- */
- private Boolean fontItalic;
- /**
- * 字体下划线
- */
- private Byte fontUnderLine;
- /**
- * 字体上标下标
- */
- private Short fontTypeOffset;
- /**
- * 字体删除线
- */
- private Boolean fontStrikeout;
- /**
- * 背景颜色
- */
- private Object backgroundColor;
- /**
- * 上边框线条类型
- */
- private BorderStyle borderTop;
- /**
- * 右边框线条类型
- */
- private BorderStyle borderRight;
- /**
- * 下边框线条类型
- */
- private BorderStyle borderBottom;
- /**
- * 左边框线条类型
- */
- private BorderStyle borderLeft;
- /**
- * 上边框线条颜色
- */
- private Object topBorderColor;
- /**
- * 上边框线条颜色
- */
- private Object rightBorderColor;
- /**
- * 下边框线条颜色
- */
- private Object bottomBorderColor;
- /**
- */
- private Object leftBorderColor;
- /**
- * 水平对齐方式
- */
- private HorizontalAlignment horizontalAlignment;
- /**
- * 垂直对齐方式
- */
- private VerticalAlignment verticalAlignment;
- /**
- * 自动换行方式
- */
- private Boolean wrapText;
- /**
- * 生成字体名称样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontName 字体名称(默认宋体)
- * @return
- */
- public static CellStyleModel createFontNameCellStyleModel(String sheetName, int rowIndex, int columnIndex, String fontName) {
- return createFontCellStyleModel(sheetName, rowIndex, columnIndex, fontName, null, null, null, null, null, null, null);
- }
- /**
- * 生成字体名称大小信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontHeight 字体大小
- * @return
- */
- public static CellStyleModel createFontHeightCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , Double fontHeight) {
- return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, fontHeight, null, null, null, null, null, null);
- }
- /**
- * 得到RBG自定义颜色
- *
- * @param redNum 红色数值
- * @param greenNum 绿色数值
- * @param blueNum 蓝色数值
- * @return
- */
- public static XSSFColor getRGBColor(int redNum, int greenNum, int blueNum) {
- XSSFColor color = new XSSFColor(new byte[]{(byte) redNum, (byte) greenNum, (byte) blueNum}, new DefaultIndexedColorMap());
- return color;
- }
- /**
- * 生成字体颜色样式信息(支持自定义RGB颜色)
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param redNum 红色数值
- * @param greenNum 绿色数值
- * @param blueNum 蓝色数值
- * @return
- */
- public static CellStyleModel createFontColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , int redNum, int greenNum, int blueNum) {
- XSSFColor fontColor = getRGBColor(redNum, greenNum, blueNum);
- return createFontColorCellStyleModel(sheetName, rowIndex, columnIndex, fontColor);
- }
- /**
- * 生成字体颜色样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontColor 字体颜色
- * @return
- */
- public static CellStyleModel createFontColorCellStyleModel(String sheetName, int rowIndex, int columnIndex, Object fontColor) {
- return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, fontColor, null, null, null, null, null);
- }
- /**
- * 生成字体加粗样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontBold 字体加粗
- * @return
- */
- public static CellStyleModel createFontBoldCellStyleModel(String sheetName, int rowIndex, int columnIndex, Boolean fontBold) {
- return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, fontBold, null, null, null, null);
- }
- /**
- * 生成字体斜体样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontItalic 字体斜体
- * @return
- */
- public static CellStyleModel createFontItalicCellStyleModel(String sheetName, int rowIndex, int columnIndex, Boolean fontItalic) {
- return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, fontItalic, null, null, null);
- }
- /**
- * 生成字体下划线样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontUnderLine 字体下划线
- * @return
- */
- public static CellStyleModel createFontUnderLineCellStyleModel(String sheetName, int rowIndex, int columnIndex, Byte fontUnderLine) {
- return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null, fontUnderLine, null, null);
- }
- /**
- * 生成字体上标下标样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontTypeOffset 字体上标下标
- * @return
- */
- public static CellStyleModel createFontTypeOffsetCellStyleModel(String sheetName, int rowIndex, int columnIndex, Short fontTypeOffset) {
- return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null, null, fontTypeOffset, null);
- }
- /**
- * 生成字体删除线样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontStrikeout 字体删除线
- * @return
- */
- public static CellStyleModel createFontStrikeoutCellStyleModel(String sheetName, int rowIndex, int columnIndex, Boolean fontStrikeout) {
- return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null, null, null, fontStrikeout);
- }
- /**
- * 生成字体样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontName 字体名称(默认宋体)
- * @param fontHeight 字体大小
- * @param fontColor 字体颜色
- * @param fontItalic 字体斜体
- * @param fontBold 字体加粗
- * @param fontUnderLine 字体下划线
- * @param fontTypeOffset 字体上标下标
- * @param fontStrikeout 字体删除线
- * @return
- */
- public static CellStyleModel createFontCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , String fontName, Double fontHeight, Object fontColor, Boolean fontBold, Boolean fontItalic, Byte fontUnderLine
- , Short fontTypeOffset, Boolean fontStrikeout) {
- return createCellStyleModel(sheetName, rowIndex, columnIndex, fontName, fontHeight, fontColor, fontBold, fontItalic
- , fontUnderLine, fontTypeOffset, fontStrikeout, null);
- }
- /**
- * 生成背景颜色样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param backgroundColor 背景颜色
- * @return
- */
- public static CellStyleModel createBackgroundColorCellStyleModel(String sheetName, int rowIndex, int columnIndex, Object backgroundColor) {
- return createCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null, null, null, null, backgroundColor);
- }
- /**
- * 生成背景颜色样式信息(支持自定义RGB颜色)
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param redNum 红色数值
- * @param greenNum 绿色数值
- * @param blueNum 蓝色数值
- * @return
- */
- public static CellStyleModel createBackgroundColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , int redNum, int greenNum, int blueNum) {
- XSSFColor backgroundColor = getRGBColor(redNum, greenNum, blueNum);
- return createBackgroundColorCellStyleModel(sheetName, rowIndex, columnIndex, backgroundColor);
- }
- /**
- * 生成样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontName 字体名称(宋体)
- * @param fontHeight 字体大小
- * @param fontColor 字体颜色
- * @param fontBold 字体加粗
- * @param fontItalic 字体斜体
- * @param fontUnderLine 字体下划线
- * @param fontTypeOffset 字体上标下标
- * @param fontStrikeout 字体删除线
- * @param backgroundColor 背景颜色
- * @return
- */
- public static CellStyleModel createCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , String fontName, Double fontHeight, Object fontColor, Boolean fontBold, Boolean fontItalic, Byte fontUnderLine
- , Short fontTypeOffset, Boolean fontStrikeout, Object backgroundColor) {
- return createCellStyleModel(sheetName, rowIndex, columnIndex, fontName, fontHeight, fontColor, fontBold, fontItalic
- , fontUnderLine, fontTypeOffset, fontStrikeout, backgroundColor, null, null, null, null, null, null, null, null);
- }
- /**
- * 生成上边框线条颜色样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param topBorderColor 上边框线条颜色
- * @return
- */
- public static CellStyleModel createTopBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , Object topBorderColor) {
- return createBorderColorCellStyleModel(sheetName, rowIndex, columnIndex, topBorderColor, null, null, null);
- }
- /**
- * 生成右边框线条颜色样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param rightBorderColor 右边框线条颜色
- * @return
- */
- public static CellStyleModel createRightBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , Object rightBorderColor) {
- return createBorderColorCellStyleModel(sheetName, rowIndex, columnIndex, null, rightBorderColor, null, null);
- }
- /**
- * 生成下边框线条颜色样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param bottomBorderColor 下边框线条颜色
- * @return
- */
- public static CellStyleModel createBottomBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , Object bottomBorderColor) {
- return createBorderColorCellStyleModel(sheetName, rowIndex, columnIndex, null, null, bottomBorderColor, null);
- }
- /**
- * 生成左边框线条颜色样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param leftBorderColor 左边框线条颜色
- * @return
- */
- public static CellStyleModel createLeftBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , Object leftBorderColor) {
- return createBorderColorCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, leftBorderColor);
- }
- /**
- * 生成上边框线条类型样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param borderTop 上边框线条类型
- * @return
- */
- public static CellStyleModel createTopBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , BorderStyle borderTop) {
- return createBorderLineTypeCellStyleModel(sheetName, rowIndex, columnIndex, borderTop, null, null, null);
- }
- /**
- * 生成右边框线条类型样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param borderRight 右边框线条类型
- * @return
- */
- public static CellStyleModel createRightBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , BorderStyle borderRight) {
- return createBorderLineTypeCellStyleModel(sheetName, rowIndex, columnIndex, null, borderRight, null, null);
- }
- /**
- * 生成下边框线条类型样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param borderBottom 下边框线条类型
- * @return
- */
- public static CellStyleModel createBottomBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , BorderStyle borderBottom) {
- return createBorderLineTypeCellStyleModel(sheetName, rowIndex, columnIndex, null, null, borderBottom, null);
- }
- /**
- * 生成左边框线条类型样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param borderLeft 左边框线条类型
- * @return
- */
- public static CellStyleModel createLeftBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , BorderStyle borderLeft) {
- return createBorderLineTypeCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, borderLeft);
- }
- /**
- * 生成边框线条颜色样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param borderColor 边框线条颜色
- * @return
- */
- public static CellStyleModel createBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , Object borderColor) {
- return createBorderCellStyleModel(sheetName, rowIndex, columnIndex, null, borderColor);
- }
- /**
- * 生成边框线条颜色样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param topBorderColor 上边框线条颜色
- * @param rightBorderColor 右边框线条颜色
- * @param bottomBorderColor 下边框线条颜色
- * @param leftBorderColor 左边框线条颜色
- * @return
- */
- public static CellStyleModel createBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , Object topBorderColor, Object rightBorderColor, Object bottomBorderColor, Object leftBorderColor) {
- return createBorderCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null
- , topBorderColor, rightBorderColor, bottomBorderColor, leftBorderColor);
- }
- /**
- * 生成边框线条类型样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param borderLineType 边框线条类型
- * @return
- */
- public static CellStyleModel createBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , BorderStyle borderLineType) {
- return createBorderCellStyleModel(sheetName, rowIndex, columnIndex, borderLineType, null);
- }
- /**
- * 生成边框线条类型样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param borderTop 上边框线条类型
- * @param borderRight 右边框线条类型
- * @param borderBottom 下边框线条类型
- * @param borderLeft 左边框线条类型
- * @return
- */
- public static CellStyleModel createBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , BorderStyle borderTop, BorderStyle borderRight, BorderStyle borderBottom, BorderStyle borderLeft) {
- return createBorderCellStyleModel(sheetName, rowIndex, columnIndex, borderTop, borderRight, borderBottom, borderLeft
- , null, null, null, null);
- }
- /**
- * 生成边框样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param borderLineType 边框线条类型
- * @param borderColor 边框线条颜色
- * @return
- */
- public static CellStyleModel createBorderCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , BorderStyle borderLineType, Object borderColor) {
- return createBorderCellStyleModel(sheetName, rowIndex, columnIndex, borderLineType, borderLineType, borderLineType, borderLineType
- , borderColor, borderColor, borderColor, borderColor);
- }
- /**
- * 生成边框样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param borderTop 上边框线条类型
- * @param borderRight 右边框线条类型
- * @param borderBottom 下边框线条类型
- * @param borderLeft 左边框线条类型
- * @param topBorderColor 上边框线条颜色
- * @param rightBorderColor 右边框线条颜色
- * @param bottomBorderColor 下边框线条颜色
- * @param leftBorderColor 左边框线条颜色
- * @return
- */
- public static CellStyleModel createBorderCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , BorderStyle borderTop, BorderStyle borderRight, BorderStyle borderBottom, BorderStyle borderLeft, Object topBorderColor
- , Object rightBorderColor, Object bottomBorderColor, Object leftBorderColor) {
- return createCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null, null, null, null
- , null, borderTop, borderRight, borderBottom, borderLeft, topBorderColor, rightBorderColor
- , bottomBorderColor, leftBorderColor);
- }
- /**
- * 生成样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontName 字体名称(宋体)
- * @param fontHeight 字体大小
- * @param fontColor 字体颜色
- * @param fontBold 字体加粗
- * @param fontItalic 字体斜体
- * @param fontUnderLine 字体下划线
- * @param fontTypeOffset 字体上标下标
- * @param fontStrikeout 字体删除线
- * @param backgroundColor 背景颜色
- * @param borderTop 上边框线条类型
- * @param borderRight 右边框线条类型
- * @param borderBottom 下边框线条类型
- * @param borderLeft 左边框线条类型
- * @param topBorderColor 上边框线条颜色
- * @param rightBorderColor 右边框线条颜色
- * @param bottomBorderColor 下边框线条颜色
- * @param leftBorderColor 左边框线条颜色
- * @return
- */
- public static CellStyleModel createCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , String fontName, Double fontHeight, Object fontColor, Boolean fontBold, Boolean fontItalic, Byte fontUnderLine
- , Short fontTypeOffset, Boolean fontStrikeout, Object backgroundColor, BorderStyle borderTop, BorderStyle borderRight
- , BorderStyle borderBottom, BorderStyle borderLeft, Object topBorderColor, Object rightBorderColor, Object bottomBorderColor
- , Object leftBorderColor) {
- return createCellStyleModel(sheetName, rowIndex, columnIndex, fontName, fontHeight, fontColor, fontBold, fontItalic
- , fontUnderLine, fontTypeOffset, fontStrikeout, backgroundColor, borderTop, borderRight, borderBottom
- , borderLeft, topBorderColor, rightBorderColor, bottomBorderColor, leftBorderColor, null, null);
- }
- /**
- * 生成水平对齐方式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param horizontalAlignment 水平对齐方式
- * @return
- */
- public static CellStyleModel createHorizontalAlignmentCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , HorizontalAlignment horizontalAlignment) {
- return createAlignmentCellStyleModel(sheetName, rowIndex, columnIndex, horizontalAlignment, null);
- }
- /**
- * 生成垂直对齐方式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param verticalAlignment 垂直对齐方式
- * @return
- */
- public static CellStyleModel createVerticalAlignmentCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , VerticalAlignment verticalAlignment) {
- return createAlignmentCellStyleModel(sheetName, rowIndex, columnIndex, null, verticalAlignment);
- }
- /**
- * 生成对齐方式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param horizontalAlignment 水平对齐方式
- * @param verticalAlignment 垂直对齐方式
- * @return
- */
- public static CellStyleModel createAlignmentCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment) {
- return createCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null
- , null, null, null, null, null, null, null
- , null, null, null, null, null, null
- , horizontalAlignment, verticalAlignment);
- }
- /**
- * 生成样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontName 字体名称(宋体)
- * @param fontHeight 字体大小
- * @param fontColor 字体颜色
- * @param fontBold 字体加粗
- * @param fontItalic 字体斜体
- * @param fontUnderLine 字体下划线
- * @param fontTypeOffset 字体上标下标
- * @param fontStrikeout 字体删除线
- * @param backgroundColor 背景颜色
- * @param borderTop 上边框线条类型
- * @param borderRight 右边框线条类型
- * @param borderBottom 下边框线条类型
- * @param borderLeft 左边框线条类型
- * @param topBorderColor 上边框线条颜色
- * @param rightBorderColor 右边框线条颜色
- * @param bottomBorderColor 下边框线条颜色
- * @param leftBorderColor 左边框线条颜色
- * @param horizontalAlignment 水平对齐方式
- * @param verticalAlignment 垂直对齐方式
- * @return
- */
- public static CellStyleModel createCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , String fontName, Double fontHeight, Object fontColor, Boolean fontBold, Boolean fontItalic, Byte fontUnderLine
- , Short fontTypeOffset, Boolean fontStrikeout, Object backgroundColor, BorderStyle borderTop, BorderStyle borderRight
- , BorderStyle borderBottom, BorderStyle borderLeft, Object topBorderColor, Object rightBorderColor, Object bottomBorderColor
- , Object leftBorderColor, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment) {
- return createCellStyleModel(sheetName, rowIndex, columnIndex, fontName, fontHeight, fontColor, fontBold, fontItalic
- , fontUnderLine, fontTypeOffset, fontStrikeout, backgroundColor, borderTop, borderRight, borderBottom
- , borderLeft, topBorderColor, rightBorderColor, bottomBorderColor, leftBorderColor, horizontalAlignment, verticalAlignment, null);
- }
- /**
- * 生成自动换行样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param wrapText 自动换行
- * @return
- */
- public static CellStyleModel createWrapTextCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , Boolean wrapText) {
- return createCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null
- , null, null, null, null, null, null, null
- , null, null, null, null, null, null, null
- , wrapText);
- }
- /**
- * 生成样式信息
- *
- * @param sheetName sheet页名称
- * @param rowIndex 行号
- * @param columnIndex 列号
- * @param fontName 字体名称(宋体)
- * @param fontHeight 字体大小
- * @param fontColor 字体颜色
- * @param fontBold 字体加粗
- * @param fontItalic 字体斜体
- * @param fontUnderLine 字体下划线
- * @param fontTypeOffset 字体上标下标
- * @param fontStrikeout 字体删除线
- * @param backgroundColor 背景颜色
- * @param borderTop 上边框线条类型
- * @param borderRight 右边框线条类型
- * @param borderBottom 下边框线条类型
- * @param borderLeft 左边框线条类型
- * @param topBorderColor 上边框线条颜色
- * @param rightBorderColor 右边框线条颜色
- * @param bottomBorderColor 下边框线条颜色
- * @param leftBorderColor 左边框线条颜色
- * @param horizontalAlignment 水平对齐方式
- * @param verticalAlignment 垂直对齐方式
- * @param wrapText 自动换行
- * @return
- */
- public static CellStyleModel createCellStyleModel(String sheetName, int rowIndex, int columnIndex
- , String fontName, Double fontHeight, Object fontColor, Boolean fontBold, Boolean fontItalic, Byte fontUnderLine
- , Short fontTypeOffset, Boolean fontStrikeout, Object backgroundColor, BorderStyle borderTop, BorderStyle borderRight
- , BorderStyle borderBottom, BorderStyle borderLeft, Object topBorderColor, Object rightBorderColor, Object bottomBorderColor
- , Object leftBorderColor, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, Boolean wrapText) {
- CellStyleModel cellStyleModel = new CellStyleModel();
- //sheet页名称
- cellStyleModel.setSheetName(sheetName);
- //行号
- cellStyleModel.setRowIndex(rowIndex);
- //列号
- cellStyleModel.setColIndex(columnIndex);
- //设置字体样式
- //字体名称(比如宋体)
- fontName = fontName != null && StrUtil.equals(fontName, "") ? "宋体" : fontName;
- cellStyleModel.setFontName(fontName);
- //字体大小
- fontHeight = fontHeight != null && fontHeight <= 0 ? null : fontHeight;
- cellStyleModel.setFontHeight(fontHeight);
- //字体颜色
- fontColor = fontColor != null && (fontColor instanceof IndexedColors == false && fontColor instanceof XSSFColor == false)
- ? null : fontColor;
- cellStyleModel.setFontColor(fontColor);
- //字体加粗
- cellStyleModel.setFontBold(fontBold);
- //字体斜体
- cellStyleModel.setFontItalic(fontItalic);
- //字体下划线
- fontUnderLine = fontUnderLine != null && (fontUnderLine != Font.U_NONE && fontUnderLine != Font.U_SINGLE && fontUnderLine != Font.U_DOUBLE
- && fontUnderLine != Font.U_DOUBLE_ACCOUNTING && fontUnderLine != Font.U_SINGLE_ACCOUNTING) ? null : fontUnderLine;
- cellStyleModel.setFontUnderLine(fontUnderLine);
- //字体上标下标
- fontTypeOffset = fontTypeOffset != null && (fontTypeOffset != Font.SS_NONE && fontTypeOffset != Font.SS_SUB && fontTypeOffset != Font.SS_SUPER)
- ? null : fontTypeOffset;
- cellStyleModel.setFontTypeOffset(fontTypeOffset);
- //字体删除线
- cellStyleModel.setFontStrikeout(fontStrikeout);
- //背景颜色
- backgroundColor = backgroundColor != null && (backgroundColor instanceof IndexedColors == false && backgroundColor instanceof XSSFColor == false)
- ? null : backgroundColor;
- cellStyleModel.setBackgroundColor(backgroundColor);
- //边框样式
- //上边框线条类型
- cellStyleModel.setBorderTop(borderTop);
- //右边框线条类型
- cellStyleModel.setBorderRight(borderRight);
- //下边框线条类型
- cellStyleModel.setBorderBottom(borderBottom);
- //左边框线条类型
- cellStyleModel.setBorderLeft(borderLeft);
- //上边框颜色类型
- cellStyleModel.setTopBorderColor(topBorderColor);
- //右边框颜色类型
- cellStyleModel.setRightBorderColor(rightBorderColor);
- //下边框颜色类型
- cellStyleModel.setBottomBorderColor(bottomBorderColor);
- //左边框颜色类型
- cellStyleModel.setLeftBorderColor(leftBorderColor);
- //对齐方式
- //水平对齐方式
- cellStyleModel.setHorizontalAlignment(horizontalAlignment);
- //垂直对齐方式
- cellStyleModel.setVerticalAlignment(verticalAlignment);
- //自动换行
- cellStyleModel.setWrapText(wrapText);
- return cellStyleModel;
- }
-
- }
五、导出实现
- public void exportPatrolWorkOrderInfo(String startTime, String endTime, HttpServletResponse response) {
-
- List
allContentList = new ArrayList<>(); -
- //设置合并策略
- List
cellStyleList = new ArrayList<>(); - //根据时间获取工单
- List
patrolProcessExportDTOList = analysisPatrolProcessExportInfo.analyzeExportProcessInfo(new ExportWorkOrderInfoDTO(startTime, endTime)); - if (!CollectionUtils.isEmpty(patrolProcessExportDTOList)) {
- patrolProcessExportDTOList = patrolProcessExportDTOList.stream().sorted(Comparator.comparing(PatrolProcessExportDTO::getProcessInstanceStartTime)).collect(Collectors.toList());
- int i = 1;
- for (PatrolProcessExportDTO patrolProcessExportDTO : patrolProcessExportDTOList) {
-
- Map
>> nfcMap = patrolProcessExportDTO.getNfcMap(); - if (!CollectionUtils.isEmpty(nfcMap)) {
- int finalI = i;
- nfcMap.forEach((k, v) -> {
- PatrolProcessDTO patrolProcessDTO = new PatrolProcessDTO();
- BeanUtils.copyProperties(patrolProcessExportDTO, patrolProcessDTO);
- patrolProcessDTO.setOrderNum(finalI);
-
- //设置打卡点情况
- if (v.size() == 0) {
- patrolProcessDTO.setContent(k + "未打卡");
- //设置单元格背景颜色
- cellStyleList.add(CellStyleModel.createBackgroundColorCellStyleModel("巡检工单", allContentList.size(), 4, IndexedColors.YELLOW));
- } else {
- patrolProcessDTO.setContent(getNfcInfo(v, k));
- }
-
- allContentList.add(patrolProcessDTO);
-
- });
- } else {
- PatrolProcessDTO patrolProcessDTO = new PatrolProcessDTO();
- BeanUtils.copyProperties(patrolProcessExportDTO, patrolProcessDTO);
- patrolProcessDTO.setOrderNum(i);
- allContentList.add(patrolProcessDTO);
- }
- i++;
- }
- }
-
- for (int i = 0; i <= allContentList.size(); i++) {
- for (int j = 0; j < 9; j++) {
- // cellStyleList.add(CellStyleModel.createBorderCellStyleModel("巡检工单", i, j, BorderStyle.THIN, IndexedColors.BLACK));
- //设置换行策略
- cellStyleList.add(CellStyleModel.createWrapTextCellStyleModel("巡检工单", i, j, true));
- }
- }
- //向会话写入
- try {
- response.setContentType("application/vnd.ms-excel");
- response.setCharacterEncoding("utf-8");
- String fileName = URLEncoder.encode("巡检工单", "UTF-8"); //.replaceAll("\\+", "%20")
- response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
- EasyExcel.write(response.getOutputStream(), PatrolProcessDTO.class)
- .excelType(ExcelTypeEnum.XLSX)
- .autoCloseStream(Boolean.TRUE)
- // 添加自定义处理程序,相当于Spring的AOP切面
- .registerWriteHandler(new ExcelFillCellMergeStrategy(0, 8, new ArrayList<>()))
- .registerWriteHandler(new CustomCellStyleHandler(cellStyleList))
- .sheet("巡检工单").doWrite(allContentList);
-
-
- } catch (Exception e) {
- log.error("导出出错-{}", ExceptionUtils.getStackTrace(e));
- renderString(response, JSON.toJSONString(new ResultInfo().error(SystemError.SYS_10055)));
- }
-
- }
备注:由于合并策略是自定义得,在效率上没法保证,当牵扯到大数据量合并导出时,用时较长。