forked from fanruan/easyexcel
Jiaju Zhuang
5 years ago
34 changed files with 1315 additions and 224 deletions
@ -0,0 +1,180 @@
|
||||
package com.alibaba.excel.metadata.property; |
||||
|
||||
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; |
||||
|
||||
import com.alibaba.excel.annotation.write.style.ContentFontStyle; |
||||
import com.alibaba.excel.annotation.write.style.HeadFontStyle; |
||||
|
||||
/** |
||||
* Configuration from annotations |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class FontProperty { |
||||
/** |
||||
* 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 static FontProperty build(HeadFontStyle headFontStyle) { |
||||
if (headFontStyle == null) { |
||||
return null; |
||||
} |
||||
FontProperty styleProperty = new FontProperty(); |
||||
styleProperty.setFontName(headFontStyle.fontName()); |
||||
styleProperty.setFontHeightInPoints(headFontStyle.fontHeightInPoints()); |
||||
styleProperty.setItalic(headFontStyle.italic()); |
||||
styleProperty.setStrikeout(headFontStyle.strikeout()); |
||||
styleProperty.setColor(headFontStyle.color()); |
||||
styleProperty.setTypeOffset(headFontStyle.typeOffset()); |
||||
styleProperty.setUnderline(headFontStyle.underline()); |
||||
styleProperty.setCharset(headFontStyle.charset()); |
||||
styleProperty.setBold(headFontStyle.bold()); |
||||
return styleProperty; |
||||
} |
||||
|
||||
public static FontProperty build(ContentFontStyle contentFontStyle) { |
||||
if (contentFontStyle == null) { |
||||
return null; |
||||
} |
||||
FontProperty styleProperty = new FontProperty(); |
||||
styleProperty.setFontName(contentFontStyle.fontName()); |
||||
styleProperty.setFontHeightInPoints(contentFontStyle.fontHeightInPoints()); |
||||
styleProperty.setItalic(contentFontStyle.italic()); |
||||
styleProperty.setStrikeout(contentFontStyle.strikeout()); |
||||
styleProperty.setColor(contentFontStyle.color()); |
||||
styleProperty.setTypeOffset(contentFontStyle.typeOffset()); |
||||
styleProperty.setUnderline(contentFontStyle.underline()); |
||||
styleProperty.setCharset(contentFontStyle.charset()); |
||||
styleProperty.setBold(contentFontStyle.bold()); |
||||
return styleProperty; |
||||
} |
||||
|
||||
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; |
||||
} |
||||
} |
@ -0,0 +1,47 @@
|
||||
package com.alibaba.excel.metadata.property; |
||||
|
||||
import com.alibaba.excel.annotation.write.style.ContentLoopMerge; |
||||
|
||||
/** |
||||
* Configuration from annotations |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class LoopMergeProperty { |
||||
/** |
||||
* Each row |
||||
*/ |
||||
private int eachRow; |
||||
/** |
||||
* Extend column |
||||
*/ |
||||
private int columnExtend; |
||||
|
||||
public LoopMergeProperty(int eachRow, int columnExtend) { |
||||
this.eachRow = eachRow; |
||||
this.columnExtend = columnExtend; |
||||
} |
||||
|
||||
public static LoopMergeProperty build(ContentLoopMerge contentLoopMerge) { |
||||
if (contentLoopMerge == null) { |
||||
return null; |
||||
} |
||||
return new LoopMergeProperty(contentLoopMerge.eachRow(), contentLoopMerge.columnExtend()); |
||||
} |
||||
|
||||
public int getEachRow() { |
||||
return eachRow; |
||||
} |
||||
|
||||
public void setEachRow(int eachRow) { |
||||
this.eachRow = eachRow; |
||||
} |
||||
|
||||
public int getColumnExtend() { |
||||
return columnExtend; |
||||
} |
||||
|
||||
public void setColumnExtend(int columnExtend) { |
||||
this.columnExtend = columnExtend; |
||||
} |
||||
} |
@ -0,0 +1,74 @@
|
||||
package com.alibaba.excel.metadata.property; |
||||
|
||||
import com.alibaba.excel.annotation.write.style.OnceAbsoluteMerge; |
||||
|
||||
/** |
||||
* Configuration from annotations |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class OnceAbsoluteMergeProperty { |
||||
/** |
||||
* First row |
||||
*/ |
||||
private int firstRowIndex; |
||||
/** |
||||
* Last row |
||||
*/ |
||||
private int lastRowIndex; |
||||
/** |
||||
* First column |
||||
*/ |
||||
private int firstColumnIndex; |
||||
/** |
||||
* Last row |
||||
*/ |
||||
private int lastColumnIndex; |
||||
|
||||
public OnceAbsoluteMergeProperty(int firstRowIndex, int lastRowIndex, int firstColumnIndex, int lastColumnIndex) { |
||||
this.firstRowIndex = firstRowIndex; |
||||
this.lastRowIndex = lastRowIndex; |
||||
this.firstColumnIndex = firstColumnIndex; |
||||
this.lastColumnIndex = lastColumnIndex; |
||||
} |
||||
|
||||
public static OnceAbsoluteMergeProperty build(OnceAbsoluteMerge onceAbsoluteMerge) { |
||||
if (onceAbsoluteMerge == null) { |
||||
return null; |
||||
} |
||||
return new OnceAbsoluteMergeProperty(onceAbsoluteMerge.firstRowIndex(), onceAbsoluteMerge.lastRowIndex(), |
||||
onceAbsoluteMerge.firstColumnIndex(), onceAbsoluteMerge.lastColumnIndex()); |
||||
} |
||||
|
||||
public int getFirstRowIndex() { |
||||
return firstRowIndex; |
||||
} |
||||
|
||||
public void setFirstRowIndex(int firstRowIndex) { |
||||
this.firstRowIndex = firstRowIndex; |
||||
} |
||||
|
||||
public int getLastRowIndex() { |
||||
return lastRowIndex; |
||||
} |
||||
|
||||
public void setLastRowIndex(int lastRowIndex) { |
||||
this.lastRowIndex = lastRowIndex; |
||||
} |
||||
|
||||
public int getFirstColumnIndex() { |
||||
return firstColumnIndex; |
||||
} |
||||
|
||||
public void setFirstColumnIndex(int firstColumnIndex) { |
||||
this.firstColumnIndex = firstColumnIndex; |
||||
} |
||||
|
||||
public int getLastColumnIndex() { |
||||
return lastColumnIndex; |
||||
} |
||||
|
||||
public void setLastColumnIndex(int lastColumnIndex) { |
||||
this.lastColumnIndex = lastColumnIndex; |
||||
} |
||||
} |
@ -0,0 +1,377 @@
|
||||
package com.alibaba.excel.metadata.property; |
||||
|
||||
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; |
||||
|
||||
import com.alibaba.excel.annotation.write.style.ContentStyle; |
||||
import com.alibaba.excel.annotation.write.style.HeadStyle; |
||||
import com.alibaba.excel.write.metadata.style.WriteFont; |
||||
|
||||
/** |
||||
* Configuration from annotations |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class StyleProperty { |
||||
/** |
||||
* 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 static StyleProperty build(HeadStyle headStyle) { |
||||
if (headStyle == null) { |
||||
return null; |
||||
} |
||||
StyleProperty styleProperty = new StyleProperty(); |
||||
styleProperty.setDataFormat(headStyle.dataFormat()); |
||||
styleProperty.setHidden(headStyle.hidden()); |
||||
styleProperty.setLocked(headStyle.locked()); |
||||
styleProperty.setQuotePrefix(headStyle.quotePrefix()); |
||||
styleProperty.setHorizontalAlignment(headStyle.horizontalAlignment()); |
||||
styleProperty.setWrapped(headStyle.wrapped()); |
||||
styleProperty.setVerticalAlignment(headStyle.verticalAlignment()); |
||||
styleProperty.setRotation(headStyle.rotation()); |
||||
styleProperty.setIndent(headStyle.indent()); |
||||
styleProperty.setBorderLeft(headStyle.borderLeft()); |
||||
styleProperty.setBorderRight(headStyle.borderRight()); |
||||
styleProperty.setBorderTop(headStyle.borderTop()); |
||||
styleProperty.setBorderBottom(headStyle.borderBottom()); |
||||
styleProperty.setLeftBorderColor(headStyle.leftBorderColor()); |
||||
styleProperty.setRightBorderColor(headStyle.rightBorderColor()); |
||||
styleProperty.setTopBorderColor(headStyle.topBorderColor()); |
||||
styleProperty.setBottomBorderColor(headStyle.bottomBorderColor()); |
||||
styleProperty.setFillPatternType(headStyle.fillPatternType()); |
||||
styleProperty.setFillBackgroundColor(headStyle.fillBackgroundColor()); |
||||
styleProperty.setFillForegroundColor(headStyle.fillForegroundColor()); |
||||
styleProperty.setShrinkToFit(headStyle.shrinkToFit()); |
||||
return styleProperty; |
||||
} |
||||
|
||||
public static StyleProperty build(ContentStyle contentStyle) { |
||||
if (contentStyle == null) { |
||||
return null; |
||||
} |
||||
StyleProperty styleProperty = new StyleProperty(); |
||||
styleProperty.setDataFormat(contentStyle.dataFormat()); |
||||
styleProperty.setHidden(contentStyle.hidden()); |
||||
styleProperty.setLocked(contentStyle.locked()); |
||||
styleProperty.setQuotePrefix(contentStyle.quotePrefix()); |
||||
styleProperty.setHorizontalAlignment(contentStyle.horizontalAlignment()); |
||||
styleProperty.setWrapped(contentStyle.wrapped()); |
||||
styleProperty.setVerticalAlignment(contentStyle.verticalAlignment()); |
||||
styleProperty.setRotation(contentStyle.rotation()); |
||||
styleProperty.setIndent(contentStyle.indent()); |
||||
styleProperty.setBorderLeft(contentStyle.borderLeft()); |
||||
styleProperty.setBorderRight(contentStyle.borderRight()); |
||||
styleProperty.setBorderTop(contentStyle.borderTop()); |
||||
styleProperty.setBorderBottom(contentStyle.borderBottom()); |
||||
styleProperty.setLeftBorderColor(contentStyle.leftBorderColor()); |
||||
styleProperty.setRightBorderColor(contentStyle.rightBorderColor()); |
||||
styleProperty.setTopBorderColor(contentStyle.topBorderColor()); |
||||
styleProperty.setBottomBorderColor(contentStyle.bottomBorderColor()); |
||||
styleProperty.setFillPatternType(contentStyle.fillPatternType()); |
||||
styleProperty.setFillBackgroundColor(contentStyle.fillBackgroundColor()); |
||||
styleProperty.setFillForegroundColor(contentStyle.fillForegroundColor()); |
||||
styleProperty.setShrinkToFit(contentStyle.shrinkToFit()); |
||||
return styleProperty; |
||||
} |
||||
|
||||
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; |
||||
} |
||||
} |
@ -1,77 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.exception.ExcelDataConvertException; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* 读取单元格的批注 |
||||
* |
||||
* @author: kaiux |
||||
* @date: 2019-10-23 14:10 |
||||
**/ |
||||
public class DemoCellCommentsListener extends AnalysisEventListener<DemoData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DemoCellCommentsListener.class); |
||||
/** |
||||
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 |
||||
*/ |
||||
private static final int BATCH_COUNT = 5; |
||||
List<DemoData> list = new ArrayList<DemoData>(); |
||||
|
||||
/** |
||||
* 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。 |
||||
* |
||||
* @param exception |
||||
* @param context |
||||
* @throws Exception |
||||
*/ |
||||
@Override |
||||
public void onException(Exception exception, AnalysisContext context) { |
||||
LOGGER.error("解析失败,但是继续解析下一行:{}", exception.getMessage()); |
||||
if (exception instanceof ExcelDataConvertException) { |
||||
ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception; |
||||
LOGGER.error("第{}行,第{}列解析异常", excelDataConvertException.getRowIndex(), |
||||
excelDataConvertException.getColumnIndex()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 这里会一行行的返回头 |
||||
* |
||||
* @param headMap |
||||
* @param context |
||||
*/ |
||||
@Override |
||||
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {} |
||||
|
||||
@Override |
||||
public void invoke(DemoData data, AnalysisContext context) { |
||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); |
||||
if (list.size() >= BATCH_COUNT) { |
||||
saveData(); |
||||
list.clear(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
saveData(); |
||||
LOGGER.info("所有数据解析完成!"); |
||||
} |
||||
|
||||
/** |
||||
* 加上存储数据库 |
||||
*/ |
||||
private void saveData() { |
||||
LOGGER.info("{}条数据,开始存储数据库!", list.size()); |
||||
LOGGER.info("存储数据库成功!"); |
||||
} |
||||
} |
@ -0,0 +1,14 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Data |
||||
public class DemoExtraData { |
||||
|
||||
private String row1; |
||||
|
||||
private String row2; |
||||
} |
@ -0,0 +1,58 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import org.junit.Assert; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.metadata.CellExtra; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* 读取单元格的批注 |
||||
* |
||||
* @author: kaiux |
||||
* @date: 2019-10-23 14:10 |
||||
**/ |
||||
public class DemoExtraListener extends AnalysisEventListener<DemoExtraData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class); |
||||
|
||||
@Override |
||||
public void invoke(DemoExtraData data, AnalysisContext context) {} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) {} |
||||
|
||||
@Override |
||||
public void extra(CellExtra extra, AnalysisContext context) { |
||||
LOGGER.info("读取到了一条额外信息:{}", JSON.toJSONString(extra)); |
||||
switch (extra.getType()) { |
||||
case COMMENT: |
||||
LOGGER.info("额外信息是批注,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(), extra.getColumnIndex(), |
||||
extra.getText()); |
||||
break; |
||||
case HYPERLINK: |
||||
if ("Sheet1!A1".equals(extra.getText())) { |
||||
LOGGER.info("额外信息是超链接,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(), |
||||
extra.getColumnIndex(), extra.getText()); |
||||
} else if ("Sheet2!A1".equals(extra.getText())) { |
||||
LOGGER.info( |
||||
"额外信息是超链接,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}," |
||||
+ "内容是:{}", |
||||
extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(), |
||||
extra.getLastColumnIndex(), extra.getText()); |
||||
} else { |
||||
Assert.fail("Unknown hyperlink!"); |
||||
} |
||||
break; |
||||
case MERGE: |
||||
LOGGER.info( |
||||
"额外信息是超链接,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}", |
||||
extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(), |
||||
extra.getLastColumnIndex()); |
||||
break; |
||||
default: |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,27 @@
|
||||
package com.alibaba.easyexcel.test.demo.write; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.annotation.write.style.ContentLoopMerge; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 样式的数据类 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
// 将第6-7行的2-3列合并成一个单元格
|
||||
// @OnceAbsoluteMerge(firstRowIndex = 5, lastRowIndex = 6, firstColumnIndex = 1, lastColumnIndex = 2)
|
||||
public class DemoMergeData { |
||||
// 这一列 每隔2行 合并单元格
|
||||
@ContentLoopMerge(eachRow = 2) |
||||
@ExcelProperty("字符串标题") |
||||
private String string; |
||||
@ExcelProperty("日期标题") |
||||
private Date date; |
||||
@ExcelProperty("数字标题") |
||||
private Double doubleData; |
||||
} |
@ -0,0 +1,44 @@
|
||||
package com.alibaba.easyexcel.test.demo.write; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import org.apache.poi.ss.usermodel.FillPatternType; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.annotation.write.style.ContentFontStyle; |
||||
import com.alibaba.excel.annotation.write.style.ContentStyle; |
||||
import com.alibaba.excel.annotation.write.style.HeadFontStyle; |
||||
import com.alibaba.excel.annotation.write.style.HeadStyle; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 样式的数据类 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
// 头背景设置成红色 IndexedColors.RED.getIndex()
|
||||
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 10) |
||||
// 头字体设置成20
|
||||
@HeadFontStyle(fontHeightInPoints = 20) |
||||
// 内容的背景设置成绿色 IndexedColors.GREEN.getIndex()
|
||||
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 17) |
||||
// 内容字体设置成20
|
||||
@ContentFontStyle(fontHeightInPoints = 20) |
||||
public class DemoStyleData { |
||||
// 字符串的头背景设置成粉红 IndexedColors.PINK.getIndex()
|
||||
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 14) |
||||
// 字符串的头字体设置成20
|
||||
@HeadFontStyle(fontHeightInPoints = 30) |
||||
// 字符串的内容的背景设置成天蓝 IndexedColors.SKY_BLUE.getIndex()
|
||||
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 40) |
||||
// 字符串的内容字体设置成20
|
||||
@ContentFontStyle(fontHeightInPoints = 30) |
||||
@ExcelProperty("字符串标题") |
||||
private String string; |
||||
@ExcelProperty("日期标题") |
||||
private Date date; |
||||
@ExcelProperty("数字标题") |
||||
private Double doubleData; |
||||
} |
Binary file not shown.
Loading…
Reference in new issue