Browse Source

Strategy 编写完成

pull/507/head
zhuangjiaju 5 years ago
parent
commit
0d4f48d303
  1. 4
      pom.xml
  2. 2
      src/main/java/com/alibaba/excel/ExcelWriter.java
  3. 9
      src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
  4. 4
      src/main/java/com/alibaba/excel/context/WriteContext.java
  5. 281
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  6. 21
      src/main/java/com/alibaba/excel/enums/HeadKindEnum.java
  7. 8
      src/main/java/com/alibaba/excel/event/Handler.java
  8. 9
      src/main/java/com/alibaba/excel/event/WriteHandler.java
  9. 36
      src/main/java/com/alibaba/excel/metadata/CellStyle.java
  10. 174
      src/main/java/com/alibaba/excel/metadata/ExcelHeadProperty.java
  11. 73
      src/main/java/com/alibaba/excel/metadata/Head.java
  12. 28
      src/main/java/com/alibaba/excel/metadata/Sheet.java
  13. 104
      src/main/java/com/alibaba/excel/metadata/SheetHolder.java
  14. 13
      src/main/java/com/alibaba/excel/metadata/Table.java
  15. 89
      src/main/java/com/alibaba/excel/metadata/TableHolder.java
  16. 61
      src/main/java/com/alibaba/excel/metadata/TableStyle.java
  17. 44
      src/main/java/com/alibaba/excel/util/StyleUtil.java
  18. 1
      src/main/java/com/alibaba/excel/write/ExcelBuilder.java
  19. 4
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  20. 30
      src/main/java/com/alibaba/excel/write/ExcelWriterBuilder.java
  21. 8
      src/main/java/com/alibaba/excel/write/MergeStrategy.java
  22. 33
      src/main/java/com/alibaba/excel/write/handler/CellExcelWriteHandler.java
  23. 10
      src/main/java/com/alibaba/excel/write/handler/ExcelWriteHandler.java
  24. 31
      src/main/java/com/alibaba/excel/write/handler/RowExcelWriteHandler.java
  25. 27
      src/main/java/com/alibaba/excel/write/handler/SheetExcelWriteHandler.java
  26. 25
      src/main/java/com/alibaba/excel/write/handler/WookbookExcelWriteHandler.java
  27. 28
      src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java
  28. 34
      src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java
  29. 39
      src/main/java/com/alibaba/excel/write/merge/OnceAbsoluteMergeStrategy.java
  30. 43
      src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java
  31. 65
      src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java
  32. 57
      src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java
  33. 41
      src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java
  34. 26
      src/main/java/com/alibaba/excel/write/style/column/AbstractHeadColumnWidthStyleStrategy.java
  35. 24
      src/main/java/com/alibaba/excel/write/style/column/SimpleColumnWidthStyleStrategy.java
  36. 31
      src/main/java/com/alibaba/excel/write/style/row/AbstractRowHighStyleStrategy.java
  37. 29
      src/main/java/com/alibaba/excel/write/style/row/SimpleRowHighStyleStrategy.java
  38. 4
      src/test/java/com/alibaba/easyexcel/test/WriteTest.java

4
pom.xml

@ -59,12 +59,12 @@
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId> <artifactId>poi</artifactId>
<version>3.17</version> <version>4.0.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId> <artifactId>poi-ooxml</artifactId>
<version>3.17</version> <version>4.0.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cglib</groupId> <groupId>cglib</groupId>

2
src/main/java/com/alibaba/excel/ExcelWriter.java

