All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kangaroohy.plugin.excel.handler.ExcelDataWriteHandler Maven / Gradle / Ivy

There is a newer version: 3.1.3
Show newest version
package com.kangaroohy.plugin.excel.handler;

import com.alibaba.excel.metadata.data.DataFormatData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.kangaroohy.plugin.excel.annotation.ExcelNotation;
import com.kangaroohy.plugin.excel.vo.ExcelExportEnhancer;
import lombok.Data;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

import java.util.List;
import java.util.Map;

/**
 * 类 ExcelDataWriteHandler 功能描述:
* * @author hy * @version 0.0.1 * @date 2023/7/23 23:01 */ @Data public class ExcelDataWriteHandler implements SheetWriteHandler, CellWriteHandler { private final ExcelExportEnhancer excelExportEnhancer; @Override public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { Sheet sheet = writeSheetHolder.getSheet(); DataValidationHelper helper = sheet.getDataValidationHelper(); if (excelExportEnhancer.getSelectedMap().isEmpty()) { return; } excelExportEnhancer.getSelectedMap().forEach((k, v) -> { // 下拉 首行 末行 首列 末列 CellRangeAddressList list = new CellRangeAddressList(v.getStartRow(), v.getEndRow(), k, k); // 下拉值 DataValidationConstraint constraint = helper.createExplicitListConstraint(v.getSelectorData()); DataValidation validation = helper.createValidation(constraint, list); validation.setErrorStyle(DataValidation.ErrorStyle.STOP); validation.setShowErrorBox(true); validation.setSuppressDropDownArrow(true); validation.createErrorBox("提示", "请输入下拉选项中的内容"); sheet.addValidationData(validation); }); } @Override public void afterCellDispose(CellWriteHandlerContext context) { WriteCellData cellData = context.getFirstCellData(); WriteCellStyle writeCellStyle = cellData.getOrCreateStyle(); DataFormatData dataFormatData = new DataFormatData(); // 单元格设置为文本格式 dataFormatData.setIndex((short) 49); writeCellStyle.setDataFormatData(dataFormatData); if (context.getHead().equals(Boolean.TRUE)) { Cell cell = context.getCell(); WriteSheetHolder writeSheetHolder = context.getWriteSheetHolder(); Sheet sheet = writeSheetHolder.getSheet(); Workbook workbook = writeSheetHolder.getSheet().getWorkbook(); Drawing drawing = sheet.createDrawingPatriarch(); // 设置标题字体样式 WriteFont headWriteFont = new WriteFont(); // 加粗 headWriteFont.setBold(true); Map headColumnMap = excelExportEnhancer.getRequiredMap(); if (!headColumnMap.isEmpty() && headColumnMap.containsKey(cell.getColumnIndex())) { // 设置字体颜色 headWriteFont.setColor(headColumnMap.get(cell.getColumnIndex())); } writeCellStyle.setWriteFont(headWriteFont); CellStyle cellStyle = StyleUtil.buildCellStyle(workbook, null, writeCellStyle); cell.setCellStyle(cellStyle); Map notationMap = excelExportEnhancer.getNotationMap(); if (!notationMap.isEmpty() && notationMap.containsKey(cell.getColumnIndex())) { // 批注内容 String notationContext = notationMap.get(cell.getColumnIndex()); // 创建绘图对象 XSSFClientAnchor xssfClientAnchor = new XSSFClientAnchor(0, 0, 0, 0, cell.getColumnIndex(), 0, cell.getColumnIndex() + 2, 4); Comment comment = drawing.createCellComment(xssfClientAnchor); comment.setString(new XSSFRichTextString(notationContext)); cell.setCellComment(comment); } List notationList = excelExportEnhancer.getNotationList(); if (!notationList.isEmpty()) { notationList.forEach(notation -> { if (notation.columnIndex() >= 0 && notation.rowIndex() >= 0 && cell.getRowIndex() == notation.rowIndex() && cell.getColumnIndex() == notation.columnIndex()) { // 创建绘图对象 XSSFClientAnchor xssfClientAnchor = new XSSFClientAnchor(0, 0, 0, 0, cell.getColumnIndex(), notation.rowIndex(), cell.getColumnIndex() + 2, notation.columnIndex() + 4); Comment comment = drawing.createCellComment(xssfClientAnchor); comment.setString(new XSSFRichTextString(notation.value())); cell.setCellComment(comment); } }); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy