Browse Source

扩充写的style

pull/507/head
zhuangjiaju 5 years ago
parent
commit
ad9544c427
  1. 31
      src/main/java/com/alibaba/excel/annotation/write/style/ContentStyle.java
  2. 27
      src/main/java/com/alibaba/excel/annotation/write/style/HeadStyle.java
  3. 48
      src/main/java/com/alibaba/excel/metadata/CellStyle.java
  4. 2
      src/main/java/com/alibaba/excel/metadata/Font.java
  5. 13
      src/main/java/com/alibaba/excel/metadata/Head.java
  6. 84
      src/main/java/com/alibaba/excel/metadata/property/CellStyleProperty.java
  7. 9
      src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java
  8. 179
      src/main/java/com/alibaba/excel/util/StyleUtil.java
  9. 20
      src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerLoader.java
  10. 88
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  11. 315
      src/main/java/com/alibaba/excel/write/metadata/style/WriteCellStyle.java
  12. 143
      src/main/java/com/alibaba/excel/write/metadata/style/WriteFont.java
  13. 15
      src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java
  14. 13
      src/main/java/com/alibaba/excel/write/style/AbstractColumnCellStyleStrategy.java
  15. 43
      src/main/java/com/alibaba/excel/write/style/RowCellStyleStrategy.java

31
src/main/java/com/alibaba/excel/annotation/write/style/ContentStyle.java

@ -1,31 +0,0 @@
package com.alibaba.excel.annotation.write.style;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.poi.ss.usermodel.IndexedColors;
/**
* Convert number format.
*
* <li>write: It can be used on classes that inherit {@link Number}
* <li>read: It can be used on classes {@link String}
*
* @author zhuangjiaju
*/
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ContentStyle {
String fontName() default "宋体";
short fontHeightInPoints() default (short)14;
boolean bold() default true;
IndexedColors indexedColors() default IndexedColors.WHITE1;
}

27
src/main/java/com/alibaba/excel/annotation/write/style/HeadStyle.java

@ -1,27 +0,0 @@
package com.alibaba.excel.annotation.write.style;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.poi.ss.usermodel.IndexedColors;
/**
* 配置
*
* @author zhuangjiaju
*/
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface HeadStyle {
String fontName() default "宋体";
short fontHeightInPoints() default (short)14;
boolean bold() default true;
IndexedColors indexedColors() default IndexedColors.GREY_25_PERCENT;
}

48
src/main/java/com/alibaba/excel/metadata/CellStyle.java

