forked from fanruan/easyexcel
zhuangjiaju
5 years ago
38 changed files with 1275 additions and 275 deletions
@ -0,0 +1,21 @@
|
||||
package com.alibaba.excel.enums; |
||||
|
||||
/** |
||||
* The types of head |
||||
* |
||||
* @author zhuangjiaju |
||||
**/ |
||||
public enum HeadKindEnum { |
||||
/** |
||||
* none |
||||
*/ |
||||
NONE, |
||||
/** |
||||
* class
|
||||
*/ |
||||
CLASS, |
||||
/** |
||||
* String |
||||
*/ |
||||
STRING; |
||||
} |
@ -0,0 +1,8 @@
|
||||
package com.alibaba.excel.event; |
||||
|
||||
/** |
||||
* intercepts handle some business logic |
||||
* |
||||
* @author zhuangjiaju |
||||
**/ |
||||
public interface Handler {} |
@ -0,0 +1,36 @@
|
||||
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 simpleFont; |
||||
|
||||
public IndexedColors getIndexedColors() { |
||||
return indexedColors; |
||||
} |
||||
|
||||
public void setIndexedColors(IndexedColors indexedColors) { |
||||
this.indexedColors = indexedColors; |
||||
} |
||||
|
||||
public Font getSimpleFont() { |
||||
return simpleFont; |
||||
} |
||||
|
||||
public void setSimpleFont(Font simpleFont) { |
||||
this.simpleFont = simpleFont; |
||||
} |
||||
} |
@ -0,0 +1,73 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* excel head |
||||
* |
||||
* @author zhuangjiaju |
||||
**/ |
||||
public class Head { |
||||
private Integer columnIndex; |
||||
private String fieldName; |
||||
private List<String> headNames; |
||||
|
||||
public Head(Integer columnIndex, String fieldName, String headName) { |
||||
this.columnIndex = columnIndex; |
||||
this.fieldName = fieldName; |
||||
List<String> headNamesTmp = new ArrayList<String>(); |
||||
headNamesTmp.add(headName); |
||||
this.headNames = headNamesTmp; |
||||
} |
||||
|
||||
public Head(Integer columnIndex, String fieldName, List<String> headNames) { |
||||
this.columnIndex = columnIndex; |
||||
this.fieldName = fieldName; |
||||
if (headNames == null) { |
||||
headNames = new ArrayList<String>(); |
||||
} |
||||
this.headNames = headNames; |
||||
} |
||||
|
||||
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> getHeadNames() { |
||||
return headNames; |
||||
} |
||||
|
||||
public void setHeadNames(List<String> headNames) { |
||||
this.headNames = headNames; |
||||
} |
||||
|
||||
/** |
||||
* Get head name with index |
||||
* |
||||
* @param index |
||||
* @return |
||||
*/ |
||||
public String getHeadName(int index) { |
||||
if (headNames == null || headNames.isEmpty()) { |
||||
return null; |
||||
} |
||||
if (index >= headNames.size()) { |
||||
return headNames.get(headNames.size() - 1); |
||||
} else { |
||||
return headNames.get(index); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,104 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
|
||||
import com.alibaba.excel.write.style.CellStyleStrategy; |
||||
import com.alibaba.excel.write.style.column.ColumnWidthStyleStrategy; |
||||
import com.alibaba.excel.write.style.row.RowHighStyleStrategy; |
||||
|
||||
/** |
||||
* sheet holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SheetHolder { |
||||
/*** |
||||
* poi sheet |
||||
*/ |
||||
private Sheet sheet; |
||||
/*** |
||||
* has been initialized table |
||||
*/ |
||||
private Map<Integer, TableHolder> hasBeenInitializedTable; |
||||
/** |
||||
* the header attribute of excel |
||||
*/ |
||||
private ExcelHeadProperty excelHeadProperty; |
||||
|
||||
private CellStyleStrategy cellStyleStrategy; |
||||
|
||||
private ColumnWidthStyleStrategy columnWidthStyleStrategy; |
||||
private RowHighStyleStrategy rowHighStyleStrategy; |
||||
/** |
||||
* current param |
||||
*/ |
||||
private com.alibaba.excel.metadata.Sheet currentSheetParam; |
||||
|
||||
private boolean needHead = true; |
||||
|
||||
public com.alibaba.excel.metadata.Sheet getCurrentSheetParam() { |
||||
return currentSheetParam; |
||||
} |
||||
|
||||
public void setCurrentSheetParam(com.alibaba.excel.metadata.Sheet currentSheetParam) { |
||||
this.currentSheetParam = currentSheetParam; |
||||
} |
||||
|
||||
public RowHighStyleStrategy getRowHighStyleStrategy() { |
||||
return rowHighStyleStrategy; |
||||
} |
||||
|
||||
public void setRowHighStyleStrategy(RowHighStyleStrategy rowHighStyleStrategy) { |
||||
this.rowHighStyleStrategy = rowHighStyleStrategy; |
||||
} |
||||
|
||||
public ColumnWidthStyleStrategy getColumnWidthStyleStrategy() { |
||||
return columnWidthStyleStrategy; |
||||
} |
||||
|
||||
public void setColumnWidthStyleStrategy(ColumnWidthStyleStrategy columnWidthStyleStrategy) { |
||||
this.columnWidthStyleStrategy = columnWidthStyleStrategy; |
||||
} |
||||
|
||||
public boolean isNeedHead() { |
||||
return needHead; |
||||
} |
||||
|
||||
public void setNeedHead(boolean needHead) { |
||||
this.needHead = needHead; |
||||
} |
||||
|
||||
public ExcelHeadProperty getExcelHeadProperty() { |
||||
return excelHeadProperty; |
||||
} |
||||
|
||||
public void setExcelHeadProperty(ExcelHeadProperty excelHeadProperty) { |
||||
this.excelHeadProperty = excelHeadProperty; |
||||
} |
||||
|
||||
public Sheet getSheet() { |
||||
return sheet; |
||||
} |
||||
|
||||
public void setSheet(Sheet sheet) { |
||||
this.sheet = sheet; |
||||
} |
||||
|
||||
public Map<Integer, TableHolder> getHasBeenInitializedTable() { |
||||
return hasBeenInitializedTable; |
||||
} |
||||
|
||||
public void setHasBeenInitializedTable(Map<Integer, TableHolder> hasBeenInitializedTable) { |
||||
this.hasBeenInitializedTable = hasBeenInitializedTable; |
||||
} |
||||
|
||||
public CellStyleStrategy getCellStyleStrategy() { |
||||
return cellStyleStrategy; |
||||
} |
||||
|
||||
public void setCellStyleStrategy(CellStyleStrategy cellStyleStrategy) { |
||||
this.cellStyleStrategy = cellStyleStrategy; |
||||
} |
||||
} |
@ -0,0 +1,89 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
import com.alibaba.excel.write.style.CellStyleStrategy; |
||||
import com.alibaba.excel.write.style.column.ColumnWidthStyleStrategy; |
||||
import com.alibaba.excel.write.style.row.RowHighStyleStrategy; |
||||
|
||||
/** |
||||
* sheet holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class TableHolder { |
||||
/*** |
||||
* poi sheet |
||||
*/ |
||||
private SheetHolder parentSheet; |
||||
/** |
||||
* the header attribute of excel |
||||
*/ |
||||
private ExcelHeadProperty excelHeadProperty; |
||||
|
||||
private CellStyleStrategy cellStyleStrategy; |
||||
|
||||
private ColumnWidthStyleStrategy columnWidthStyleStrategy; |
||||
private RowHighStyleStrategy rowHighStyleStrategy; |
||||
|
||||
private boolean needHead = true; |
||||
|
||||
/** |
||||
* current table param |
||||
*/ |
||||
private Table currentTableParam; |
||||
|
||||
public Table getCurrentTableParam() { |
||||
return currentTableParam; |
||||
} |
||||
|
||||
public void setCurrentTableParam(Table currentTableParam) { |
||||
this.currentTableParam = currentTableParam; |
||||
} |
||||
|
||||
public SheetHolder getParentSheet() { |
||||
return parentSheet; |
||||
} |
||||
|
||||
public void setParentSheet(SheetHolder parentSheet) { |
||||
this.parentSheet = parentSheet; |
||||
} |
||||
|
||||
public ExcelHeadProperty getExcelHeadProperty() { |
||||
return excelHeadProperty; |
||||
} |
||||
|
||||
public void setExcelHeadProperty(ExcelHeadProperty excelHeadProperty) { |
||||
this.excelHeadProperty = excelHeadProperty; |
||||
} |
||||
|
||||
public CellStyleStrategy getCellStyleStrategy() { |
||||
return cellStyleStrategy; |
||||
} |
||||
|
||||
public void setCellStyleStrategy(CellStyleStrategy cellStyleStrategy) { |
||||
this.cellStyleStrategy = cellStyleStrategy; |
||||
} |
||||
|
||||
public ColumnWidthStyleStrategy getColumnWidthStyleStrategy() { |
||||
return columnWidthStyleStrategy; |
||||
} |
||||
|
||||
public void setColumnWidthStyleStrategy(ColumnWidthStyleStrategy columnWidthStyleStrategy) { |
||||
this.columnWidthStyleStrategy = columnWidthStyleStrategy; |
||||
} |
||||
|
||||
public RowHighStyleStrategy getRowHighStyleStrategy() { |
||||
return rowHighStyleStrategy; |
||||
} |
||||
|
||||
public void setRowHighStyleStrategy(RowHighStyleStrategy rowHighStyleStrategy) { |
||||
this.rowHighStyleStrategy = rowHighStyleStrategy; |
||||
} |
||||
|
||||
public boolean isNeedHead() { |
||||
return needHead; |
||||
} |
||||
|
||||
public void setNeedHead(boolean needHead) { |
||||
this.needHead = needHead; |
||||
} |
||||
} |
@ -1,61 +0,0 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
import org.apache.poi.ss.usermodel.IndexedColors; |
||||
|
||||
/** |
||||
* @author jipengfei |
||||
*/ |
||||
public class TableStyle { |
||||
|
||||
/** |
||||
* 表头背景颜色 |
||||
*/ |
||||
private IndexedColors tableHeadBackGroundColor; |
||||
|
||||
/** |
||||
* 表头字体样式 |
||||
*/ |
||||
private Font tableHeadFont; |
||||
|
||||
/** |
||||
* 表格内容字体样式 |
||||
*/ |
||||
private Font tableContentFont; |
||||
|
||||
/** |
||||
* 表格内容背景颜色 |
||||
*/ |
||||
private IndexedColors tableContentBackGroundColor; |
||||
|
||||
public IndexedColors getTableHeadBackGroundColor() { |
||||
return tableHeadBackGroundColor; |
||||
} |
||||
|
||||
public void setTableHeadBackGroundColor(IndexedColors tableHeadBackGroundColor) { |
||||
this.tableHeadBackGroundColor = tableHeadBackGroundColor; |
||||
} |
||||
|
||||
public Font getTableHeadFont() { |
||||
return tableHeadFont; |
||||
} |
||||
|
||||
public void setTableHeadFont(Font tableHeadFont) { |
||||
this.tableHeadFont = tableHeadFont; |
||||
} |
||||
|
||||
public Font getTableContentFont() { |
||||
return tableContentFont; |
||||
} |
||||
|
||||
public void setTableContentFont(Font tableContentFont) { |
||||
this.tableContentFont = tableContentFont; |
||||
} |
||||
|
||||
public IndexedColors getTableContentBackGroundColor() { |
||||
return tableContentBackGroundColor; |
||||
} |
||||
|
||||
public void setTableContentBackGroundColor(IndexedColors tableContentBackGroundColor) { |
||||
this.tableContentBackGroundColor = tableContentBackGroundColor; |
||||
} |
||||
} |
@ -0,0 +1,30 @@
|
||||
package com.alibaba.excel.write; |
||||
|
||||
import java.io.OutputStream; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import com.alibaba.excel.ExcelWriter; |
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
|
||||
public class ExcelWriterBuilder { |
||||
/** |
||||
* Excel type |
||||
*/ |
||||
private ExcelTypeEnum excelType; |
||||
|
||||
/** |
||||
* Final output stream |
||||
*/ |
||||
private OutputStream outputStream; |
||||
|
||||
/** |
||||
* |
||||
*/ |
||||
private List<Converter> customConverter = new ArrayList<Converter>(); |
||||
|
||||
public ExcelWriter build() { |
||||
new ExcelBuilderImpl(templateInputStream, outputStream, typeEnum, needHead, writeHandler, converters); |
||||
} |
||||
} |
@ -1,8 +0,0 @@
|
||||
package com.alibaba.excel.write; |
||||
|
||||
public interface MergeStrategy { |
||||
int getFirstRow(); |
||||
int getLastRow(); |
||||
int getFirstCol(); |
||||
int getLastCol(); |
||||
} |
@ -0,0 +1,33 @@
|
||||
package com.alibaba.excel.write.handler; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Row; |
||||
|
||||
import com.alibaba.excel.metadata.Head; |
||||
import com.alibaba.excel.metadata.SheetHolder; |
||||
import com.alibaba.excel.metadata.TableHolder; |
||||
import com.sun.istack.internal.Nullable; |
||||
|
||||
/** |
||||
* intercepts handle cell creation |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public interface CellExcelWriteHandler extends ExcelWriteHandler { |
||||
|
||||
/** |
||||
* called before create the cell |
||||
* |
||||
* @param writeContext |
||||
*/ |
||||
void beforeCellCreate(SheetHolder sheetHolder, @Nullable TableHolder tableHolder, Row row, Head head, |
||||
int relativeRowIndex, boolean isHead); |
||||
|
||||
/** |
||||
* called after the cell is created |
||||
* |
||||
* @param writeContext |
||||
*/ |
||||
void afterCellCreate(SheetHolder sheetHolder, @Nullable TableHolder tableHolder, Cell cell, Head head, |
||||
int relativeRowIndex, boolean isHead); |
||||
} |
@ -0,0 +1,10 @@
|
||||
package com.alibaba.excel.write.handler; |
||||
|
||||
import com.alibaba.excel.event.Handler; |
||||
|
||||
/** |
||||
* intercepts handle excel write |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public interface ExcelWriteHandler extends Handler {} |
@ -0,0 +1,31 @@
|
||||
package com.alibaba.excel.write.handler; |
||||
|
||||
import org.apache.poi.ss.usermodel.Row; |
||||
|
||||
import com.alibaba.excel.metadata.SheetHolder; |
||||
import com.alibaba.excel.metadata.TableHolder; |
||||
import com.sun.istack.internal.Nullable; |
||||
|
||||
/** |
||||
* intercepts handle row creation |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public interface RowExcelWriteHandler extends ExcelWriteHandler { |
||||
|
||||
/** |
||||
* called before create the row |
||||
* |
||||
* @param writeContext |
||||
*/ |
||||
void beforeRowCreate(SheetHolder sheetHolder, @Nullable TableHolder tableHolder, int rowIndex, int relativeRowIndex, |
||||
boolean isHead); |
||||
|
||||
/** |
||||
* called after the row is created |
||||
* |
||||
* @param writeContext |
||||
*/ |
||||
void afterRowCreate(SheetHolder sheetHolder, @Nullable TableHolder tableHolder, Row row, int relativeRowIndex, |
||||
boolean isHead); |
||||
} |
@ -0,0 +1,27 @@
|
||||
package com.alibaba.excel.write.handler; |
||||
|
||||
import org.apache.poi.ss.usermodel.Workbook; |
||||
|
||||
import com.alibaba.excel.metadata.SheetHolder; |
||||
|
||||
/** |
||||
* intercepts handle sheet creation |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public interface SheetExcelWriteHandler extends ExcelWriteHandler { |
||||
|
||||
/** |
||||
* called before create the sheet |
||||
* |
||||
* @param writeContext |
||||
*/ |
||||
void beforeSheetCreate(Workbook workbook, SheetHolder sheetHolder); |
||||
|
||||
/** |
||||
* called after the sheet is created |
||||
* |
||||
* @param writeContext |
||||
*/ |
||||
void afterSheetCreate(Workbook workbook, SheetHolder sheetHolder); |
||||
} |
@ -0,0 +1,25 @@
|
||||
package com.alibaba.excel.write.handler; |
||||
|
||||
import org.apache.poi.ss.usermodel.Workbook; |
||||
|
||||
/** |
||||
* intercepts handle Wookbook creation |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public interface WookbookExcelWriteHandler extends ExcelWriteHandler { |
||||
|
||||
/** |
||||
* called before create the sheet |
||||
* |
||||
* @param writeContext |
||||
*/ |
||||
void beforeWookbookCreate(); |
||||
|
||||
/** |
||||
* called after the sheet is created |
||||
* |
||||
* @param writeContext |
||||
*/ |
||||
void afterWookbookCreate(Workbook workbook); |
||||
} |
@ -0,0 +1,28 @@
|
||||
package com.alibaba.excel.write.merge; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Row; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
|
||||
import com.alibaba.excel.metadata.Head; |
||||
import com.alibaba.excel.metadata.SheetHolder; |
||||
import com.alibaba.excel.metadata.TableHolder; |
||||
import com.alibaba.excel.write.handler.CellExcelWriteHandler; |
||||
|
||||
public abstract class AbstractMergeStrategy implements CellExcelWriteHandler { |
||||
|
||||
@Override |
||||
public void beforeCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Row row, Head head, |
||||
int relativeRowIndex, boolean isHead) {} |
||||
|
||||
@Override |
||||
public void afterCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Cell cell, Head head, |
||||
int relativeRowIndex, boolean isHead) { |
||||
if (isHead) { |
||||
return; |
||||
} |
||||
merge(sheetHolder.getSheet(), cell, head, relativeRowIndex); |
||||
} |
||||
|
||||
protected abstract void merge(Sheet sheet, Cell cell, Head head, int relativeRowIndex); |
||||
} |
@ -0,0 +1,34 @@
|
||||
package com.alibaba.excel.write.merge; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
import org.apache.poi.ss.util.CellRangeAddress; |
||||
|
||||
import com.alibaba.excel.metadata.Head; |
||||
|
||||
/** |
||||
* The regions of the loop merge |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class LoopMergeStrategy extends AbstractMergeStrategy { |
||||
private int eachRow; |
||||
private int eachColumn; |
||||
|
||||
public LoopMergeStrategy(int eachRow, int eachColumn) { |
||||
if (eachRow < 1 || eachColumn < 1) { |
||||
throw new IllegalArgumentException("All parameters must be greater than 1"); |
||||
} |
||||
this.eachRow = eachRow; |
||||
this.eachColumn = eachColumn; |
||||
} |
||||
|
||||
@Override |
||||
protected void merge(Sheet sheet, Cell cell, Head head, int relativeRowIndex) { |
||||
if (relativeRowIndex % eachRow == 0 && head.getColumnIndex() % eachColumn == 0) { |
||||
CellRangeAddress cellRangeAddress = new CellRangeAddress(cell.getRowIndex(), cell.getRowIndex() + eachRow, |
||||
cell.getColumnIndex(), cell.getColumnIndex() + eachColumn); |
||||
sheet.addMergedRegion(cellRangeAddress); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,39 @@
|
||||
package com.alibaba.excel.write.merge; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
import org.apache.poi.ss.util.CellRangeAddress; |
||||
|
||||
import com.alibaba.excel.metadata.Head; |
||||
|
||||
/** |
||||
* It only merges once when create cell(firstRowIndex,lastRowIndex) |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class OnceAbsoluteMergeStrategy extends AbstractMergeStrategy { |
||||
|
||||
private int firstRowIndex; |
||||
private int lastRowIndex; |
||||
private int firstColumnIndex; |
||||
private int lastColumnIndex; |
||||
|
||||
public OnceAbsoluteMergeStrategy(int firstRowIndex, int lastRowIndex, int firstColumnIndex, int lastColumnIndex) { |
||||
if (firstRowIndex < 0 || lastRowIndex < 0 || firstColumnIndex < 0 || lastColumnIndex < 0) { |
||||
throw new IllegalArgumentException("All parameters must be greater than 0"); |
||||
} |
||||
this.firstRowIndex = firstRowIndex; |
||||
this.lastRowIndex = lastRowIndex; |
||||
this.firstColumnIndex = firstColumnIndex; |
||||
this.lastColumnIndex = lastColumnIndex; |
||||
} |
||||
|
||||
@Override |
||||
protected void merge(Sheet sheet, Cell cell, Head head, int relativeRowIndex) { |
||||
if (cell.getRowIndex() == firstRowIndex && cell.getColumnIndex() == firstColumnIndex) { |
||||
CellRangeAddress cellRangeAddress = |
||||
new CellRangeAddress(firstRowIndex, lastRowIndex, firstColumnIndex, lastColumnIndex); |
||||
sheet.addMergedRegion(cellRangeAddress); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,43 @@
|
||||
package com.alibaba.excel.write.style; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Row; |
||||
import org.apache.poi.ss.usermodel.Workbook; |
||||
|
||||
import com.alibaba.excel.metadata.Head; |
||||
import com.alibaba.excel.metadata.SheetHolder; |
||||
import com.alibaba.excel.metadata.TableHolder; |
||||
import com.alibaba.excel.write.handler.CellExcelWriteHandler; |
||||
import com.alibaba.excel.write.handler.WookbookExcelWriteHandler; |
||||
|
||||
public abstract class AbstractCellStyleStrategy implements CellExcelWriteHandler, WookbookExcelWriteHandler { |
||||
@Override |
||||
public void beforeWookbookCreate() {} |
||||
|
||||
@Override |
||||
public void afterWookbookCreate(Workbook workbook) { |
||||
initCellStyle(workbook); |
||||
} |
||||
|
||||
@Override |
||||
public void beforeCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Row row, Head head, |
||||
int relativeRowIndex, boolean isHead) {} |
||||
|
||||
@Override |
||||
public void afterCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Cell cell, Head head, |
||||
int relativeRowIndex, boolean isHead) { |
||||
if (isHead) { |
||||
setHeadCellStyle(cell, head, relativeRowIndex); |
||||
} else { |
||||
setContentCellStyle(cell, head, relativeRowIndex); |
||||
} |
||||
|
||||
} |
||||
|
||||
protected abstract void initCellStyle(Workbook workbook); |
||||
|
||||
protected abstract void setHeadCellStyle(Cell cell, Head head, int relativeRowIndex); |
||||
|
||||
protected abstract void setContentCellStyle(Cell cell, Head head, int relativeRowIndex); |
||||
|
||||
} |
@ -0,0 +1,65 @@
|
||||
package com.alibaba.excel.write.style; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
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.Head; |
||||
import com.alibaba.excel.util.StyleUtil; |
||||
|
||||
/** |
||||
* Head of style shareholders and content of style loop |
||||
*/ |
||||
public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyleStrategy { |
||||
|
||||
private Workbook workbook; |
||||
private Map<Integer, CellStyle> headCellStyleCache = new HashMap<Integer, CellStyle>(); |
||||
private Map<Integer, CellStyle> contentCellStyleCache = new HashMap<Integer, CellStyle>(); |
||||
|
||||
@Override |
||||
protected void initCellStyle(Workbook workbook) { |
||||
this.workbook = workbook; |
||||
} |
||||
|
||||
@Override |
||||
protected void setHeadCellStyle(Cell cell, Head head, int relativeRowIndex) { |
||||
int columnIndex = head.getColumnIndex(); |
||||
if (headCellStyleCache.containsKey(columnIndex)) { |
||||
cell.setCellStyle(headCellStyleCache.get(columnIndex)); |
||||
} |
||||
CellStyle cellStyle = StyleUtil.buildCellStyle(workbook, headCellStyle(head)); |
||||
headCellStyleCache.put(columnIndex, cellStyle); |
||||
cell.setCellStyle(cellStyle); |
||||
} |
||||
|
||||
@Override |
||||
protected void setContentCellStyle(Cell cell, Head head, int relativeRowIndex) { |
||||
int columnIndex = head.getColumnIndex(); |
||||
if (contentCellStyleCache.containsKey(columnIndex)) { |
||||
cell.setCellStyle(contentCellStyleCache.get(columnIndex)); |
||||
} |
||||
CellStyle cellStyle = StyleUtil.buildCellStyle(workbook, contentCellStyle(head)); |
||||
contentCellStyleCache.put(columnIndex, cellStyle); |
||||
cell.setCellStyle(cellStyle); |
||||
} |
||||
|
||||
/** |
||||
* Returns the column width corresponding to each column header |
||||
* |
||||
* @param head |
||||
* @return |
||||
*/ |
||||
protected abstract com.alibaba.excel.metadata.CellStyle headCellStyle(Head head); |
||||
|
||||
/** |
||||
* Returns the column width corresponding to each column header |
||||
* |
||||
* @param head |
||||
* @return |
||||
*/ |
||||
protected abstract com.alibaba.excel.metadata.CellStyle contentCellStyle(Head head); |
||||
|
||||
} |
@ -0,0 +1,57 @@
|
||||
package com.alibaba.excel.write.style; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
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; |
||||
|
||||
public class HorizontalCellStyleStrategy extends AbstractCellStyleStrategy { |
||||
|
||||
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 HorizontalCellStyleStrategy(CellStyle headCellStyle, List<CellStyle> contentCellStyleList) { |
||||
if (headCellStyle == null || contentCellStyleList == null || contentCellStyleList.isEmpty()) { |
||||
throw new IllegalArgumentException("All parameters can not be null"); |
||||
} |
||||
this.headCellStyle = headCellStyle; |
||||
this.contentCellStyleList = contentCellStyleList; |
||||
} |
||||
|
||||
public HorizontalCellStyleStrategy(CellStyle headCellStyle, CellStyle contentCellStyle) { |
||||
if (headCellStyle == null || contentCellStyle == null) { |
||||
throw new IllegalArgumentException("All parameters can not be null"); |
||||
} |
||||
this.headCellStyle = headCellStyle; |
||||
contentCellStyleList = new ArrayList<CellStyle>(); |
||||
contentCellStyleList.add(contentCellStyle); |
||||
} |
||||
|
||||
@Override |
||||
protected void initCellStyle(Workbook workbook) { |
||||
poiHeadCellStyle = StyleUtil.buildCellStyle(workbook, headCellStyle); |
||||
poiContentCellStyleList = new ArrayList<org.apache.poi.ss.usermodel.CellStyle>(); |
||||
for (CellStyle cellStyle : contentCellStyleList) { |
||||
poiContentCellStyleList.add(StyleUtil.buildCellStyle(workbook, cellStyle)); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected void setHeadCellStyle(Cell cell, Head head, int relativeRowIndex) { |
||||
cell.setCellStyle(poiHeadCellStyle); |
||||
} |
||||
|
||||
@Override |
||||
protected void setContentCellStyle(Cell cell, Head head, int relativeRowIndex) { |
||||
cell.setCellStyle(poiContentCellStyleList.get(relativeRowIndex % poiContentCellStyleList.size())); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.alibaba.excel.write.style.column; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Row; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
|
||||
import com.alibaba.excel.metadata.Head; |
||||
import com.alibaba.excel.metadata.SheetHolder; |
||||
import com.alibaba.excel.metadata.TableHolder; |
||||
import com.alibaba.excel.write.handler.CellExcelWriteHandler; |
||||
|
||||
/** |
||||
* Column width style strategy |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public abstract class AbstractColumnWidthStyleStrategy implements CellExcelWriteHandler { |
||||
|
||||
@Override |
||||
public void beforeCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Row row, Head head, |
||||
int relativeRowIndex, boolean isHead) {} |
||||
|
||||
@Override |
||||
public void afterCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Cell cell, Head head, |
||||
int relativeRowIndex, boolean isHead) { |
||||
if (!isHead) { |
||||
return; |
||||
} |
||||
setColumnWidth(sheetHolder.getSheet(), cell, head); |
||||
} |
||||
|
||||
/** |
||||
* Sets the column width when head create |
||||
* |
||||
* @param sheet |
||||
* @param cell |
||||
* @param head |
||||
*/ |
||||
protected abstract void setColumnWidth(Sheet sheet, Cell cell, Head head); |
||||
|
||||
} |
@ -0,0 +1,26 @@
|
||||
package com.alibaba.excel.write.style.column; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
|
||||
import com.alibaba.excel.metadata.Head; |
||||
|
||||
/** |
||||
* Returns the column width according to each column header |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public abstract class AbstractHeadColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy { |
||||
@Override |
||||
protected void setColumnWidth(Sheet sheet, Cell cell, Head head) { |
||||
sheet.setColumnWidth(cell.getColumnIndex(), columnWidth(head)); |
||||
} |
||||
|
||||
/** |
||||
* Returns the column width corresponding to each column header |
||||
* |
||||
* @param head |
||||
* @return |
||||
*/ |
||||
protected abstract int columnWidth(Head head); |
||||
} |
@ -0,0 +1,24 @@
|
||||
package com.alibaba.excel.write.style.column; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
|
||||
import com.alibaba.excel.metadata.Head; |
||||
|
||||
/** |
||||
* All the columns are the same width |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SimpleColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy { |
||||
private int columnWidth; |
||||
|
||||
public SimpleColumnWidthStyleStrategy(int columnWidth) { |
||||
this.columnWidth = columnWidth; |
||||
} |
||||
|
||||
@Override |
||||
protected void setColumnWidth(Sheet sheet, Cell cell, Head head) { |
||||
sheet.setColumnWidth(cell.getColumnIndex(), columnWidth); |
||||
} |
||||
} |
@ -0,0 +1,31 @@
|
||||
package com.alibaba.excel.write.style.row; |
||||
|
||||
import org.apache.poi.ss.usermodel.Row; |
||||
|
||||
import com.alibaba.excel.metadata.SheetHolder; |
||||
import com.alibaba.excel.metadata.TableHolder; |
||||
import com.alibaba.excel.write.handler.RowExcelWriteHandler; |
||||
|
||||
public abstract class AbstractRowHighStyleStrategy implements RowExcelWriteHandler { |
||||
|
||||
@Override |
||||
public void beforeRowCreate(SheetHolder sheetHolder, TableHolder tableHolder, int rowIndex, int relativeRowIndex, |
||||
boolean isHead) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void afterRowCreate(SheetHolder sheetHolder, TableHolder tableHolder, Row row, int relativeRowIndex, |
||||
boolean isHead) { |
||||
if (isHead) { |
||||
setHeadColumnHigh(row, relativeRowIndex); |
||||
} else { |
||||
setContentColumnHigh(row, relativeRowIndex); |
||||
} |
||||
} |
||||
|
||||
protected abstract void setHeadColumnHigh(Row row, int relativeRowIndex); |
||||
|
||||
protected abstract void setContentColumnHigh(Row row, int relativeRowIndex); |
||||
|
||||
} |
@ -0,0 +1,29 @@
|
||||
package com.alibaba.excel.write.style.row; |
||||
|
||||
import org.apache.poi.ss.usermodel.Row; |
||||
|
||||
/** |
||||
* Set the head column high and content column high |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SimpleRowHighStyleStrategy extends AbstractRowHighStyleStrategy { |
||||
private short headColumnHigh; |
||||
private short contentColumnHigh; |
||||
|
||||
public SimpleRowHighStyleStrategy(short headColumnHigh, short contentColumnHigh) { |
||||
this.headColumnHigh = headColumnHigh; |
||||
this.contentColumnHigh = contentColumnHigh; |
||||
} |
||||
|
||||
@Override |
||||
protected void setHeadColumnHigh(Row row, int relativeRowIndex) { |
||||
row.setHeight(headColumnHigh); |
||||
} |
||||
|
||||
@Override |
||||
protected void setContentColumnHigh(Row row, int relativeRowIndex) { |
||||
row.setHeight(contentColumnHigh); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue