Skip to content

Commit

Permalink
完成填充支持样式
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Oct 15, 2021
1 parent adaedfc commit 5465de6
Show file tree
Hide file tree
Showing 20 changed files with 836 additions and 140 deletions.
91 changes: 3 additions & 88 deletions src/main/java/com/alibaba/excel/metadata/Head.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import com.alibaba.excel.metadata.property.LoopMergeProperty;
import com.alibaba.excel.metadata.property.StyleProperty;

import lombok.Data;

/**
* excel head
*
* @author Jiaju Zhuang
**/
@Data
public class Head {
/**
* Column index of head
Expand Down Expand Up @@ -77,92 +80,4 @@ public Head(Integer columnIndex, String fieldName, List<String> headNameList, Bo
this.forceIndex = forceIndex;
this.forceName = forceName;
}

public Integer getColumnIndex() {
return columnIndex;
}

public void setColumnIndex(Integer columnIndex) {
this.columnIndex = columnIndex;
}

public String getFieldName() {
return fieldName;
}

public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}

public List<String> getHeadNameList() {
return headNameList;
}

public void setHeadNameList(List<String> headNameList) {
this.headNameList = headNameList;
}

public ColumnWidthProperty getColumnWidthProperty() {
return columnWidthProperty;
}

public void setColumnWidthProperty(ColumnWidthProperty columnWidthProperty) {
this.columnWidthProperty = columnWidthProperty;
}

public Boolean getForceIndex() {
return forceIndex;
}

public void setForceIndex(Boolean forceIndex) {
this.forceIndex = forceIndex;
}

public Boolean getForceName() {
return forceName;
}

public void setForceName(Boolean forceName) {
this.forceName = forceName;
}

public LoopMergeProperty getLoopMergeProperty() {
return loopMergeProperty;
}

public void setLoopMergeProperty(LoopMergeProperty loopMergeProperty) {
this.loopMergeProperty = loopMergeProperty;
}

public StyleProperty getHeadStyleProperty() {
return headStyleProperty;
}

public void setHeadStyleProperty(StyleProperty headStyleProperty) {
this.headStyleProperty = headStyleProperty;
}

public StyleProperty getContentStyleProperty() {
return contentStyleProperty;
}

public void setContentStyleProperty(StyleProperty contentStyleProperty) {
this.contentStyleProperty = contentStyleProperty;
}

public FontProperty getHeadFontProperty() {
return headFontProperty;
}

public void setHeadFontProperty(FontProperty headFontProperty) {
this.headFontProperty = headFontProperty;
}

public FontProperty getContentFontProperty() {
return contentFontProperty;
}

public void setContentFontProperty(FontProperty contentFontProperty) {
this.contentFontProperty = contentFontProperty;
}
}
17 changes: 7 additions & 10 deletions src/main/java/com/alibaba/excel/util/FieldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Map;

import com.alibaba.excel.metadata.NullObject;
import com.alibaba.excel.write.metadata.RowData;

import net.sf.cglib.beans.BeanMap;

Expand All @@ -20,19 +19,17 @@ public class FieldUtils {

private static final int START_RESOLVE_FIELD_LENGTH = 2;

public static Class<?> getFieldClass(Map dataMap, String filedName) {
public static Class<?> getFieldClass(Map dataMap, String filedName, Object value) {
if (dataMap instanceof BeanMap) {
return ((BeanMap)dataMap).getPropertyType(filedName);
}
Object value = dataMap.get(filedName);
if (value != null) {
return value.getClass();
Class<?> fieldClass = ((BeanMap)dataMap).getPropertyType(filedName);
if (fieldClass != null) {
return fieldClass;
}
}
return nullObjectClass;
return getFieldClass(value);
}

public static Class<?> getFieldClass(RowData rowData, int dataIndex) {
Object value = rowData.get(dataIndex);
public static Class<?> getFieldClass(Object value) {
if (value != null) {
return value.getClass();
}
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/alibaba/excel/util/StyleUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.usermodel.HSSFFont;
Expand All @@ -27,6 +28,7 @@
/**
* @author jipengfei
*/
@Slf4j
public class StyleUtil {

private StyleUtil() {}
Expand Down Expand Up @@ -123,15 +125,24 @@ public static short buildDataFormat(Workbook workbook, DataFormatData dataFormat
return dataFormatData.getIndex();
}
if (StringUtils.isNotBlank(dataFormatData.getFormat())) {
if (log.isDebugEnabled()) {
log.info("create new data fromat:{}", dataFormatData);
}
DataFormat dataFormatCreate = workbook.createDataFormat();
return dataFormatCreate.getFormat(dataFormatData.getFormat());
}
return BuiltinFormats.GENERAL;
}

public static Font buildFont(Workbook workbook, Font originFont, WriteFont writeFont) {
Font font = createFont(workbook, originFont);
if (writeFont == null || font == null) {
if (log.isDebugEnabled()) {
log.info("create new font:{},{}", writeFont, originFont);
}
if (writeFont == null && originFont == null) {
return null;
}
Font font = createFont(workbook, originFont, writeFont);
if (writeFont == null) {
return font;
}
if (writeFont.getFontName() != null) {
Expand Down Expand Up @@ -164,7 +175,7 @@ public static Font buildFont(Workbook workbook, Font originFont, WriteFont write
return font;
}

private static Font createFont(Workbook workbook, Font originFont) {
private static Font createFont(Workbook workbook, Font originFont, WriteFont writeFont) {
Font font = workbook.createFont();
if (originFont == null) {
return font;
Expand All @@ -176,7 +187,10 @@ private static Font createFont(Workbook workbook, Font originFont) {
xssfFont.setFontHeightInPoints(xssfOriginFont.getFontHeightInPoints());
xssfFont.setItalic(xssfOriginFont.getItalic());
xssfFont.setStrikeout(xssfOriginFont.getStrikeout());
xssfFont.setColor(new XSSFColor(xssfOriginFont.getXSSFColor().getRGB(), null));
// Colors cannot be overwritten
if (writeFont == null || writeFont.getColor() == null) {
xssfFont.setColor(new XSSFColor(xssfOriginFont.getXSSFColor().getRGB(), null));
}
xssfFont.setTypeOffset(xssfOriginFont.getTypeOffset());
xssfFont.setUnderline(xssfOriginFont.getUnderline());
xssfFont.setCharSet(xssfOriginFont.getCharSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ protected WriteCellData<?> converterAndSet(WriteHolder currentWriteHolder, Class

}


private void fillFormula(Cell cell, FormulaData formulaData) {
if (formulaData == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void doAddBasicTypeToExcel(RowData oneRowData, Head head, Row row, int r
WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE);
Object value = oneRowData.get(dataIndex);
WriteCellData<?> cellData = converterAndSet(writeContext.currentWriteHolder(),
FieldUtils.getFieldClass(oneRowData, dataIndex), null, cell, value, null, head, relativeRowIndex);
FieldUtils.getFieldClass(value), null, cell, value, null, head, relativeRowIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE);
}

Expand Down Expand Up @@ -173,7 +173,7 @@ private void addJavaObjectToExcel(Object oneRowData, Row row, int relativeRowInd
Cell cell = WorkBookUtil.createCell(row, maxCellIndex++);
WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE);
WriteCellData<?> cellData = converterAndSet(currentWriteHolder,
FieldUtils.getFieldClass(beanMap, filedName), null, cell, value, null, null, relativeRowIndex);
FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, value, null, null, relativeRowIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private void doFill(List<AnalysisCell> analysisCellList, Object oneRowData, Fill
}
Object value = dataMap.get(variable);
WriteCellData<?> cellData = converterAndSet(writeSheetHolder,
FieldUtils.getFieldClass(dataMap, variable),
FieldUtils.getFieldClass(dataMap, variable, value),
null, cell, value, fieldNameContentPropertyMap.get(variable), null, relativeRowIndex);

// Restyle
Expand All @@ -221,8 +221,9 @@ private void doFill(List<AnalysisCell> analysisCellList, Object oneRowData, Fill
continue;
}
Object value = dataMap.get(variable);
WriteCellData<?> cellData = convert(writeSheetHolder, value == null ? null : value.getClass(),
CellDataTypeEnum.STRING, cell, value, fieldNameContentPropertyMap.get(variable));
WriteCellData<?> cellData = convert(writeSheetHolder,
FieldUtils.getFieldClass(dataMap, variable, value), CellDataTypeEnum.STRING, cell, value,
fieldNameContentPropertyMap.get(variable));
cellDataList.add(cellData);
CellDataTypeEnum type = cellData.getType();
if (type != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public int order() {
@Override
public void afterCellDispose(CellWriteHandlerContext context) {
List<WriteCellData<?>> cellDataList = context.getCellDataList();
if (CollectionUtils.isEmpty(cellDataList) || cellDataList.size() > 1) {
if (CollectionUtils.isEmpty(cellDataList)) {
return;
}
WriteCellData<?> cellData = cellDataList.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteBasicParameter;
Expand Down Expand Up @@ -242,13 +243,15 @@ public int order() {
}

@Override
protected WriteCellStyle headCellStyle(Head head) {
return WriteCellStyle.build(head.getHeadStyleProperty(), head.getHeadFontProperty());
protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) {
//return WriteCellStyle.build(head.getHeadStyleProperty(), head.getHeadFontProperty());
return null;
}

@Override
protected WriteCellStyle contentCellStyle(Head head) {
return WriteCellStyle.build(head.getContentStyleProperty(), head.getContentFontProperty());
protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
//return WriteCellStyle.build(head.getContentStyleProperty(), head.getContentFontProperty());
return null;
}
};
handlerList.add(styleStrategy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.alibaba.excel.write.metadata.style.WriteFont;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
Expand All @@ -39,6 +40,7 @@
* @author Jiaju Zhuang
*/
@Data
@Slf4j
public class WriteWorkbookHolder extends AbstractWriteHolder {
/***
* Current poi Workbook.This is only for writing, and there may be no data in version 07 when template data needs to
Expand Down Expand Up @@ -254,15 +256,15 @@ public CellStyle createCellStyle(WriteCellStyle writeCellStyle, CellStyle origin

short styleIndex = -1;
Font originFont = null;
boolean useCache = false;
boolean useCache = true;
if (originCellStyle != null) {
styleIndex = originCellStyle.getIndex();
if (originCellStyle instanceof XSSFCellStyle) {
originFont = ((XSSFCellStyle)originCellStyle).getFont();
} else if (originCellStyle instanceof HSSFCellStyle) {
originFont = ((HSSFCellStyle)originCellStyle).getFont(workbook);
}
useCache = true;
useCache = false;
}

Map<WriteCellStyle, CellStyle> cellStyleMap = cellStyleIndexMap.computeIfAbsent(styleIndex,
Expand All @@ -271,9 +273,18 @@ public CellStyle createCellStyle(WriteCellStyle writeCellStyle, CellStyle origin
if (cellStyle != null) {
return cellStyle;
}
if (log.isDebugEnabled()) {
log.info("create new style:{},{}", writeCellStyle, originCellStyle);
}
cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, writeCellStyle);
cellStyle.setDataFormat(createDataFormat(writeCellStyle.getDataFormatData(), useCache));
cellStyle.setFont(createFont(writeCellStyle.getWriteFont(), originFont, useCache));
Short dataFormat = createDataFormat(writeCellStyle.getDataFormatData(), useCache);
if (dataFormat != null) {
cellStyle.setDataFormat(dataFormat);
}
Font font = createFont(writeCellStyle.getWriteFont(), originFont, useCache);
if (font != null) {
cellStyle.setFont(font);
}
cellStyleMap.put(writeCellStyle, cellStyle);
return cellStyle;
}
Expand Down Expand Up @@ -307,6 +318,9 @@ public Font createFont(WriteFont writeFont, Font originFont, boolean useCache) {
* @return
*/
public Short createDataFormat(DataFormatData dataFormatData, boolean useCache) {
if (dataFormatData == null) {
return null;
}
if (!useCache) {
return StyleUtil.buildDataFormat(workbook, dataFormatData);
}
Expand Down
Loading

0 comments on commit 5465de6

Please sign in to comment.