@ -1,48 +0,0 @@
package com.alibaba.excel.metadata;
import org.apache.poi.ss.usermodel.IndexedColors;
/**
* Simple cell style
*
* @author zhuangjiaju
*/
public class CellStyle {
/**
* 表头背景颜色
*/
private IndexedColors indexedColors;
/**
* 表头字体样式
*/
private Font font;
public CellStyle() {
}
public CellStyle(String fontName, Short fontHeightInPoints, Boolean bold, IndexedColors indexedColors) {
Font font = new Font();
font.setFontName(fontName);
font.setFontHeightInPoints(fontHeightInPoints);
font.setBold(bold);
this.font = font;
this.indexedColors = indexedColors;
}
public IndexedColors getIndexedColors() {
return indexedColors;
}
public void setIndexedColors(IndexedColors indexedColors) {
this.indexedColors = indexedColors;
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
}

2
src/main/java/com/alibaba/excel/metadata/Font.java

@ -3,7 +3,9 @@ package com.alibaba.excel.metadata;
/**
*
* @author jipengfei
* @deprecated please use {@link com.alibaba.excel.write.metadata.style.WriteFont}
*/
@Deprecated
public class Font {
/**

13
src/main/java/com/alibaba/excel/metadata/Head.java

@ -3,7 +3,6 @@ package com.alibaba.excel.metadata;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.excel.metadata.property.CellStyleProperty;
import com.alibaba.excel.metadata.property.ColumnWidthProperty;
/**
@ -28,10 +27,6 @@ public class Head {
* Whether index is specified
*/
private Boolean forceIndex;
/**
* Cell style property
*/
private CellStyleProperty cellStyleProperty;
/**
* column with
*/
@ -79,14 +74,6 @@ public class Head {
this.headNameList = headNameList;
}
public CellStyleProperty getCellStyleProperty() {
return cellStyleProperty;
}
public void setCellStyleProperty(CellStyleProperty cellStyleProperty) {
this.cellStyleProperty = cellStyleProperty;
}
public ColumnWidthProperty getColumnWidthProperty() {
return columnWidthProperty;
}

84
src/main/java/com/alibaba/excel/metadata/property/CellStyleProperty.java

@ -1,84 +0,0 @@
package com.alibaba.excel.metadata.property;
import org.apache.poi.ss.usermodel.IndexedColors;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadStyle;
/**
* Configuration from annotations
*
* @author zhuangjiaju
*/
public class CellStyleProperty {
private String fontName;
private Short fontHeightInPoints;
private Boolean bold;
private IndexedColors indexedColors;
public CellStyleProperty(String fontName, Short fontHeightInPoints, Boolean bold, IndexedColors indexedColors) {
this.fontName = fontName;
this.fontHeightInPoints = fontHeightInPoints;
this.bold = bold;
this.indexedColors = indexedColors;
}
public static CellStyleProperty build(HeadStyle headStyle) {
if (headStyle == null) {
return null;
}
boolean isDefault = "宋体".equals(headStyle.fontName()) && headStyle.fontHeightInPoints() == 14
&& headStyle.bold() && IndexedColors.GREY_25_PERCENT.equals(headStyle.indexedColors());
if (isDefault) {
return null;
}
return new CellStyleProperty(headStyle.fontName(), headStyle.fontHeightInPoints(), headStyle.bold(),
headStyle.indexedColors());
}
public static CellStyleProperty build(ContentStyle contentStyle) {
if (contentStyle == null) {
return null;
}
boolean isDefault = "宋体".equals(contentStyle.fontName()) && contentStyle.fontHeightInPoints() == 14
&& contentStyle.bold() && IndexedColors.WHITE1.equals(contentStyle.indexedColors());
if (isDefault) {
return null;
}
return new CellStyleProperty(contentStyle.fontName(), contentStyle.fontHeightInPoints(), contentStyle.bold(),
contentStyle.indexedColors());
}
public String getFontName() {
return fontName;
}
public void setFontName(String fontName) {
this.fontName = fontName;
}
public Short getFontHeightInPoints() {
return fontHeightInPoints;
}
public void setFontHeightInPoints(Short fontHeightInPoints) {
this.fontHeightInPoints = fontHeightInPoints;
}
public Boolean getBold() {
return bold;
}
public void setBold(Boolean bold) {
this.bold = bold;
}
public IndexedColors getIndexedColors() {
return indexedColors;
}
public void setIndexedColors(IndexedColors indexedColors) {
this.indexedColors = indexedColors;
}
}

9
src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java

@ -21,7 +21,6 @@ public class ExcelContentProperty {
* Custom defined converters
*/
private Converter converter;
private CellStyleProperty cellStyleProperty;
private DateTimeFormatProperty dateTimeFormatProperty;
private NumberFormatProperty numberFormatProperty;
@ -41,14 +40,6 @@ public class ExcelContentProperty {
this.head = head;
}
public CellStyleProperty getCellStyleProperty() {
return cellStyleProperty;
}
public void setCellStyleProperty(CellStyleProperty cellStyleProperty) {
this.cellStyleProperty = cellStyleProperty;
}
public DateTimeFormatProperty getDateTimeFormatProperty() {
return dateTimeFormatProperty;
}

179
src/main/java/com/alibaba/excel/util/StyleUtil.java

@ -9,23 +9,20 @@ import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
/**
* @author jipengfei
*/
public class StyleUtil {
/**
*
* @param workbook
* @return
*/
public static CellStyle buildDefaultCellStyle(Workbook workbook) {
CellStyle newCellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short)14);
font.setBold(true);
newCellStyle.setFont(font);
newCellStyle.setWrapText(true);
newCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
newCellStyle.setAlignment(HorizontalAlignment.CENTER);
@ -38,80 +35,148 @@ public class StyleUtil {
}
/**
* Build style
* Build head cell style
*
* @param workbook
* @param cs
* @param writeCellStyle
* @return
*/
public static CellStyle buildHeadCellStyle(Workbook workbook, com.alibaba.excel.metadata.CellStyle cs) {
public static CellStyle buildHeadCellStyle(Workbook workbook, WriteCellStyle writeCellStyle) {
CellStyle cellStyle = buildDefaultCellStyle(workbook);
if (cs == null) {
if (writeCellStyle == null) {
return cellStyle;
}
return buildCellStyle(workbook, cellStyle, cs.getFont(), cs.getIndexedColors());
}
/**
*
* @param workbook
* @param f
* @param indexedColors
* @return
*/
public static CellStyle buildHeadCellStyle(Workbook workbook, com.alibaba.excel.metadata.Font f,
IndexedColors indexedColors) {
CellStyle cellStyle = buildDefaultCellStyle(workbook);
return buildCellStyle(workbook, cellStyle, f, indexedColors);
buildCellStyle(workbook, cellStyle, writeCellStyle, true);
return cellStyle;
}
/**
* Build style
* Build content cell style
*
* @param workbook
* @param cs
* @param writeCellStyle
* @return
*/
public static CellStyle buildContentCellStyle(Workbook workbook, com.alibaba.excel.metadata.CellStyle cs) {
public static CellStyle buildContentCellStyle(Workbook workbook, WriteCellStyle writeCellStyle) {
CellStyle cellStyle = workbook.createCellStyle();
if (cs == null) {
if (writeCellStyle == null) {
return cellStyle;
}
return buildCellStyle(workbook, cellStyle, cs.getFont(), cs.getIndexedColors());
buildCellStyle(workbook, cellStyle, writeCellStyle, false);
return cellStyle;
}
/**
*
* @param workbook
* @param f
* @param indexedColors
* @return
*/
public static CellStyle buildContentCellStyle(Workbook workbook, com.alibaba.excel.metadata.Font f,
IndexedColors indexedColors) {
CellStyle cellStyle = workbook.createCellStyle();
return buildCellStyle(workbook, cellStyle, f, indexedColors);
private static void buildCellStyle(Workbook workbook, CellStyle cellStyle, WriteCellStyle writeCellStyle,
boolean isHead) {
buildFont(workbook, cellStyle, writeCellStyle.getWriteFont(), isHead);
if (writeCellStyle.getDataFormat() != null) {
cellStyle.setDataFormat(writeCellStyle.getDataFormat());
}
if (writeCellStyle.getHidden() != null) {
cellStyle.setHidden(writeCellStyle.getHidden());
}
if (writeCellStyle.getLocked() != null) {
cellStyle.setLocked(writeCellStyle.getLocked());
}
if (writeCellStyle.getQuotePrefix() != null) {
cellStyle.setQuotePrefixed(writeCellStyle.getQuotePrefix());
}
if (writeCellStyle.getHorizontalAlignment() != null) {
cellStyle.setAlignment(writeCellStyle.getHorizontalAlignment());
}
if (writeCellStyle.getWrapped() != null) {
cellStyle.setWrapText(writeCellStyle.getWrapped());
}
if (writeCellStyle.getVerticalAlignment() != null) {
cellStyle.setVerticalAlignment(writeCellStyle.getVerticalAlignment());
}
if (writeCellStyle.getRotation() != null) {
cellStyle.setRotation(writeCellStyle.getRotation());
}
if (writeCellStyle.getIndent() != null) {
cellStyle.setIndention(writeCellStyle.getIndent());
}
if (writeCellStyle.getBorderLeft() != null) {
cellStyle.setBorderLeft(writeCellStyle.getBorderLeft());
}
if (writeCellStyle.getBorderRight() != null) {
cellStyle.setBorderRight(writeCellStyle.getBorderRight());
}
if (writeCellStyle.getBorderTop() != null) {
cellStyle.setBorderTop(writeCellStyle.getBorderTop());
}
if (writeCellStyle.getBorderBottom() != null) {
cellStyle.setBorderBottom(writeCellStyle.getBorderBottom());
}
if (writeCellStyle.getLeftBorderColor() != null) {
cellStyle.setLeftBorderColor(writeCellStyle.getLeftBorderColor());
}
if (writeCellStyle.getRightBorderColor() != null) {
cellStyle.setRightBorderColor(writeCellStyle.getRightBorderColor());
}
if (writeCellStyle.getTopBorderColor() != null) {
cellStyle.setTopBorderColor(writeCellStyle.getTopBorderColor());
}
if (writeCellStyle.getBottomBorderColor() != null) {
cellStyle.setBottomBorderColor(writeCellStyle.getBottomBorderColor());
}
if (writeCellStyle.getFillPatternType() != null) {
cellStyle.setFillPattern(writeCellStyle.getFillPatternType());
}
if (writeCellStyle.getFillBackgroundColor() != null) {
cellStyle.setFillBackgroundColor(writeCellStyle.getFillBackgroundColor());
}
if (writeCellStyle.getFillForegroundColor() != null) {
cellStyle.setFillForegroundColor(writeCellStyle.getFillForegroundColor());
}
if (writeCellStyle.getShrinkToFit() != null) {
cellStyle.setShrinkToFit(writeCellStyle.getShrinkToFit());
}
}
/**
*
* @param workbook
* @param f
* @param indexedColors
* @return
*/
private static CellStyle buildCellStyle(Workbook workbook, CellStyle cellStyle, com.alibaba.excel.metadata.Font f,
IndexedColors indexedColors) {
if (f != null) {
Font font = workbook.createFont();
font.setFontName(f.getFontName());
font.setFontHeightInPoints(f.getFontHeightInPoints());
font.setBold(f.isBold());
private static void buildFont(Workbook workbook, CellStyle cellStyle, WriteFont writeFont, boolean isHead) {
Font font = null;
if (isHead) {
font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short)14);
font.setBold(true);
cellStyle.setFont(font);
}
if (indexedColors != null) {
cellStyle.setFillForegroundColor(indexedColors.getIndex());
if (writeFont == null) {
return;
}
return cellStyle;
if (!isHead) {
font = workbook.createFont();
cellStyle.setFont(font);
}
if (writeFont.getFontName() != null) {
font.setFontName(writeFont.getFontName());
}
if (writeFont.getFontHeightInPoints() != null) {
font.setFontHeightInPoints(writeFont.getFontHeightInPoints());
}
if (writeFont.getItalic() != null) {
font.setItalic(writeFont.getItalic());
}
if (writeFont.getStrikeout() != null) {
font.setStrikeout(writeFont.getStrikeout());
}
if (writeFont.getColor() != null) {
font.setColor(writeFont.getColor());
}
if (writeFont.getTypeOffset() != null) {
font.setTypeOffset(writeFont.getTypeOffset());
}
if (writeFont.getUnderline() != null) {
font.setUnderline(writeFont.getUnderline());
}
if (writeFont.getCharset() != null) {
font.setCharSet(writeFont.getCharset());
}
if (writeFont.getBold() != null) {
font.setBold(writeFont.getBold());
}
}
}

20
src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerLoader.java

@ -5,8 +5,8 @@ import java.util.List;
import org.apache.poi.ss.usermodel.IndexedColors;
import com.alibaba.excel.metadata.CellStyle;
import com.alibaba.excel.metadata.Font;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.RowCellStyleStrategy;
/**
@ -23,14 +23,14 @@ public class DefaultWriteHandlerLoader {
*/
public static List<WriteHandler> loadDefaultHandler() {
List<WriteHandler> handlerList = new ArrayList<WriteHandler>();
CellStyle headCellStyle = new CellStyle();
Font font = new Font();
headCellStyle.setFont(font);
font.setFontName("宋体");
font.setFontHeightInPoints((short)14);
font.setBold(true);
headCellStyle.setIndexedColors(IndexedColors.GREY_25_PERCENT);
handlerList.add(new RowCellStyleStrategy(headCellStyle, new ArrayList<CellStyle>()));
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontName("宋体");
headWriteFont.setFontHeightInPoints((short)14);
headWriteFont.setBold(true);
headWriteCellStyle.setWriteFont(headWriteFont);
handlerList.add(new RowCellStyleStrategy(headWriteCellStyle, new ArrayList<WriteCellStyle>()));
return handlerList;
}

88
src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java

@ -9,6 +9,7 @@ import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import com.alibaba.excel.converters.Converter;
@ -18,13 +19,13 @@ import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.event.NotRepeatExecutor;
import com.alibaba.excel.event.Order;
import com.alibaba.excel.metadata.AbstractHolder;
import com.alibaba.excel.metadata.CellStyle;
import com.alibaba.excel.metadata.Font;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.TableStyle;
import com.alibaba.excel.metadata.property.CellStyleProperty;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.metadata.property.RowHeightProperty;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.DefaultWriteHandlerLoader;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler;
@ -32,8 +33,9 @@ import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.WriteBasicParameter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.property.ExcelWriteHeadProperty;
import com.alibaba.excel.write.style.AbstractColumnCellStyleStrategy;
import com.alibaba.excel.write.style.RowCellStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy;
@ -103,13 +105,19 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
compatibleOldCode(writeBasicParameter);
// Set writeHandlerMap
List<WriteHandler> handlerList = new ArrayList<WriteHandler>();
List<WriteHandler> handlerList;
if (parentAbstractWriteHolder == null) {
handlerList = DefaultWriteHandlerLoader.loadDefaultHandler();
} else {
handlerList = new ArrayList<WriteHandler>();
}
// Initialization Annotation
initAnnotationConfig(handlerList);
if (writeBasicParameter.getCustomWriteHandlerList() != null
&& !writeBasicParameter.getCustomWriteHandlerList().isEmpty()) {
handlerList.addAll(writeBasicParameter.getCustomWriteHandlerList());
}
// Initialization Annotation
initAnnotationConfig(handlerList);
Map<Class<? extends WriteHandler>, List<WriteHandler>> parentWriteHandlerMap = null;
if (parentAbstractWriteHolder != null) {
@ -160,13 +168,25 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
if (writeBasicParameter.getCustomWriteHandlerList() == null) {
writeBasicParameter.setCustomWriteHandlerList(new ArrayList<WriteHandler>());
}
CellStyle headCellStyle = new CellStyle();
headCellStyle.setFont(tableStyle.getTableHeadFont());
headCellStyle.setIndexedColors(tableStyle.getTableContentBackGroundColor());
CellStyle contentCellStyle = new CellStyle();
contentCellStyle.setFont(tableStyle.getTableContentFont());
contentCellStyle.setIndexedColors(tableStyle.getTableContentBackGroundColor());
writeBasicParameter.getCustomWriteHandlerList().add(new RowCellStyleStrategy(headCellStyle, contentCellStyle));
writeBasicParameter.getCustomWriteHandlerList()
.add(new RowCellStyleStrategy(
buildWriteCellStyle(tableStyle.getTableHeadFont(), tableStyle.getTableHeadBackGroundColor()),
buildWriteCellStyle(tableStyle.getTableContentFont(), tableStyle.getTableContentBackGroundColor())));
}
@Deprecated
private WriteCellStyle buildWriteCellStyle(Font font, IndexedColors indexedColors) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
if (indexedColors != null) {
writeCellStyle.setFillForegroundColor(indexedColors.getIndex());
}
if (font != null) {
WriteFont writeFont = new WriteFont();
writeFont.setFontName(font.getFontName());
writeFont.setFontHeightInPoints(font.getFontHeightInPoints());
writeFont.setBold(font.isBold());
}
return writeCellStyle;
}
@Deprecated
@ -196,24 +216,14 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
Map<Integer, Head> headMap = getExcelWriteHeadProperty().getHeadMap();
Map<Integer, ExcelContentProperty> contentPropertyMap = getExcelWriteHeadProperty().getContentPropertyMap();
boolean hasCellStyle = false;
boolean hasColumnWidth = false;
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
if (entry.getValue().getCellStyleProperty() != null) {
hasCellStyle = true;
}
if (entry.getValue().getColumnWidthProperty() != null) {
hasColumnWidth = true;
}
ExcelContentProperty excelContentProperty = contentPropertyMap.get(entry.getKey());
if (excelContentProperty.getCellStyleProperty() != null) {
hasCellStyle = true;
break;
}
}
if (hasCellStyle) {
dealCellStyle(handlerList, contentPropertyMap);
}
if (hasColumnWidth) {
dealColumnWidth(handlerList);
}
@ -252,36 +262,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
handlerList.add(columnWidthStyleStrategy);
}
private void dealCellStyle(List<WriteHandler> handlerList,
final Map<Integer, ExcelContentProperty> contentPropertyMap) {
WriteHandler columnCellStyleStrategy = new AbstractColumnCellStyleStrategy() {
@Override
protected CellStyle headCellStyle(Head head) {
if (head == null || head.getCellStyleProperty() == null) {
return null;
}
CellStyleProperty cellStyleProperty = head.getCellStyleProperty();
return new CellStyle(cellStyleProperty.getFontName(), cellStyleProperty.getFontHeightInPoints(),
cellStyleProperty.getBold(), cellStyleProperty.getIndexedColors());
}
@Override
protected CellStyle contentCellStyle(Head head) {
if (head == null) {
return null;
}
ExcelContentProperty excelContentProperty = contentPropertyMap.get(head.getColumnIndex());
if (excelContentProperty == null || excelContentProperty.getCellStyleProperty() == null) {
return null;
}
CellStyleProperty cellStyleProperty = excelContentProperty.getCellStyleProperty();
return new CellStyle(cellStyleProperty.getFontName(), cellStyleProperty.getFontHeightInPoints(),
cellStyleProperty.getBold(), cellStyleProperty.getIndexedColors());
}
};
handlerList.add(columnCellStyleStrategy);
}
protected Map<Class<? extends WriteHandler>, List<WriteHandler>> sortAndClearUpHandler(
List<WriteHandler> handlerList, Map<Class<? extends WriteHandler>, List<WriteHandler>> parentHandlerMap) {
// add

315
src/main/java/com/alibaba/excel/write/metadata/style/WriteCellStyle.java

@ -0,0 +1,315 @@
package com.alibaba.excel.write.metadata.style;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IgnoredErrorType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
/**
* Cell style when writing
*
* @author zhuangjiaju
*/
public class WriteCellStyle {
/**
* Set the data format (must be a valid format). Built in formats are defined at {@link BuiltinFormats}.
*/
private Short dataFormat;
/**
* Set the font for this style
*/
private WriteFont writeFont;
/**
* Set the cell's using this style to be hidden
*/
private Boolean hidden;
/**
* Set the cell's using this style to be locked
*/
private Boolean locked;
/**
* Turn on or off "Quote Prefix" or "123 Prefix" for the style, which is used to tell Excel that the thing which
* looks like a number or a formula shouldn't be treated as on. Turning this on is somewhat (but not completely, see
* {@link IgnoredErrorType}) like prefixing the cell value with a ' in Excel
*/
private Boolean quotePrefix;
/**
* Set the type of horizontal alignment for the cell
*/
private HorizontalAlignment horizontalAlignment;
/**
* Set whether the text should be wrapped. Setting this flag to <code>true</code> make all content visible within a
* cell by displaying it on multiple lines
*
*/
private Boolean wrapped;
/**
* Set the type of vertical alignment for the cell
*/
private VerticalAlignment verticalAlignment;
/**
* Set the degree of rotation for the text in the cell.
*
* Note: HSSF uses values from -90 to 90 degrees, whereas XSSF uses values from 0 to 180 degrees. The
* implementations of this method will map between these two value-ranges accordingly, however the corresponding
* getter is returning values in the range mandated by the current type of Excel file-format that this CellStyle is
* applied to.
*/
private Short rotation;
/**
* Set the number of spaces to indent the text in the cell
*/
private Short indent;
/**
* Set the type of border to use for the left border of the cell
*/
private BorderStyle borderLeft;
/**
* Set the type of border to use for the right border of the cell
*/
private BorderStyle borderRight;
/**
* Set the type of border to use for the top border of the cell
*/
private BorderStyle borderTop;
/**
* Set the type of border to use for the bottom border of the cell
*/
private BorderStyle borderBottom;
/**
* Set the color to use for the left border
*
* @see IndexedColors
*/
private Short leftBorderColor;
/**
* Set the color to use for the right border
*
* @see IndexedColors
*
*/
private Short rightBorderColor;
/**
* Set the color to use for the top border
*
* @see IndexedColors
*
*/
private Short topBorderColor;
/**
* Set the color to use for the bottom border
*
* @see IndexedColors
*
*/
private Short bottomBorderColor;
/**
* Setting to one fills the cell with the foreground color... No idea about other values
*
* @see FillPatternType#SOLID_FOREGROUND
*/
private FillPatternType fillPatternType;
/**
* Set the background fill color.
*
* @see IndexedColors
*
*/
private Short fillBackgroundColor;
/**
* Set the foreground fill color <i>Note: Ensure Foreground color is set prior to background color.</i>
*
* @see IndexedColors
*
*/
private Short fillForegroundColor;
/**
* Controls if the Cell should be auto-sized to shrink to fit if the text is too long
*/
private Boolean shrinkToFit;
public Short getDataFormat() {
return dataFormat;
}
public void setDataFormat(Short dataFormat) {
this.dataFormat = dataFormat;
}
public WriteFont getWriteFont() {
return writeFont;
}
public void setWriteFont(WriteFont writeFont) {
this.writeFont = writeFont;
}
public Boolean getHidden() {
return hidden;
}
public void setHidden(Boolean hidden) {
this.hidden = hidden;
}
public Boolean getLocked() {
return locked;
}
public void setLocked(Boolean locked) {
this.locked = locked;
}
public Boolean getQuotePrefix() {
return quotePrefix;
}
public void setQuotePrefix(Boolean quotePrefix) {
this.quotePrefix = quotePrefix;
}
public HorizontalAlignment getHorizontalAlignment() {
return horizontalAlignment;
}
public void setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {
this.horizontalAlignment = horizontalAlignment;
}
public Boolean getWrapped() {
return wrapped;
}
public void setWrapped(Boolean wrapped) {
this.wrapped = wrapped;
}
public VerticalAlignment getVerticalAlignment() {
return verticalAlignment;
}
public void setVerticalAlignment(VerticalAlignment verticalAlignment) {
this.verticalAlignment = verticalAlignment;
}
public Short getRotation() {
return rotation;
}
public void setRotation(Short rotation) {
this.rotation = rotation;
}
public Short getIndent() {
return indent;
}
public void setIndent(Short indent) {
this.indent = indent;
}
public BorderStyle getBorderLeft() {
return borderLeft;
}
public void setBorderLeft(BorderStyle borderLeft) {
this.borderLeft = borderLeft;
}
public BorderStyle getBorderRight() {
return borderRight;
}
public void setBorderRight(BorderStyle borderRight) {
this.borderRight = borderRight;
}
public BorderStyle getBorderTop() {
return borderTop;
}
public void setBorderTop(BorderStyle borderTop) {
this.borderTop = borderTop;
}
public BorderStyle getBorderBottom() {
return borderBottom;
}
public void setBorderBottom(BorderStyle borderBottom) {
this.borderBottom = borderBottom;
}
public Short getLeftBorderColor() {
return leftBorderColor;
}
public void setLeftBorderColor(Short leftBorderColor) {
this.leftBorderColor = leftBorderColor;
}
public Short getRightBorderColor() {
return rightBorderColor;
}
public void setRightBorderColor(Short rightBorderColor) {
this.rightBorderColor = rightBorderColor;
}
public Short getTopBorderColor() {
return topBorderColor;
}
public void setTopBorderColor(Short topBorderColor) {
this.topBorderColor = topBorderColor;
}
public Short getBottomBorderColor() {
return bottomBorderColor;
}
public void setBottomBorderColor(Short bottomBorderColor) {
this.bottomBorderColor = bottomBorderColor;
}
public FillPatternType getFillPatternType() {
return fillPatternType;
}
public void setFillPatternType(FillPatternType fillPatternType) {
this.fillPatternType = fillPatternType;
}
public Short getFillBackgroundColor() {
return fillBackgroundColor;
}
public void setFillBackgroundColor(Short fillBackgroundColor) {
this.fillBackgroundColor = fillBackgroundColor;
}
public Short getFillForegroundColor() {
return fillForegroundColor;
}
public void setFillForegroundColor(Short fillForegroundColor) {
this.fillForegroundColor = fillForegroundColor;
}
public Boolean getShrinkToFit() {
return shrinkToFit;
}
public void setShrinkToFit(Boolean shrinkToFit) {
this.shrinkToFit = shrinkToFit;
}
}

143
src/main/java/com/alibaba/excel/write/metadata/style/WriteFont.java

@ -0,0 +1,143 @@
package com.alibaba.excel.write.metadata.style;
import org.apache.poi.common.usermodel.fonts.FontCharset;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
/**
* Font when writing
*
* @author jipengfei
*/
public class WriteFont {
/**
* The name for the font (i.e. Arial)
*/
private String fontName;
/**
* Height in the familiar unit of measure - points
*/
private Short fontHeightInPoints;
/**
* Whether to use italics or not
*/
private Boolean italic;
/**
* Whether to use a strikeout horizontal line through the text or not
*/
private Boolean strikeout;
/**
* The color for the font
*
* @see Font#COLOR_NORMAL
* @see Font#COLOR_RED
* @see HSSFPalette#getColor(short)
* @see IndexedColors
*/
private Short color;
/**
* Set normal,super or subscript.
*
* @see Font#SS_NONE
* @see Font#SS_SUPER
* @see Font#SS_SUB
*/
private Short typeOffset;
/**
* set type of text underlining to use
*
* @see Font#U_NONE
* @see Font#U_SINGLE
* @see Font#U_DOUBLE
* @see Font#U_SINGLE_ACCOUNTING
* @see Font#U_DOUBLE_ACCOUNTING
*/
private Byte underline;
/**
* Set character-set to use.
*
* @see FontCharset
* @see Font#ANSI_CHARSET
* @see Font#DEFAULT_CHARSET
* @see Font#SYMBOL_CHARSET
*/
private Integer charset;
/**
* Bold
*/
private Boolean bold;
public String getFontName() {
return fontName;
}
public void setFontName(String fontName) {
this.fontName = fontName;
}
public Short getFontHeightInPoints() {
return fontHeightInPoints;
}
public void setFontHeightInPoints(Short fontHeightInPoints) {
this.fontHeightInPoints = fontHeightInPoints;
}
public Boolean getItalic() {
return italic;
}
public void setItalic(Boolean italic) {
this.italic = italic;
}
public Boolean getStrikeout() {
return strikeout;
}
public void setStrikeout(Boolean strikeout) {
this.strikeout = strikeout;
}
public Short getColor() {
return color;
}
public void setColor(Short color) {
this.color = color;
}
public Short getTypeOffset() {
return typeOffset;
}
public void setTypeOffset(Short typeOffset) {
this.typeOffset = typeOffset;
}
public Byte getUnderline() {
return underline;
}
public void setUnderline(Byte underline) {
this.underline = underline;
}
public Integer getCharset() {
return charset;
}
public void setCharset(Integer charset) {
this.charset = charset;
}
public Boolean getBold() {
return bold;
}
public void setBold(Boolean bold) {
this.bold = bold;
}
}

15
src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java

@ -7,13 +7,10 @@ import java.util.Map;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.annotation.write.style.HeadStyle;
import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.metadata.CellRange;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.property.CellStyleProperty;
import com.alibaba.excel.metadata.property.ColumnWidthProperty;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.metadata.property.ExcelHeadProperty;
@ -38,29 +35,17 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty {
this.contentRowHeightProperty =
RowHeightProperty.build((ContentRowHeight)headClazz.getAnnotation(ContentRowHeight.class));
HeadStyle parentHeadStyle = (HeadStyle)headClazz.getAnnotation(HeadStyle.class);
ContentStyle parentContentStyle = (ContentStyle)headClazz.getAnnotation(ContentStyle.class);
ColumnWidth parentColumnWidth = (ColumnWidth)headClazz.getAnnotation(ColumnWidth.class);
for (Map.Entry<Integer, ExcelContentProperty> entry : getContentPropertyMap().entrySet()) {
Integer index = entry.getKey();
ExcelContentProperty excelContentPropertyData = entry.getValue();
Field field = excelContentPropertyData.getField();
Head headData = getHeadMap().get(index);
HeadStyle headStyle = field.getAnnotation(HeadStyle.class);
if (headStyle == null) {
headStyle = parentHeadStyle;
}
headData.setCellStyleProperty(CellStyleProperty.build(headStyle));
ColumnWidth columnWidth = field.getAnnotation(ColumnWidth.class);
if (columnWidth == null) {
columnWidth = parentColumnWidth;
}
headData.setColumnWidthProperty(ColumnWidthProperty.build(columnWidth));
ContentStyle contentStyle = field.getAnnotation(ContentStyle.class);
if (contentStyle == null) {
contentStyle = parentContentStyle;
}
excelContentPropertyData.setCellStyleProperty(CellStyleProperty.build(contentStyle));
}
}

13
src/main/java/com/alibaba/excel/write/style/AbstractColumnCellStyleStrategy.java

@ -9,6 +9,7 @@ import org.apache.poi.ss.usermodel.Workbook;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.sun.istack.internal.Nullable;
/**
@ -38,11 +39,11 @@ public abstract class AbstractColumnCellStyleStrategy extends AbstractCellStyleS
}
return;
}
com.alibaba.excel.metadata.CellStyle headCellStyle = headCellStyle(head);
WriteCellStyle headCellStyle = headCellStyle(head);
if (headCellStyle == null) {
headCellStyleCache.put(columnIndex, null);
} else {
CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headCellStyle(head));
CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headCellStyle);
headCellStyleCache.put(columnIndex, cellStyle);
cell.setCellStyle(cellStyle);
}
@ -58,11 +59,11 @@ public abstract class AbstractColumnCellStyleStrategy extends AbstractCellStyleS
}
return;
}
com.alibaba.excel.metadata.CellStyle contentCellStyle = contentCellStyle(head);
WriteCellStyle contentCellStyle = contentCellStyle(head);
if (contentCellStyle == null) {
contentCellStyleCache.put(columnIndex, null);
} else {
CellStyle cellStyle = StyleUtil.buildContentCellStyle(workbook, contentCellStyle(head));
CellStyle cellStyle = StyleUtil.buildContentCellStyle(workbook, contentCellStyle);
contentCellStyleCache.put(columnIndex, cellStyle);
cell.setCellStyle(cellStyle);
}
@ -74,7 +75,7 @@ public abstract class AbstractColumnCellStyleStrategy extends AbstractCellStyleS
* @param head
* @return
*/
protected abstract com.alibaba.excel.metadata.CellStyle headCellStyle(@Nullable Head head);
protected abstract WriteCellStyle headCellStyle(@Nullable Head head);
/**
* Returns the column width corresponding to each column head
@ -82,6 +83,6 @@ public abstract class AbstractColumnCellStyleStrategy extends AbstractCellStyleS
* @param head
* @return
*/
protected abstract com.alibaba.excel.metadata.CellStyle contentCellStyle(@Nullable Head head);
protected abstract WriteCellStyle contentCellStyle(@Nullable Head head);
}

43
src/main/java/com/alibaba/excel/write/style/RowCellStyleStrategy.java

@ -4,11 +4,12 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
import com.alibaba.excel.metadata.CellStyle;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
/**
*
@ -18,50 +19,50 @@ import com.alibaba.excel.util.StyleUtil;
*/
public class RowCellStyleStrategy extends AbstractCellStyleStrategy {
private WriteCellStyle headWriteCellStyle;
private List<WriteCellStyle> contentWriteCellStyleList;
private CellStyle headCellStyle;
private List<CellStyle> contentCellStyleList;
private org.apache.poi.ss.usermodel.CellStyle poiHeadCellStyle;
private List<org.apache.poi.ss.usermodel.CellStyle> poiContentCellStyleList;
public RowCellStyleStrategy(CellStyle headCellStyle, List<CellStyle> contentCellStyleList) {
this.headCellStyle = headCellStyle;
this.contentCellStyleList = contentCellStyleList;
public RowCellStyleStrategy(WriteCellStyle headWriteCellStyle, List<WriteCellStyle> contentWriteCellStyleList) {
this.headWriteCellStyle = headWriteCellStyle;
this.contentWriteCellStyleList = contentWriteCellStyleList;
}
public RowCellStyleStrategy(CellStyle headCellStyle, CellStyle contentCellStyle) {
this.headCellStyle = headCellStyle;
contentCellStyleList = new ArrayList<CellStyle>();
contentCellStyleList.add(contentCellStyle);
public RowCellStyleStrategy(WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) {
this.headWriteCellStyle = headWriteCellStyle;
contentWriteCellStyleList = new ArrayList<WriteCellStyle>();
contentWriteCellStyleList.add(contentWriteCellStyle);
}
@Override
protected void initCellStyle(Workbook workbook) {
if (headCellStyle != null) {
poiHeadCellStyle = StyleUtil.buildHeadCellStyle(workbook, headCellStyle);
if (headWriteCellStyle != null) {
headCellStyle = StyleUtil.buildHeadCellStyle(workbook, headWriteCellStyle);
}
if (contentCellStyleList != null && !contentCellStyleList.isEmpty()) {
poiContentCellStyleList = new ArrayList<org.apache.poi.ss.usermodel.CellStyle>();
for (CellStyle cellStyle : contentCellStyleList) {
poiContentCellStyleList.add(StyleUtil.buildContentCellStyle(workbook, cellStyle));
if (contentWriteCellStyleList != null && !contentWriteCellStyleList.isEmpty()) {
contentCellStyleList = new ArrayList<CellStyle>();
for (WriteCellStyle writeCellStyle : contentWriteCellStyleList) {
contentCellStyleList.add(StyleUtil.buildContentCellStyle(workbook, writeCellStyle));
}
}
}
@Override
protected void setHeadCellStyle(Cell cell, Head head, int relativeRowIndex) {
if (poiHeadCellStyle == null) {
if (headCellStyle == null) {
return;
}
cell.setCellStyle(poiHeadCellStyle);
cell.setCellStyle(headCellStyle);
}
@Override
protected void setContentCellStyle(Cell cell, Head head, int relativeRowIndex) {
if (poiContentCellStyleList == null || poiContentCellStyleList.isEmpty()) {
if (contentCellStyleList == null || contentCellStyleList.isEmpty()) {
return;
}
cell.setCellStyle(poiContentCellStyleList.get(relativeRowIndex % poiContentCellStyleList.size()));
cell.setCellStyle(contentCellStyleList.get(relativeRowIndex % contentCellStyleList.size()));
}
}

Loading…
Cancel
Save