@ -21,7 +21,7 @@ import com.alibaba.excel.parameter.GenerateParam;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.ExcelBuilder; import com.alibaba.excel.write.ExcelBuilder;
import com.alibaba.excel.write.ExcelBuilderImpl; import com.alibaba.excel.write.ExcelBuilderImpl;
import com.alibaba.excel.write.MergeStrategy; import com.alibaba.excel.write.merge.MergeStrategy;
/** /**
* Excel Writer This tool is used to write value out to Excel via POI. This object can perform the * Excel Writer This tool is used to write value out to Excel via POI. This object can perform the

9
src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java

@ -34,7 +34,7 @@ public class AnalysisContextImpl implements AnalysisContext {
private boolean trim; private boolean trim;
private boolean use1904WindowDate = false; private boolean use1904WindowDate = false;
private ConverterRegistryCenter converterRegistryCenter; private ConverterRegistryCenter converterRegistryCenter;
@Override @Override
@ -74,13 +74,14 @@ public class AnalysisContextImpl implements AnalysisContext {
this.converterRegistryCenter = converterRegistryCenter; this.converterRegistryCenter = converterRegistryCenter;
} }
@Override @Override
public void setCurrentSheet(Sheet currentSheet) { public void setCurrentSheet(Sheet currentSheet) {
cleanCurrentSheet(); cleanCurrentSheet();
this.currentSheet = currentSheet; this.currentSheet = currentSheet;
if (currentSheet.getClazz() != null) { if (currentSheet.getClazz() != null) {
ExcelHeadProperty.buildExcelHeadProperty(this.excelHeadProperty, currentSheet.getClazz(), null); excelHeadProperty =
ExcelHeadProperty.buildExcelHeadProperty(this.excelHeadProperty, currentSheet.getClazz(), null);
} }
} }
@ -156,7 +157,7 @@ public class AnalysisContextImpl implements AnalysisContext {
public ExcelHeadProperty getExcelHeadProperty() { public ExcelHeadProperty getExcelHeadProperty() {
return this.excelHeadProperty; return this.excelHeadProperty;
} }
@Override @Override
public void setExcelHeadProperty(ExcelHeadProperty excelHeadProperty) { public void setExcelHeadProperty(ExcelHeadProperty excelHeadProperty) {

4
src/main/java/com/alibaba/excel/context/WriteContext.java

@ -9,6 +9,7 @@ import org.apache.poi.ss.usermodel.Workbook;
import com.alibaba.excel.converters.ConverterRegistryCenter; import com.alibaba.excel.converters.ConverterRegistryCenter;
import com.alibaba.excel.event.WriteHandler; import com.alibaba.excel.event.WriteHandler;
import com.alibaba.excel.metadata.ExcelHeadProperty; import com.alibaba.excel.metadata.ExcelHeadProperty;
import com.alibaba.excel.metadata.SheetHolder;
import com.alibaba.excel.metadata.Table; import com.alibaba.excel.metadata.Table;
public interface WriteContext { public interface WriteContext {
@ -26,7 +27,8 @@ public interface WriteContext {
OutputStream getOutputStream(); OutputStream getOutputStream();
Workbook getWorkbook(); Workbook getWorkbook();
@Deprecated
WriteHandler getWriteHandler(); WriteHandler getWriteHandler();
CellStyle getCurrentContentStyle(); CellStyle getCurrentContentStyle();

281
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -1,13 +1,11 @@
package com.alibaba.excel.context; package com.alibaba.excel.context;
import static com.alibaba.excel.util.StyleUtil.buildSheetStyle;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
@ -18,13 +16,15 @@ import org.apache.poi.ss.util.CellRangeAddress;
import com.alibaba.excel.converters.ConverterRegistryCenter; import com.alibaba.excel.converters.ConverterRegistryCenter;
import com.alibaba.excel.event.WriteHandler; import com.alibaba.excel.event.WriteHandler;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.ExcelHeadProperty; import com.alibaba.excel.metadata.ExcelHeadProperty;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.SheetHolder;
import com.alibaba.excel.metadata.Table; import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.metadata.TableHolder;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.StyleUtil; import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.write.style.CellStyleStrategy;
/** /**
* A context is the main anchorage point of a excel writer. * A context is the main anchorage point of a excel writer.
@ -32,27 +32,18 @@ import com.alibaba.excel.util.WorkBookUtil;
* @author jipengfei * @author jipengfei
*/ */
public class WriteContextImpl implements WriteContext { public class WriteContextImpl implements WriteContext {
/***
* The sheet currently written
*/
private Sheet currentSheet;
/** /**
* current param * prevent duplicate creation of sheet objects
*/ */
private com.alibaba.excel.metadata.Sheet currentSheetParam; private final Map<Integer, SheetHolder> hasBeenInitializedSheet = new HashMap<Integer, SheetHolder>();
/** /**
* The sheet currently written's name * Current sheet holder
*/ */
private String currentSheetName; private SheetHolder currentSheetHolder;
/** /**
* * The table currently written
*/ */
private Table currentTable; private TableHolder currentTableHolder;
/** /**
* Excel type * Excel type
*/ */
@ -69,42 +60,16 @@ public class WriteContextImpl implements WriteContext {
private OutputStream outputStream; private OutputStream outputStream;
/** /**
* Written form collection * If sheet and table don't have {@link CellStyleStrategy} , use this one. If they have, use their own
*/
private Map<Integer, Table> tableMap = new ConcurrentHashMap<Integer, Table>();
/**
* Cell default style
*/
private CellStyle defaultCellStyle;
/**
* Current table head style
*/
private CellStyle currentHeadCellStyle;
/**
* Current table content style
*/
private CellStyle currentContentCellStyle;
/**
* the header attribute of excel
*/ */
private ExcelHeadProperty excelHeadProperty; private CellStyleStrategy defalutCellStyleStrategy;
private boolean needHead = true;
private WriteHandler writeHandler; private WriteHandler writeHandler;
private ConverterRegistryCenter registryCenter;
public WriteHandler getWriteHandler() { private ConverterRegistryCenter registryCenter;
return writeHandler;
}
public WriteContextImpl(InputStream templateInputStream, OutputStream out, ExcelTypeEnum excelType, public WriteContextImpl(InputStream templateInputStream, OutputStream out, ExcelTypeEnum excelType,
boolean needHead, WriteHandler writeHandler, ConverterRegistryCenter registryCenter) throws IOException { boolean needHead, WriteHandler writeHandler, ConverterRegistryCenter registryCenter) throws IOException {
this.needHead = needHead; this.needHead = needHead;
this.outputStream = out; this.outputStream = out;
this.writeHandler = writeHandler; this.writeHandler = writeHandler;
@ -118,103 +83,146 @@ public class WriteContextImpl implements WriteContext {
*/ */
@Override @Override
public void currentSheet(com.alibaba.excel.metadata.Sheet sheet) { public void currentSheet(com.alibaba.excel.metadata.Sheet sheet) {
if (null == currentSheetParam || currentSheetParam.getSheetNo() != sheet.getSheetNo()) { if (sheet == null) {
cleanCurrentSheet(); throw new IllegalArgumentException("Sheet argument cannot be null");
currentSheetParam = sheet; }
try { if (hasBeenInitializedSheet.containsKey(sheet.getSheetNo())) {
this.currentSheet = workbook.getSheetAt(sheet.getSheetNo() - 1); currentSheetHolder = hasBeenInitializedSheet.get(sheet.getSheetNo());
} catch (Exception e) { return;
this.currentSheet = WorkBookUtil.createSheet(workbook, sheet); }
if (null != writeHandler) { // create sheet
this.writeHandler.sheet(sheet.getSheetNo(), currentSheet); currentSheetParam = sheet;
} try {
this.currentSheet = workbook.getSheetAt(sheet.getSheetNo() - 1);
} catch (Exception e) {
this.currentSheet = WorkBookUtil.createSheet(workbook, sheet);
if (null != writeHandler) {
this.writeHandler.sheet(sheet.getSheetNo(), currentSheet);
} }
buildSheetStyle(currentSheet, sheet.getColumnWidthMap());
/** **/
this.initCurrentSheet(sheet);
} }
// Initialization current sheet
initCurrentSheet();
} }
private void initCurrentSheet(com.alibaba.excel.metadata.Sheet sheet) { private void initCurrentSheet() {
// Initialization head
/** **/ initExcelHeadProperty();
initExcelHeadProperty(sheet.getHead(), sheet.getClazz()); // Initialization cell style strategy
initCellStyleStrategy();
initTableStyle(sheet.getTableStyle()); // Initialization sheet head
initSheetHead();
}
initTableHead(); private void initCellStyleStrategy() {
if (currentSheetParam.getCellStyleStrategy() != null) {
currentSheetHolder.setCellStyleStrategy(currentSheetParam.getCellStyleStrategy());
}
currentSheetHolder.setCellStyleStrategy(defalutCellStyleStrategy);
}
private void initTableCellStyleStrategy() {
if (.getCellStyleStrategy() != null) {
currentSheetHolder.setCellStyleStrategy(currentSheetParam.getCellStyleStrategy());
}
currentSheetHolder.setCellStyleStrategy(defalutCellStyleStrategy);
} }
private void cleanCurrentSheet() { /**
this.currentSheet = null; * init excel header
this.currentSheetParam = null; */
this.excelHeadProperty = null; private void initExcelHeadProperty() {
this.currentHeadCellStyle = null; currentSheetHolder
this.currentContentCellStyle = null; .setExcelHeadProperty(new ExcelHeadProperty(currentSheetParam.getClazz(), currentSheetParam.getHead()));
this.currentTable = null; }
/**
* init table excel header
*/
private void initTableExcelHeadProperty() {
currentTableHolder.setExcelHeadProperty(
new ExcelHeadProperty(getCurrentTableParam().getClazz(), getCurrentTableParam().getHead()));
} }
/** /**
* init excel header * init sheet column with
*
* @param head
* @param clazz
*/ */
private void initExcelHeadProperty(List<List<String>> head, Class<? extends BaseRowModel> clazz) { public void initSheetColumnWidth() {
if (head != null || clazz != null) { this.excelHeadProperty = new ExcelHeadProperty(clazz, head); } for (Head head : currentSheetHolder.getExcelHeadProperty().getHeadList()) {
currentSheet.setColumnWidth(head.getColumnIndex(), currentSheetHolder.getColumnWidthStyleStrategy()
.columnWidth(head.getColumnIndex(), head.getFieldName()));
}
} }
public void initTableHead() { public void initSheetHead() {
if (needHead && null != excelHeadProperty && !CollectionUtils.isEmpty(excelHeadProperty.getHead())) { if (!currentSheetHolder.isNeedHead() || !currentSheetHolder.getExcelHeadProperty().hasHead()) {
int startRow = currentSheet.getLastRowNum(); return;
if (startRow > 0) { }
startRow += 4; int startRow = getCurrentSheet().getLastRowNum();
} else { if (startRow > 0) {
startRow = currentSheetParam.getStartRow(); startRow += 4;
} else {
startRow = getCurrentSheetParam().getStartRow();
}
// Combined head
addMergedRegionToCurrentSheet(startRow);
for (int i = startRow; i < currentSheetHolder.getExcelHeadProperty().getHeadRowNumber() + startRow; i++) {
Row row = WorkBookUtil.createRow(getCurrentSheet(), i);
// Set the row height of the header
currentSheetHolder.getRowHighStyleStrategy().headColumnHigh(i);
if (null != writeHandler) {
this.writeHandler.row(i, row);
} }
addMergedRegionToCurrentSheet(startRow); addOneRowOfHeadDataToExcel(row, i, currentSheetHolder.getExcelHeadProperty().getHeadList());
int i = startRow; }
for (; i < this.excelHeadProperty.getRowNum() + startRow; i++) { // Initialization sheet column width
Row row = WorkBookUtil.createRow(currentSheet, i); initSheetColumnWidth();
if (null != writeHandler) { }
this.writeHandler.row(i, row);
} public void initTableHead() {
addOneRowOfHeadDataToExcel(row, this.excelHeadProperty.getHeadByRowNum(i - startRow)); if (!currentTableHolder.isNeedHead() || !currentTableHolder.getExcelHeadProperty().hasHead()) {
return;
}
int startRow = getCurrentSheet().getLastRowNum();
if (startRow > 0) {
startRow += 4;
} else {
startRow = getCurrentSheetParam().getStartRow();
}
// Combined head
addMergedRegionToCurrentSheet(startRow);
for (int i = startRow; i < currentTableHolder.getExcelHeadProperty().getHeadRowNumber() + startRow; i++) {
Row row = WorkBookUtil.createRow(getCurrentSheet(), i);
// Set the row height of the header
currentTableHolder.getRowHighStyleStrategy().headColumnHigh(i);
if (null != writeHandler) {
this.writeHandler.row(i, row);
} }
addOneRowOfHeadDataToExcel(row, i, currentTableHolder.getExcelHeadProperty().getHeadList());
} }
// Initialization sheet column width
initSheetColumnWidth();
} }
private void addMergedRegionToCurrentSheet(int startRow) { private void addMergedRegionToCurrentSheet(int startRow) {
for (com.alibaba.excel.metadata.CellRange cellRangeModel : excelHeadProperty.getCellRangeModels()) { for (com.alibaba.excel.metadata.CellRange cellRangeModel : currentSheetHolder.getExcelHeadProperty()
.getCellRangeModels()) {
currentSheet.addMergedRegion(new CellRangeAddress(cellRangeModel.getFirstRow() + startRow, currentSheet.addMergedRegion(new CellRangeAddress(cellRangeModel.getFirstRow() + startRow,
cellRangeModel.getLastRow() + startRow, cellRangeModel.getLastRow() + startRow, cellRangeModel.getFirstCol(), cellRangeModel.getLastCol()));
cellRangeModel.getFirstCol(), cellRangeModel.getLastCol()));
} }
} }
private void addOneRowOfHeadDataToExcel(Row row, List<String> headByRowNum) { private void addOneRowOfHeadDataToExcel(Row row, int rowIndex, List<Head> headList) {
if (headByRowNum != null && headByRowNum.size() > 0) { for (int i = 0; i < headList.size(); i++) {
for (int i = 0; i < headByRowNum.size(); i++) { Head head = headList.get(i);
Cell cell = WorkBookUtil.createCell(row, i, getCurrentHeadCellStyle(), headByRowNum.get(i)); Cell cell = WorkBookUtil.createCell(row, i,
if (null != writeHandler) { currentSheetHolder.getCellStyleStrategy().headCellStyle(rowIndex, headList.get(i)),
this.writeHandler.cell(i, cell); head.getHeadName(i));
} if (null != writeHandler) {
this.writeHandler.cell(i, cell);
} }
} }
} }
private void initTableStyle(com.alibaba.excel.metadata.TableStyle tableStyle) {
if (tableStyle != null) {
this.currentHeadCellStyle = StyleUtil.buildCellStyle(this.workbook, tableStyle.getTableHeadFont(),
tableStyle.getTableHeadBackGroundColor());
this.currentContentCellStyle = StyleUtil.buildCellStyle(this.workbook, tableStyle.getTableContentFont(),
tableStyle.getTableContentBackGroundColor());
}
}
private void cleanCurrentTable() { private void cleanCurrentTable() {
this.excelHeadProperty = null; this.excelHeadProperty = null;
this.currentHeadCellStyle = null; this.currentHeadCellStyle = null;
@ -225,6 +233,23 @@ public class WriteContextImpl implements WriteContext {
@Override @Override
public void currentTable(Table table) { public void currentTable(Table table) {
if (table == null) {
return;
}
if (currentSheetHolder == null) {
throw new IllegalStateException("Must first call WriteContextImpl#currentSheet");
}
if(currentSheetHolder.getHasBeenInitializedTable().containsKey(table.getTableNo())){
currentTableHolder=currentSheetHolder.getHasBeenInitializedTable().get(table.getTableNo());
return;
}
currentTable.getTableNo()
if (null == currentTable || currentTable.getTableNo() != table.getTableNo()) { if (null == currentTable || currentTable.getTableNo() != table.getTableNo()) {
cleanCurrentTable(); cleanCurrentTable();
this.currentTable = table; this.currentTable = table;
@ -235,17 +260,19 @@ public class WriteContextImpl implements WriteContext {
} }
@Override
public ExcelHeadProperty getExcelHeadProperty() { public ExcelHeadProperty getExcelHeadProperty() {
return this.excelHeadProperty; return currentSheetHolder.getExcelHeadProperty();
} }
@Override
public boolean needHead() { public boolean needHead() {
return this.needHead; return this.needHead;
} }
@Override @Override
public Sheet getCurrentSheet() { public Sheet getCurrentSheet() {
return currentSheet; return currentSheetHolder.getSheet();
} }
public void setCurrentSheet(Sheet currentSheet) { public void setCurrentSheet(Sheet currentSheet) {
@ -253,7 +280,7 @@ public class WriteContextImpl implements WriteContext {
} }
public String getCurrentSheetName() { public String getCurrentSheetName() {
return currentSheetName; return currentSheetHolder.getCurrentSheetParam();
} }
public void setCurrentSheetName(String currentSheetName) { public void setCurrentSheetName(String currentSheetName) {
@ -287,19 +314,19 @@ public class WriteContextImpl implements WriteContext {
} }
public com.alibaba.excel.metadata.Sheet getCurrentSheetParam() { public com.alibaba.excel.metadata.Sheet getCurrentSheetParam() {
return currentSheetParam; return currentSheetHolder.getCurrentSheetParam();
} }
public void setCurrentSheetParam(com.alibaba.excel.metadata.Sheet currentSheetParam) { public void setCurrentSheetParam(com.alibaba.excel.metadata.Sheet currentSheetParam) {
this.currentSheetParam = currentSheetParam; this.currentSheetParam = currentSheetParam;
} }
public Table getCurrentTable() { public Table getCurrentTableParam() {
return currentTable; return currentTableHolder.getCurrentTableParam();
} }
public void setCurrentTable(Table currentTable) { public Table getCurrentTable() {
this.currentTable = currentTable; return currentTable;
} }
@Override @Override
@ -307,5 +334,3 @@ public class WriteContextImpl implements WriteContext {
return registryCenter; return registryCenter;
} }
} }

21
src/main/java/com/alibaba/excel/enums/HeadKindEnum.java

@ -0,0 +1,21 @@
package com.alibaba.excel.enums;
/**
* The types of head
*
* @author zhuangjiaju
**/
public enum HeadKindEnum {
/**
* none
*/
NONE,
/**
* class
*/
CLASS,
/**
* String
*/
STRING;
}

8
src/main/java/com/alibaba/excel/event/Handler.java

@ -0,0 +1,8 @@
package com.alibaba.excel.event;
/**
* intercepts handle some business logic
*
* @author zhuangjiaju
**/
public interface Handler {}

9
src/main/java/com/alibaba/excel/event/WriteHandler.java

@ -4,11 +4,17 @@ import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import com.alibaba.excel.write.handler.ExcelWriteHandler;
/** /**
* *
* @deprecated Separate implementations
* @see com.alibaba.excel.write.handler.SheetExcelWriteHandler
* @see com.alibaba.excel.write.handler.RowExcelWriteHandler
* @see com.alibaba.excel.write.handler.CellExcelWriteHandler
* @author jipengfei * @author jipengfei
*/ */
public interface WriteHandler { public interface WriteHandler extends ExcelWriteHandler {
/** /**
* Custom operation after creating each sheet * Custom operation after creating each sheet
@ -28,6 +34,7 @@ public interface WriteHandler {
/** /**
* Custom operation after creating each cell * Custom operation after creating each cell
*
* @param cellNum * @param cellNum
* @param cell * @param cell
*/ */

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

@ -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;
}
}

174
src/main/java/com/alibaba/excel/metadata/ExcelHeadProperty.java

@ -1,11 +1,16 @@
package com.alibaba.excel.metadata; package com.alibaba.excel.metadata;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.excel.annotation.ExcelColumnNum; import com.alibaba.excel.annotation.ExcelColumnNum;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.enums.HeadKindEnum;
import java.lang.reflect.Field;
import java.util.*;
/** /**
* Define the header attribute of excel * Define the header attribute of excel
@ -22,7 +27,7 @@ public class ExcelHeadProperty {
/** /**
* A two-dimensional array describing the header * A two-dimensional array describing the header
*/ */
private List<List<String>> head = new ArrayList<List<String>>(); private List<Head> headList;
/** /**
* Attributes described by the header * Attributes described by the header
@ -33,11 +38,53 @@ public class ExcelHeadProperty {
* Attributes described by the header * Attributes described by the header
*/ */
private Map<Integer, ExcelColumnProperty> excelColumnPropertyMap1 = new HashMap<Integer, ExcelColumnProperty>(); private Map<Integer, ExcelColumnProperty> excelColumnPropertyMap1 = new HashMap<Integer, ExcelColumnProperty>();
/**
* The types of head
*/
private HeadKindEnum headKind;
/**
* The number of rows in the line with the most rows
*/
private int headRowNumber;
public ExcelHeadProperty(Class<? extends BaseRowModel> headClazz, List<List<String>> head) { public ExcelHeadProperty(Class<? extends BaseRowModel> headClazz, List<List<String>> head) {
this.headClazz = headClazz; this.headClazz = headClazz;
this.head = head; headList = new ArrayList<Head>();
initColumnProperties(); headKind = HeadKindEnum.NONE;
headRowNumber = 0;
if (head != null && !head.isEmpty()) {
int index = 0;
for (List<String> headData : head) {
headList.add(new Head(index++, null, headData));
}
headKind = HeadKindEnum.STRING;
} else {
// convert headClazz to head
initColumnProperties();
}
initHeadRowNumber();
}
public static ExcelHeadProperty buildExcelHeadProperty(ExcelHeadProperty excelHeadProperty,
Class<? extends BaseRowModel> clazz, List<String> headOneRow) {
if (excelHeadProperty == null) {
return new ExcelHeadProperty(clazz, new ArrayList<List<String>>());
}
if (headOneRow != null) {
excelHeadProperty.appendOneRow(headOneRow);
}
return excelHeadProperty;
}
private void initHeadRowNumber() {
headRowNumber = 0;
for (Head head : headList) {
List<String> list = head.getHeadNames();
if (list != null && list.size() > headRowNumber) {
headRowNumber = list.size();
}
}
} }
/** /**
@ -46,25 +93,28 @@ public class ExcelHeadProperty {
if (this.headClazz != null) { if (this.headClazz != null) {
List<Field> fieldList = new ArrayList<Field>(); List<Field> fieldList = new ArrayList<Field>();
Class tempClass = this.headClazz; Class tempClass = this.headClazz;
//When the parent class is null, it indicates that the parent class (Object class) has reached the top // When the parent class is null, it indicates that the parent class (Object class) has reached the top
// level. // level.
while (tempClass != null) { while (tempClass != null) {
fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields())); fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
//Get the parent class and give it to yourself // Get the parent class and give it to yourself
tempClass = tempClass.getSuperclass(); tempClass = tempClass.getSuperclass();
} }
List<List<String>> headList = new ArrayList<List<String>>();
for (Field f : fieldList) { for (Field f : fieldList) {
initOneColumnProperty(f); initOneColumnProperty(f);
} }
//对列排序 // 对列排序
Collections.sort(columnPropertyList); Collections.sort(columnPropertyList);
if (head == null || head.size() == 0) {
for (ExcelColumnProperty excelColumnProperty : columnPropertyList) { int index = 0;
headList.add(excelColumnProperty.getHead()); for (ExcelColumnProperty excelColumnProperty : columnPropertyList) {
if (excelColumnProperty.getHead() != null && excelColumnProperty.getHead().size() > headRowNumber) {
headRowNumber = excelColumnProperty.getHead().size();
} }
this.head = headList; headList
.add(new Head(index++, excelColumnProperty.getField().getName(), excelColumnProperty.getHead()));
} }
headKind = HeadKindEnum.CLASS;
} }
} }
@ -98,21 +148,21 @@ public class ExcelHeadProperty {
} }
/** /**
* * Add one more head under the last head
*/ */
public void appendOneRow(List<String> row) { public void appendOneRow(List<String> row) {
int headSize = headList.size();
for (int i = 0; i < row.size(); i++) { for (int i = 0; i < row.size(); i++) {
List<String> list; String rowData = row.get(i);
if (head.size() <= i) { // join
list = new ArrayList<String>(); if (i <= headSize) {
head.add(list); headList.get(i).getHeadNames().add(rowData);
} else { } else {
list = head.get(0); // create and join
headList.add(new Head(i, null, rowData));
} }
list.add(row.get(i));
} }
initHeadRowNumber();
} }
/** /**
@ -131,14 +181,6 @@ public class ExcelHeadProperty {
this.headClazz = headClazz; this.headClazz = headClazz;
} }
public List<List<String>> getHead() {
return this.head;
}
public void setHead(List<List<String>> head) {
this.head = head;
}
public List<ExcelColumnProperty> getColumnPropertyList() { public List<ExcelColumnProperty> getColumnPropertyList() {
return columnPropertyList; return columnPropertyList;
} }
@ -147,6 +189,34 @@ public class ExcelHeadProperty {
this.columnPropertyList = columnPropertyList; this.columnPropertyList = columnPropertyList;
} }
public List<Head> getHeadList() {
return headList;
}
public void setHeadList(List<Head> headList) {
this.headList = headList;
}
public HeadKindEnum getHeadKind() {
return headKind;
}
public void setHeadKind(HeadKindEnum headKind) {
this.headKind = headKind;
}
public boolean hasHead() {
return headKind == HeadKindEnum.NONE;
}
public int getHeadRowNumber() {
return headRowNumber;
}
public void setHeadRowNumber(int headRowNumber) {
this.headRowNumber = headRowNumber;
}
/** /**
* Calculate all cells that need to be merged * Calculate all cells that need to be merged
* *
@ -154,8 +224,8 @@ public class ExcelHeadProperty {
*/ */
public List<CellRange> getCellRangeModels() { public List<CellRange> getCellRangeModels() {
List<CellRange> cellRanges = new ArrayList<CellRange>(); List<CellRange> cellRanges = new ArrayList<CellRange>();
for (int i = 0; i < head.size(); i++) { for (int i = 0; i < headList.size(); i++) {
List<String> columnValues = head.get(i); List<String> columnValues = headList.get(i).getHeadNames();
for (int j = 0; j < columnValues.size(); j++) { for (int j = 0; j < columnValues.size(); j++) {
int lastRow = getLastRangNum(j, columnValues.get(j), columnValues); int lastRow = getLastRangNum(j, columnValues.get(j), columnValues);
int lastColumn = getLastRangNum(i, columnValues.get(j), getHeadByRowNum(j)); int lastColumn = getLastRangNum(i, columnValues.get(j), getHeadByRowNum(j));
@ -168,8 +238,9 @@ public class ExcelHeadProperty {
} }
public List<String> getHeadByRowNum(int rowNum) { public List<String> getHeadByRowNum(int rowNum) {
List<String> l = new ArrayList<String>(head.size()); List<String> l = new ArrayList<String>(headList.size());
for (List<String> list : head) { for (Head head : headList) {
List<String> list = head.getHeadNames();
if (list.size() > rowNum) { if (list.size() > rowNum) {
l.add(list.get(rowNum)); l.add(list.get(rowNum));
} else { } else {
@ -182,9 +253,12 @@ public class ExcelHeadProperty {
/** /**
* Get the last consecutive string position * Get the last consecutive string position
* *
* @param j current value position * @param j
* @param value value content * current value position
* @param values values * @param value
* value content
* @param values
* values
* @return the last consecutive string position * @return the last consecutive string position
*/ */
private int getLastRangNum(int j, String value, List<String> values) { private int getLastRangNum(int j, String value, List<String> values) {
@ -210,27 +284,5 @@ public class ExcelHeadProperty {
} }
} }
return last; return last;
}
public int getRowNum() {
int headRowNum = 0;
for (List<String> list : head) {
if (list != null && list.size() > 0) {
if (list.size() > headRowNum) {
headRowNum = list.size();
}
}
}
return headRowNum;
}
public static ExcelHeadProperty buildExcelHeadProperty(ExcelHeadProperty excelHeadProperty, Class<? extends BaseRowModel> clazz, List<String> headOneRow) {
if (excelHeadProperty == null && (clazz != null || headOneRow != null)) {
excelHeadProperty = new ExcelHeadProperty(clazz, new ArrayList<List<String>>());
}
if (excelHeadProperty.getHead() == null && headOneRow != null) {
excelHeadProperty.appendOneRow(headOneRow);
}
return excelHeadProperty;
} }
} }

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

@ -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);
}
}
}

28
src/main/java/com/alibaba/excel/metadata/Sheet.java

@ -4,6 +4,13 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.poi.ss.usermodel.TableStyle;
import com.alibaba.excel.write.style.CellStyleStrategy;
import com.alibaba.excel.write.style.column.ColumnWihtStyleStrategy;
import com.alibaba.excel.write.style.column.ColumnWithStyleStrategy;
import com.oracle.webservices.internal.api.databinding.DatabindingMode;
/** /**
* *
* @author jipengfei * @author jipengfei
@ -34,11 +41,13 @@ public class Sheet {
/** /**
* *
*/ */
@Deprecated
private TableStyle tableStyle; private TableStyle tableStyle;
/** /**
* column with * column with
*/ */
@Deprecated
private Map<Integer,Integer> columnWidthMap = new HashMap<Integer, Integer>(); private Map<Integer,Integer> columnWidthMap = new HashMap<Integer, Integer>();
/** /**
@ -51,6 +60,17 @@ public class Sheet {
*/ */
private int startRow = 0; private int startRow = 0;
private CellStyleStrategy cellStyleStrategy;
private ColumnWithStyleStrategy columnWithStyleStrategy;
public ColumnWithStyleStrategy getColumnWithStyleStrategy() {
return columnWithStyleStrategy;
}
public void setColumnWithStyleStrategy(ColumnWithStyleStrategy columnWithStyleStrategy) {
this.columnWithStyleStrategy = columnWithStyleStrategy;
}
public Sheet(int sheetNo) { public Sheet(int sheetNo) {
this.sheetNo = sheetNo; this.sheetNo = sheetNo;
@ -165,4 +185,12 @@ public class Sheet {
public void setStartRow(int startRow) { public void setStartRow(int startRow) {
this.startRow = startRow; this.startRow = startRow;
} }
public CellStyleStrategy getCellStyleStrategy() {
return cellStyleStrategy;
}
public void setCellStyleStrategy(CellStyleStrategy cellStyleStrategy) {
this.cellStyleStrategy = cellStyleStrategy;
}
} }

104
src/main/java/com/alibaba/excel/metadata/SheetHolder.java

@ -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;
}
}

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

@ -2,6 +2,8 @@ package com.alibaba.excel.metadata;
import java.util.List; import java.util.List;
import com.alibaba.excel.write.style.CellStyleStrategy;
/** /**
* @author jipengfei * @author jipengfei
*/ */
@ -20,8 +22,19 @@ public class Table {
/** /**
*/ */
@Deprecated
private TableStyle tableStyle; private TableStyle tableStyle;
private CellStyleStrategy cellStyleStrategy;
public CellStyleStrategy getCellStyleStrategy() {
return cellStyleStrategy;
}
public void setCellStyleStrategy(CellStyleStrategy cellStyleStrategy) {
this.cellStyleStrategy = cellStyleStrategy;
}
public TableStyle getTableStyle() { public TableStyle getTableStyle() {
return tableStyle; return tableStyle;
} }

89
src/main/java/com/alibaba/excel/metadata/TableHolder.java

@ -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;
}
}

61
src/main/java/com/alibaba/excel/metadata/TableStyle.java

@ -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;
}
}

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

@ -1,14 +1,38 @@
package com.alibaba.excel.util; package com.alibaba.excel.util;
import org.apache.poi.ss.usermodel.*;
import java.util.Map; import java.util.Map;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
/** /**
* @author jipengfei * @author jipengfei
*/ */
public class StyleUtil { public class StyleUtil {
public static void defaultCellStyle(Workbook workbook, CellStyle newCellStyle) {
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);
newCellStyle.setLocked(true);
newCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
newCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
newCellStyle.setBorderBottom(BorderStyle.THIN);
newCellStyle.setBorderLeft(BorderStyle.THIN);
}
/** /**
* *
* @param workbook * @param workbook
@ -40,7 +64,7 @@ public class StyleUtil {
* @return * @return
*/ */
public static CellStyle buildCellStyle(Workbook workbook, com.alibaba.excel.metadata.Font f, public static CellStyle buildCellStyle(Workbook workbook, com.alibaba.excel.metadata.Font f,
IndexedColors indexedColors) { IndexedColors indexedColors) {
CellStyle cellStyle = buildDefaultCellStyle(workbook); CellStyle cellStyle = buildDefaultCellStyle(workbook);
if (f != null) { if (f != null) {
Font font = workbook.createFont(); Font font = workbook.createFont();
@ -55,7 +79,18 @@ public class StyleUtil {
return cellStyle; return cellStyle;
} }
public static Sheet buildSheetStyle(Sheet currentSheet, Map<Integer, Integer> sheetWidthMap){ /**
* Convert {@link com.alibaba.excel.metadata.CellStyle} to {@link CellStyle}
*
* @param workbook
* @param cellStyle
* @return
*/
public static CellStyle buildCellStyle(Workbook workbook, com.alibaba.excel.metadata.CellStyle cellStyle) {
return buildCellStyle(workbook, cellStyle.getSimpleFont(), cellStyle.getIndexedColors());
}
public static Sheet buildSheetStyle(Sheet currentSheet, Map<Integer, Integer> sheetWidthMap) {
currentSheet.setDefaultColumnWidth(20); currentSheet.setDefaultColumnWidth(20);
for (Map.Entry<Integer, Integer> entry : sheetWidthMap.entrySet()) { for (Map.Entry<Integer, Integer> entry : sheetWidthMap.entrySet()) {
currentSheet.setColumnWidth(entry.getKey(), entry.getValue()); currentSheet.setColumnWidth(entry.getKey(), entry.getValue());
@ -63,5 +98,4 @@ public class StyleUtil {
return currentSheet; return currentSheet;
} }
} }

1
src/main/java/com/alibaba/excel/write/ExcelBuilder.java

@ -2,6 +2,7 @@ package com.alibaba.excel.write;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table; import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.write.merge.MergeStrategy;
import java.util.List; import java.util.List;

4
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java

@ -24,8 +24,8 @@ import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.POITempFile; import com.alibaba.excel.util.POITempFile;
import com.alibaba.excel.util.TypeUtil;
import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.write.merge.MergeStrategy;
import net.sf.cglib.beans.BeanMap; import net.sf.cglib.beans.BeanMap;
@ -34,6 +34,7 @@ import net.sf.cglib.beans.BeanMap;
*/ */
public class ExcelBuilderImpl implements ExcelBuilder, ConverterRegistryCenter { public class ExcelBuilderImpl implements ExcelBuilder, ConverterRegistryCenter {
private WriteContext context; private WriteContext context;
private final List<Converter> converters = new ArrayList<Converter>(); private final List<Converter> converters = new ArrayList<Converter>();
@ -89,6 +90,7 @@ public class ExcelBuilderImpl implements ExcelBuilder, ConverterRegistryCenter {
addContent(data, sheetParam.getStartRow()); addContent(data, sheetParam.getStartRow());
} }
@Override @Override
public void merge(List<MergeStrategy> strategies) { public void merge(List<MergeStrategy> strategies) {
if (strategies != null) { if (strategies != null) {

30
src/main/java/com/alibaba/excel/write/ExcelWriterBuilder.java

@ -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);
}
}

8
src/main/java/com/alibaba/excel/write/MergeStrategy.java

@ -1,8 +0,0 @@
package com.alibaba.excel.write;
public interface MergeStrategy {
int getFirstRow();
int getLastRow();
int getFirstCol();
int getLastCol();
}

33
src/main/java/com/alibaba/excel/write/handler/CellExcelWriteHandler.java

@ -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);
}

10
src/main/java/com/alibaba/excel/write/handler/ExcelWriteHandler.java

@ -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 {}

31
src/main/java/com/alibaba/excel/write/handler/RowExcelWriteHandler.java

@ -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);
}

27
src/main/java/com/alibaba/excel/write/handler/SheetExcelWriteHandler.java

@ -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);
}

25
src/main/java/com/alibaba/excel/write/handler/WookbookExcelWriteHandler.java

@ -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);
}

28
src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java

@ -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);
}

34
src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java

@ -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);
}
}
}

39
src/main/java/com/alibaba/excel/write/merge/OnceAbsoluteMergeStrategy.java

@ -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);
}
}
}

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

@ -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);
}

65
src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java

@ -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);
}

57
src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java

@ -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()));
}
}

41
src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java

@ -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);
}

26
src/main/java/com/alibaba/excel/write/style/column/AbstractHeadColumnWidthStyleStrategy.java

@ -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);
}

24
src/main/java/com/alibaba/excel/write/style/column/SimpleColumnWidthStyleStrategy.java

@ -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);
}
}

31
src/main/java/com/alibaba/excel/write/style/row/AbstractRowHighStyleStrategy.java

@ -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);
}

29
src/main/java/com/alibaba/excel/write/style/row/SimpleRowHighStyleStrategy.java

@ -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);
}
}

4
src/test/java/com/alibaba/easyexcel/test/WriteTest.java

@ -25,13 +25,13 @@ import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table; import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.MergeStrategy; import com.alibaba.excel.write.merge.MergeStrategy;
public class WriteTest { public class WriteTest {
@Test @Test
public void writeV2007() throws IOException { public void writeV2007() throws IOException {
OutputStream out = new FileOutputStream(new File(FileUtil.getPath(), "write_2007.xlsx")); OutputStream out = new FileOutputStream(new File(FileUtil.getPath(), "write_2007"+System.currentTimeMillis()+".xlsx"));
ExcelWriter writer = EasyExcelFactory.getWriter(out); ExcelWriter writer = EasyExcelFactory.getWriter(out);
// 写第一个sheet, sheet1 数据全是List<String> 无模型映射关系 // 写第一个sheet, sheet1 数据全是List<String> 无模型映射关系
Sheet sheet1 = new Sheet(1, 3); Sheet sheet1 = new Sheet(1, 3);

Loading…
Cancel
Save