mirror of https://github.com/alibaba/easyexcel
Jiaju Zhuang
3 years ago
committed by
GitHub
36 changed files with 1198 additions and 454 deletions
@ -0,0 +1,33 @@
|
||||
package com.alibaba.excel.enums; |
||||
|
||||
import lombok.Getter; |
||||
|
||||
/** |
||||
* Default values cannot be used for annotations. |
||||
* So an additional an enumeration to determine whether the user has added the enumeration. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Getter |
||||
public enum BooleanEnum { |
||||
/** |
||||
* NULL |
||||
*/ |
||||
DEFAULT(null), |
||||
/** |
||||
* TRUE |
||||
*/ |
||||
TRUE(Boolean.TRUE), |
||||
/** |
||||
* FALSE |
||||
*/ |
||||
FALSE(Boolean.FALSE), |
||||
; |
||||
|
||||
Boolean booleanValue; |
||||
|
||||
BooleanEnum(Boolean booleanValue) { |
||||
this.booleanValue = booleanValue; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,95 @@
|
||||
package com.alibaba.excel.enums.poi; |
||||
|
||||
import lombok.Getter; |
||||
import org.apache.poi.ss.usermodel.BorderStyle; |
||||
|
||||
/** |
||||
* The enumeration value indicating the line style of a border in a cell, |
||||
* i.e., whether it is bordered dash dot, dash dot dot, dashed, dotted, double, hair, medium, |
||||
* medium dash dot, medium dash dot dot, medium dashed, none, slant dash dot, thick or thin. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Getter |
||||
public enum BorderStyleEnum { |
||||
/** |
||||
* null |
||||
*/ |
||||
DEFAULT(null), |
||||
|
||||
/** |
||||
* No border (default) |
||||
*/ |
||||
NONE(BorderStyle.NONE), |
||||
|
||||
/** |
||||
* Thin border |
||||
*/ |
||||
THIN(BorderStyle.THIN), |
||||
|
||||
/** |
||||
* Medium border |
||||
*/ |
||||
MEDIUM(BorderStyle.MEDIUM), |
||||
|
||||
/** |
||||
* dash border |
||||
*/ |
||||
DASHED(BorderStyle.DASHED), |
||||
|
||||
/** |
||||
* dot border |
||||
*/ |
||||
DOTTED(BorderStyle.DOTTED), |
||||
|
||||
/** |
||||
* Thick border |
||||
*/ |
||||
THICK(BorderStyle.THICK), |
||||
|
||||
/** |
||||
* double-line border |
||||
*/ |
||||
DOUBLE(BorderStyle.DOUBLE), |
||||
|
||||
/** |
||||
* hair-line border |
||||
*/ |
||||
HAIR(BorderStyle.HAIR), |
||||
|
||||
/** |
||||
* Medium dashed border |
||||
*/ |
||||
MEDIUM_DASHED(BorderStyle.MEDIUM_DASHED), |
||||
|
||||
/** |
||||
* dash-dot border |
||||
*/ |
||||
DASH_DOT(BorderStyle.DASH_DOT), |
||||
|
||||
/** |
||||
* medium dash-dot border |
||||
*/ |
||||
MEDIUM_DASH_DOT(BorderStyle.MEDIUM_DASH_DOT), |
||||
|
||||
/** |
||||
* dash-dot-dot border |
||||
*/ |
||||
DASH_DOT_DOT(BorderStyle.DASH_DOT_DOT), |
||||
|
||||
/** |
||||
* medium dash-dot-dot border |
||||
*/ |
||||
MEDIUM_DASH_DOT_DOT(BorderStyle.MEDIUM_DASH_DOT_DOT), |
||||
|
||||
/** |
||||
* slanted dash-dot border |
||||
*/ |
||||
SLANTED_DASH_DOT(BorderStyle.SLANTED_DASH_DOT); |
||||
|
||||
BorderStyle poiBorderStyle; |
||||
|
||||
BorderStyleEnum(BorderStyle poiBorderStyle) { |
||||
this.poiBorderStyle = poiBorderStyle; |
||||
} |
||||
} |
@ -0,0 +1,119 @@
|
||||
package com.alibaba.excel.enums.poi; |
||||
|
||||
import lombok.Getter; |
||||
import org.apache.poi.ss.usermodel.FillPatternType; |
||||
|
||||
/** |
||||
* The enumeration value indicating the style of fill pattern being used for a cell format. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Getter |
||||
public enum FillPatternTypeEnum { |
||||
|
||||
/** |
||||
* null |
||||
*/ |
||||
DEFAULT(null), |
||||
|
||||
/** |
||||
* No background |
||||
*/ |
||||
NO_FILL(FillPatternType.NO_FILL), |
||||
|
||||
/** |
||||
* Solidly filled |
||||
*/ |
||||
SOLID_FOREGROUND(FillPatternType.SOLID_FOREGROUND), |
||||
|
||||
/** |
||||
* Small fine dots |
||||
*/ |
||||
FINE_DOTS(FillPatternType.FINE_DOTS), |
||||
|
||||
/** |
||||
* Wide dots |
||||
*/ |
||||
ALT_BARS(FillPatternType.ALT_BARS), |
||||
|
||||
/** |
||||
* Sparse dots |
||||
*/ |
||||
SPARSE_DOTS(FillPatternType.SPARSE_DOTS), |
||||
|
||||
/** |
||||
* Thick horizontal bands |
||||
*/ |
||||
THICK_HORZ_BANDS(FillPatternType.THICK_HORZ_BANDS), |
||||
|
||||
/** |
||||
* Thick vertical bands |
||||
*/ |
||||
THICK_VERT_BANDS(FillPatternType.THICK_VERT_BANDS), |
||||
|
||||
/** |
||||
* Thick backward facing diagonals |
||||
*/ |
||||
THICK_BACKWARD_DIAG(FillPatternType.THICK_BACKWARD_DIAG), |
||||
|
||||
/** |
||||
* Thick forward facing diagonals |
||||
*/ |
||||
THICK_FORWARD_DIAG(FillPatternType.THICK_FORWARD_DIAG), |
||||
|
||||
/** |
||||
* Large spots |
||||
*/ |
||||
BIG_SPOTS(FillPatternType.BIG_SPOTS), |
||||
|
||||
/** |
||||
* Brick-like layout |
||||
*/ |
||||
BRICKS(FillPatternType.BRICKS), |
||||
|
||||
/** |
||||
* Thin horizontal bands |
||||
*/ |
||||
THIN_HORZ_BANDS(FillPatternType.THIN_HORZ_BANDS), |
||||
|
||||
/** |
||||
* Thin vertical bands |
||||
*/ |
||||
THIN_VERT_BANDS(FillPatternType.THIN_VERT_BANDS), |
||||
|
||||
/** |
||||
* Thin backward diagonal |
||||
*/ |
||||
THIN_BACKWARD_DIAG(FillPatternType.THIN_BACKWARD_DIAG), |
||||
|
||||
/** |
||||
* Thin forward diagonal |
||||
*/ |
||||
THIN_FORWARD_DIAG(FillPatternType.THIN_FORWARD_DIAG), |
||||
|
||||
/** |
||||
* Squares |
||||
*/ |
||||
SQUARES(FillPatternType.SQUARES), |
||||
|
||||
/** |
||||
* Diamonds |
||||
*/ |
||||
DIAMONDS(FillPatternType.DIAMONDS), |
||||
|
||||
/** |
||||
* Less Dots |
||||
*/ |
||||
LESS_DOTS(FillPatternType.LESS_DOTS), |
||||
|
||||
/** |
||||
* Least Dots |
||||
*/ |
||||
LEAST_DOTS(FillPatternType.LEAST_DOTS); |
||||
|
||||
FillPatternType poiFillPatternType; |
||||
|
||||
FillPatternTypeEnum(FillPatternType poiFillPatternType) { |
||||
this.poiFillPatternType = poiFillPatternType; |
||||
} |
||||
} |
@ -0,0 +1,94 @@
|
||||
package com.alibaba.excel.enums.poi; |
||||
|
||||
import lombok.Getter; |
||||
import org.apache.poi.ss.usermodel.HorizontalAlignment; |
||||
|
||||
/** |
||||
* The enumeration value indicating horizontal alignment of a cell, |
||||
* i.e., whether it is aligned general, left, right, horizontally centered, filled (replicated), |
||||
* justified, centered across multiple cells, or distributed. |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Getter |
||||
public enum HorizontalAlignmentEnum { |
||||
/** |
||||
* null |
||||
*/ |
||||
DEFAULT(null), |
||||
/** |
||||
* The horizontal alignment is general-aligned. Text data is left-aligned. |
||||
* Numbers, dates, and times are rightaligned. Boolean types are centered. |
||||
* Changing the alignment does not change the type of data. |
||||
*/ |
||||
GENERAL(HorizontalAlignment.GENERAL), |
||||
|
||||
/** |
||||
* The horizontal alignment is left-aligned, even in Rightto-Left mode. |
||||
* Aligns contents at the left edge of the cell. If an indent amount is specified, the contents of |
||||
* the cell is indented from the left by the specified number of character spaces. The character spaces are |
||||
* based on the default font and font size for the workbook. |
||||
*/ |
||||
LEFT(HorizontalAlignment.LEFT), |
||||
|
||||
/** |
||||
* The horizontal alignment is centered, meaning the text is centered across the cell. |
||||
*/ |
||||
CENTER(HorizontalAlignment.CENTER), |
||||
|
||||
/** |
||||
* The horizontal alignment is right-aligned, meaning that cell contents are aligned at the right edge of the cell, |
||||
* even in Right-to-Left mode. |
||||
*/ |
||||
RIGHT(HorizontalAlignment.RIGHT), |
||||
|
||||
/** |
||||
* Indicates that the value of the cell should be filled |
||||
* across the entire width of the cell. If blank cells to the right also have the fill alignment, |
||||
* they are also filled with the value, using a convention similar to centerContinuous. |
||||
* <p> |
||||
* Additional rules: |
||||
* <ol> |
||||
* <li>Only whole values can be appended, not partial values.</li> |
||||
* <li>The column will not be widened to 'best fit' the filled value</li> |
||||
* <li>If appending an additional occurrence of the value exceeds the boundary of the cell |
||||
* left/right edge, don't append the additional occurrence of the value.</li> |
||||
* <li>The display value of the cell is filled, not the underlying raw number.</li> |
||||
* </ol> |
||||
* </p> |
||||
*/ |
||||
FILL(HorizontalAlignment.FILL), |
||||
|
||||
/** |
||||
* The horizontal alignment is justified (flush left and right). |
||||
* For each line of text, aligns each line of the wrapped text in a cell to the right and left |
||||
* (except the last line). If no single line of text wraps in the cell, then the text is not justified. |
||||
*/ |
||||
JUSTIFY(HorizontalAlignment.JUSTIFY), |
||||
|
||||
/** |
||||
* The horizontal alignment is centered across multiple cells. |
||||
* The information about how many cells to span is expressed in the Sheet Part, |
||||
* in the row of the cell in question. For each cell that is spanned in the alignment, |
||||
* a cell element needs to be written out, with the same style Id which references the centerContinuous alignment. |
||||
*/ |
||||
CENTER_SELECTION(HorizontalAlignment.CENTER_SELECTION), |
||||
|
||||
/** |
||||
* Indicates that each 'word' in each line of text inside the cell is evenly distributed |
||||
* across the width of the cell, with flush right and left margins. |
||||
* <p> |
||||
* When there is also an indent value to apply, both the left and right side of the cell |
||||
* are padded by the indent value. |
||||
* </p> |
||||
* <p> A 'word' is a set of characters with no space character in them. </p> |
||||
* <p> Two lines inside a cell are separated by a carriage return. </p> |
||||
*/ |
||||
DISTRIBUTED(HorizontalAlignment.DISTRIBUTED); |
||||
|
||||
HorizontalAlignment poiHorizontalAlignment; |
||||
|
||||
HorizontalAlignmentEnum(HorizontalAlignment poiHorizontalAlignment) { |
||||
this.poiHorizontalAlignment = poiHorizontalAlignment; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,71 @@
|
||||
package com.alibaba.excel.enums.poi; |
||||
|
||||
import lombok.Getter; |
||||
import org.apache.poi.ss.usermodel.VerticalAlignment; |
||||
|
||||
/** |
||||
* This enumeration value indicates the type of vertical alignment for a cell, i.e., |
||||
* whether it is aligned top, bottom, vertically centered, justified or distributed. |
||||
* |
||||
* <!-- FIXME: Identical to {@link org.apache.poi.ss.usermodel.VerticalAlignment}. Should merge these to |
||||
* {@link org.apache.poi.common.usermodel}.VerticalAlignment in the future. --> |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Getter |
||||
public enum VerticalAlignmentEnum { |
||||
/** |
||||
* null |
||||
*/ |
||||
DEFAULT(null), |
||||
/** |
||||
* The vertical alignment is aligned-to-top. |
||||
*/ |
||||
TOP(VerticalAlignment.TOP), |
||||
|
||||
/** |
||||
* The vertical alignment is centered across the height of the cell. |
||||
*/ |
||||
CENTER(VerticalAlignment.CENTER), |
||||
|
||||
/** |
||||
* The vertical alignment is aligned-to-bottom. (typically the default value) |
||||
*/ |
||||
BOTTOM(VerticalAlignment.BOTTOM), |
||||
|
||||
/** |
||||
* <p> |
||||
* When text direction is horizontal: the vertical alignment of lines of text is distributed vertically, |
||||
* where each line of text inside the cell is evenly distributed across the height of the cell, |
||||
* with flush top and bottom margins. |
||||
* </p> |
||||
* <p> |
||||
* When text direction is vertical: similar behavior as horizontal justification. |
||||
* The alignment is justified (flush top and bottom in this case). For each line of text, each |
||||
* line of the wrapped text in a cell is aligned to the top and bottom (except the last line). |
||||
* If no single line of text wraps in the cell, then the text is not justified. |
||||
* </p> |
||||
*/ |
||||
JUSTIFY(VerticalAlignment.JUSTIFY), |
||||
|
||||
/** |
||||
* <p> |
||||
* When text direction is horizontal: the vertical alignment of lines of text is distributed vertically, |
||||
* where each line of text inside the cell is evenly distributed across the height of the cell, |
||||
* with flush top |
||||
* </p> |
||||
* <p> |
||||
* When text direction is vertical: behaves exactly as distributed horizontal alignment. |
||||
* The first words in a line of text (appearing at the top of the cell) are flush |
||||
* with the top edge of the cell, and the last words of a line of text are flush with the bottom edge of the cell, |
||||
* and the line of text is distributed evenly from top to bottom. |
||||
* </p> |
||||
*/ |
||||
DISTRIBUTED(VerticalAlignment.DISTRIBUTED); |
||||
|
||||
VerticalAlignment poiVerticalAlignmentEnum; |
||||
|
||||
VerticalAlignmentEnum(VerticalAlignment poiVerticalAlignmentEnum) { |
||||
this.poiVerticalAlignmentEnum = poiVerticalAlignmentEnum; |
||||
} |
||||
} |
@ -0,0 +1,46 @@
|
||||
package com.alibaba.excel.write.style; |
||||
|
||||
import com.alibaba.excel.constant.OrderConstant; |
||||
import com.alibaba.excel.write.metadata.style.WriteCellStyle; |
||||
import com.alibaba.excel.write.metadata.style.WriteFont; |
||||
|
||||
import org.apache.poi.ss.usermodel.BorderStyle; |
||||
import org.apache.poi.ss.usermodel.FillPatternType; |
||||
import org.apache.poi.ss.usermodel.HorizontalAlignment; |
||||
import org.apache.poi.ss.usermodel.IndexedColors; |
||||
import org.apache.poi.ss.usermodel.VerticalAlignment; |
||||
|
||||
/** |
||||
* The default styles |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class DefaultStyle extends HorizontalCellStyleStrategy { |
||||
|
||||
@Override |
||||
public int order() { |
||||
return OrderConstant.DEFAULT_DEFINE_STYLE; |
||||
} |
||||
|
||||
public DefaultStyle() { |
||||
super(); |
||||
WriteCellStyle headWriteCellStyle = new WriteCellStyle(); |
||||
headWriteCellStyle.setWrapped(true); |
||||
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
||||
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); |
||||
headWriteCellStyle.setLocked(true); |
||||
headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); |
||||
headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); |
||||
headWriteCellStyle.setBorderTop(BorderStyle.THIN); |
||||
headWriteCellStyle.setBorderBottom(BorderStyle.THIN); |
||||
headWriteCellStyle.setBorderLeft(BorderStyle.THIN); |
||||
headWriteCellStyle.setBorderRight(BorderStyle.THIN); |
||||
WriteFont headWriteFont = new WriteFont(); |
||||
headWriteFont.setFontName("宋体"); |
||||
headWriteFont.setFontHeightInPoints((short)14); |
||||
headWriteFont.setBold(true); |
||||
headWriteCellStyle.setWriteFont(headWriteFont); |
||||
|
||||
setHeadWriteCellStyle(headWriteCellStyle); |
||||
} |
||||
} |
@ -1,39 +1,31 @@
|
||||
package com.alibaba.easyexcel.test.demo.write; |
||||
|
||||
import com.alibaba.excel.write.handler.SheetWriteHandler; |
||||
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.poi.ss.usermodel.DataValidation; |
||||
import org.apache.poi.ss.usermodel.DataValidationConstraint; |
||||
import org.apache.poi.ss.usermodel.DataValidationHelper; |
||||
import org.apache.poi.ss.util.CellRangeAddressList; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.write.handler.SheetWriteHandler; |
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
||||
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; |
||||
|
||||
/** |
||||
* 自定义拦截器.对第一列第一行和第二行的数据新增下拉框,显示 测试1 测试2 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Slf4j |
||||
public class CustomSheetWriteHandler implements SheetWriteHandler { |
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomSheetWriteHandler.class); |
||||
|
||||
@Override |
||||
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { |
||||
LOGGER.info("第{}个Sheet写入成功。", writeSheetHolder.getSheetNo()); |
||||
public void afterSheetCreate(SheetWriteHandlerContext context) { |
||||
log.info("第{}个Sheet写入成功。", context.getWriteSheetHolder().getSheetNo()); |
||||
|
||||
// 区间设置 第一列第一行和第二行的数据。由于第一行是头,所以第一、二行的数据实际上是第二三行
|
||||
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 2, 0, 0); |
||||
DataValidationHelper helper = writeSheetHolder.getSheet().getDataValidationHelper(); |
||||
DataValidationHelper helper = context.getWriteSheetHolder().getSheet().getDataValidationHelper(); |
||||
DataValidationConstraint constraint = helper.createExplicitListConstraint(new String[] {"测试1", "测试2"}); |
||||
DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList); |
||||
writeSheetHolder.getSheet().addValidationData(dataValidation); |
||||
context.getWriteSheetHolder().getSheet().addValidationData(dataValidation); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue