|
|
@ -1,8 +1,6 @@ |
|
|
|
package com.alibaba.excel.context; |
|
|
|
package com.alibaba.excel.context; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
|
|
|
|
import java.io.OutputStream; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.HashSet; |
|
|
@ -15,13 +13,11 @@ import org.apache.poi.ss.usermodel.Cell; |
|
|
|
import org.apache.poi.ss.usermodel.IndexedColors; |
|
|
|
import org.apache.poi.ss.usermodel.IndexedColors; |
|
|
|
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 org.apache.poi.ss.usermodel.Workbook; |
|
|
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress; |
|
|
|
import org.apache.poi.ss.util.CellRangeAddress; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.excel.converters.Converter; |
|
|
|
import com.alibaba.excel.converters.Converter; |
|
|
|
import com.alibaba.excel.converters.ConverterRegistryCenter; |
|
|
|
|
|
|
|
import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter; |
|
|
|
import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter; |
|
|
|
import com.alibaba.excel.converters.date.DateStringConverter; |
|
|
|
import com.alibaba.excel.converters.date.DateStringConverter; |
|
|
|
import com.alibaba.excel.event.NotRepeatExecutor; |
|
|
|
import com.alibaba.excel.event.NotRepeatExecutor; |
|
|
@ -32,14 +28,15 @@ import com.alibaba.excel.metadata.ExcelHeadProperty; |
|
|
|
import com.alibaba.excel.metadata.Font; |
|
|
|
import com.alibaba.excel.metadata.Font; |
|
|
|
import com.alibaba.excel.metadata.Head; |
|
|
|
import com.alibaba.excel.metadata.Head; |
|
|
|
import com.alibaba.excel.metadata.Table; |
|
|
|
import com.alibaba.excel.metadata.Table; |
|
|
|
|
|
|
|
import com.alibaba.excel.metadata.TableStyle; |
|
|
|
import com.alibaba.excel.metadata.holder.ConfigurationSelector; |
|
|
|
import com.alibaba.excel.metadata.holder.ConfigurationSelector; |
|
|
|
import com.alibaba.excel.metadata.holder.SheetHolder; |
|
|
|
import com.alibaba.excel.metadata.holder.SheetHolder; |
|
|
|
import com.alibaba.excel.metadata.holder.TableHolder; |
|
|
|
import com.alibaba.excel.metadata.holder.TableHolder; |
|
|
|
import com.alibaba.excel.metadata.holder.WorkbookHolder; |
|
|
|
import com.alibaba.excel.metadata.holder.WorkbookHolder; |
|
|
|
import com.alibaba.excel.support.ExcelTypeEnum; |
|
|
|
|
|
|
|
import com.alibaba.excel.util.WorkBookUtil; |
|
|
|
import com.alibaba.excel.util.WorkBookUtil; |
|
|
|
import com.alibaba.excel.write.handler.WriteHandler; |
|
|
|
import com.alibaba.excel.write.handler.WriteHandler; |
|
|
|
import com.alibaba.excel.write.style.RowCellStyleStrategy; |
|
|
|
import com.alibaba.excel.write.style.RowCellStyleStrategy; |
|
|
|
|
|
|
|
import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy; |
|
|
|
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; |
|
|
|
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -67,30 +64,17 @@ public class WriteContextImpl implements WriteContext { |
|
|
|
* Configuration of currently operated cell |
|
|
|
* Configuration of currently operated cell |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private ConfigurationSelector currentConfigurationSelector; |
|
|
|
private ConfigurationSelector currentConfigurationSelector; |
|
|
|
/** |
|
|
|
|
|
|
|
* Excel type |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private ExcelTypeEnum excelType; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Final output stream |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private OutputStream outputStream; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Final output stream |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private InputStream templateInputStream; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public WriteContextImpl(InputStream templateInputStream, OutputStream outputStream, ExcelTypeEnum excelType, |
|
|
|
public WriteContextImpl(com.alibaba.excel.metadata.Workbook workbook) { |
|
|
|
Boolean needHead, Map<Class, Converter> customConverterMap, List<WriteHandler> customWriteHandlerList) { |
|
|
|
if (workbook == null) { |
|
|
|
|
|
|
|
throw new IllegalArgumentException("Workbook argument cannot be null"); |
|
|
|
|
|
|
|
} |
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
LOGGER.debug("Begin to Initialization 'WriteContextImpl'"); |
|
|
|
LOGGER.debug("Begin to Initialization 'WriteContextImpl'"); |
|
|
|
} |
|
|
|
} |
|
|
|
initCurrentWorkbookHolder(needHead, customConverterMap, customWriteHandlerList); |
|
|
|
initCurrentWorkbookHolder(workbook); |
|
|
|
this.excelType = excelType; |
|
|
|
|
|
|
|
this.outputStream = outputStream; |
|
|
|
|
|
|
|
this.templateInputStream = templateInputStream; |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
currentWorkbookHolder.setWorkbook(WorkBookUtil.createWorkBook(templateInputStream, excelType)); |
|
|
|
currentWorkbookHolder.setWorkbook(WorkBookUtil.createWorkBook(workbook)); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
throw new ExcelGenerateException("Create workbook failure", e); |
|
|
|
throw new ExcelGenerateException("Create workbook failure", e); |
|
|
|
} |
|
|
|
} |
|
|
@ -99,23 +83,30 @@ public class WriteContextImpl implements WriteContext { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void initCurrentWorkbookHolder(Boolean needHead, Map<Class, Converter> customConverterMap, |
|
|
|
private void initCurrentWorkbookHolder(com.alibaba.excel.metadata.Workbook workbook) { |
|
|
|
List<WriteHandler> customWriteHandlerList) { |
|
|
|
|
|
|
|
currentWorkbookHolder = new WorkbookHolder(); |
|
|
|
currentWorkbookHolder = new WorkbookHolder(); |
|
|
|
if (needHead == null) { |
|
|
|
currentWorkbookHolder.setWorkbookParam(workbook); |
|
|
|
|
|
|
|
currentWorkbookHolder.setTemplateInputStream(workbook.getTemplateInputStream()); |
|
|
|
|
|
|
|
currentWorkbookHolder.setOutputStream(workbook.getOutputStream()); |
|
|
|
|
|
|
|
if (workbook.getNeedHead() == null) { |
|
|
|
currentWorkbookHolder.setNeedHead(Boolean.TRUE); |
|
|
|
currentWorkbookHolder.setNeedHead(Boolean.TRUE); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
currentWorkbookHolder.setNeedHead(needHead); |
|
|
|
currentWorkbookHolder.setNeedHead(workbook.getNeedHead()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (workbook.getWriteRelativeHeadRowIndex() == null) { |
|
|
|
|
|
|
|
currentWorkbookHolder.setWriteRelativeHeadRowIndex(0); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
currentWorkbookHolder.setWriteRelativeHeadRowIndex(workbook.getWriteRelativeHeadRowIndex()); |
|
|
|
} |
|
|
|
} |
|
|
|
List<WriteHandler> handlerList = new ArrayList<WriteHandler>(); |
|
|
|
List<WriteHandler> handlerList = new ArrayList<WriteHandler>(); |
|
|
|
if (customWriteHandlerList != null && !customWriteHandlerList.isEmpty()) { |
|
|
|
if (workbook.getCustomWriteHandlerList() != null && !workbook.getCustomWriteHandlerList().isEmpty()) { |
|
|
|
handlerList.addAll(customWriteHandlerList); |
|
|
|
handlerList.addAll(workbook.getCustomWriteHandlerList()); |
|
|
|
} |
|
|
|
} |
|
|
|
handlerList.addAll(loadDefaultHandler()); |
|
|
|
handlerList.addAll(loadDefaultHandler()); |
|
|
|
currentWorkbookHolder.setWriteHandlerList(sortAndClearUpHandler(handlerList)); |
|
|
|
currentWorkbookHolder.setWriteHandlerList(sortAndClearUpHandler(handlerList)); |
|
|
|
Map<Class, Converter> converterMap = loadDefaultConverter(); |
|
|
|
Map<Class, Converter> converterMap = loadDefaultConverter(); |
|
|
|
if (customConverterMap != null && !customConverterMap.isEmpty()) { |
|
|
|
if (workbook.getCustomConverterMap() != null && !workbook.getCustomConverterMap().isEmpty()) { |
|
|
|
converterMap.putAll(customConverterMap); |
|
|
|
converterMap.putAll(workbook.getCustomConverterMap()); |
|
|
|
} |
|
|
|
} |
|
|
|
currentWorkbookHolder.setConverterMap(converterMap); |
|
|
|
currentWorkbookHolder.setConverterMap(converterMap); |
|
|
|
currentWorkbookHolder.setHasBeenInitializedSheet(new HashMap<Integer, SheetHolder>()); |
|
|
|
currentWorkbookHolder.setHasBeenInitializedSheet(new HashMap<Integer, SheetHolder>()); |
|
|
@ -127,8 +118,7 @@ public class WriteContextImpl implements WriteContext { |
|
|
|
|
|
|
|
|
|
|
|
private List<WriteHandler> sortAndClearUpHandler(List<WriteHandler> handlerList) { |
|
|
|
private List<WriteHandler> sortAndClearUpHandler(List<WriteHandler> handlerList) { |
|
|
|
// sort
|
|
|
|
// sort
|
|
|
|
Map<Integer, List<WriteHandler>> orderExcelWriteHandlerMap = |
|
|
|
Map<Integer, List<WriteHandler>> orderExcelWriteHandlerMap = new TreeMap<Integer, List<WriteHandler>>(); |
|
|
|
new TreeMap<Integer, List<WriteHandler>>(); |
|
|
|
|
|
|
|
for (WriteHandler handler : handlerList) { |
|
|
|
for (WriteHandler handler : handlerList) { |
|
|
|
int order = Integer.MIN_VALUE; |
|
|
|
int order = Integer.MIN_VALUE; |
|
|
|
if (handler instanceof Order) { |
|
|
|
if (handler instanceof Order) { |
|
|
@ -198,6 +188,7 @@ public class WriteContextImpl implements WriteContext { |
|
|
|
LOGGER.debug("Sheet:{} is already existed", sheet.getSheetNo()); |
|
|
|
LOGGER.debug("Sheet:{} is already existed", sheet.getSheetNo()); |
|
|
|
} |
|
|
|
} |
|
|
|
currentSheetHolder = currentWorkbookHolder.getHasBeenInitializedSheet().get(sheet.getSheetNo()); |
|
|
|
currentSheetHolder = currentWorkbookHolder.getHasBeenInitializedSheet().get(sheet.getSheetNo()); |
|
|
|
|
|
|
|
currentTableHolder = null; |
|
|
|
currentConfigurationSelector = currentSheetHolder; |
|
|
|
currentConfigurationSelector = currentSheetHolder; |
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
LOGGER.debug("CurrentConfigurationSelector is currentSheetHolder"); |
|
|
|
LOGGER.debug("CurrentConfigurationSelector is currentSheetHolder"); |
|
|
@ -206,12 +197,13 @@ public class WriteContextImpl implements WriteContext { |
|
|
|
} |
|
|
|
} |
|
|
|
initCurrentSheetHolder(sheet); |
|
|
|
initCurrentSheetHolder(sheet); |
|
|
|
// Initialization current sheet
|
|
|
|
// Initialization current sheet
|
|
|
|
initCurrentSheet(sheet); |
|
|
|
initSheet(sheet); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void initCurrentSheetHolder(com.alibaba.excel.metadata.Sheet sheet) { |
|
|
|
private void initCurrentSheetHolder(com.alibaba.excel.metadata.Sheet sheet) { |
|
|
|
currentSheetHolder = new SheetHolder(); |
|
|
|
currentSheetHolder = new SheetHolder(); |
|
|
|
currentSheetHolder.setSheetParam(sheet); |
|
|
|
currentSheetHolder.setSheetParam(sheet); |
|
|
|
|
|
|
|
currentSheetHolder.setParentWorkBook(currentWorkbookHolder); |
|
|
|
if (sheet.getNeedHead() == null) { |
|
|
|
if (sheet.getNeedHead() == null) { |
|
|
|
currentSheetHolder.setNeedHead(currentConfigurationSelector.needHead()); |
|
|
|
currentSheetHolder.setNeedHead(currentConfigurationSelector.needHead()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -220,30 +212,66 @@ public class WriteContextImpl implements WriteContext { |
|
|
|
if (sheet.getWriteRelativeHeadRowIndex() == null) { |
|
|
|
if (sheet.getWriteRelativeHeadRowIndex() == null) { |
|
|
|
currentSheetHolder.setWriteRelativeHeadRowIndex(currentConfigurationSelector.writeRelativeHeadRowIndex()); |
|
|
|
currentSheetHolder.setWriteRelativeHeadRowIndex(currentConfigurationSelector.writeRelativeHeadRowIndex()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
currentSheetHolder.setNeedHead(sheet.getWriteRelativeHeadRowIndex()); |
|
|
|
currentSheetHolder.setWriteRelativeHeadRowIndex(sheet.getWriteRelativeHeadRowIndex()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Compatible with old code
|
|
|
|
|
|
|
|
compatibleOldCode(sheet); |
|
|
|
List<WriteHandler> handlerList = new ArrayList<WriteHandler>(); |
|
|
|
List<WriteHandler> handlerList = new ArrayList<WriteHandler>(); |
|
|
|
if (sheet.getCustomWriteHandlerList() != null && !sheet.getCustomWriteHandlerList().isEmpty()) { |
|
|
|
if (sheet.getCustomWriteHandlerList() != null && !sheet.getCustomWriteHandlerList().isEmpty()) { |
|
|
|
handlerList.addAll(sheet.getCustomWriteHandlerList()); |
|
|
|
handlerList.addAll(sheet.getCustomWriteHandlerList()); |
|
|
|
} |
|
|
|
} |
|
|
|
handlerList.addAll(currentConfigurationSelector.writeHandlerList()); |
|
|
|
handlerList.addAll(currentConfigurationSelector.writeHandlerList()); |
|
|
|
currentSheetHolder.setWriteHandlerList(sortAndClearUpHandler(handlerList)); |
|
|
|
currentSheetHolder.setWriteHandlerList(sortAndClearUpHandler(handlerList)); |
|
|
|
|
|
|
|
|
|
|
|
Map<Class, Converter> converterMap = new HashMap<Class, Converter>(currentConfigurationSelector.converterMap()); |
|
|
|
Map<Class, Converter> converterMap = new HashMap<Class, Converter>(currentConfigurationSelector.converterMap()); |
|
|
|
if (sheet.getCustomConverterMap() != null && !sheet.getCustomConverterMap().isEmpty()) { |
|
|
|
if (sheet.getCustomConverterMap() != null && !sheet.getCustomConverterMap().isEmpty()) { |
|
|
|
converterMap.putAll(sheet.getCustomConverterMap()); |
|
|
|
converterMap.putAll(sheet.getCustomConverterMap()); |
|
|
|
} |
|
|
|
} |
|
|
|
currentWorkbookHolder.setConverterMap(converterMap); |
|
|
|
currentSheetHolder.setConverterMap(converterMap); |
|
|
|
currentSheetHolder.setHasBeenInitializedTable(new HashMap<Integer, TableHolder>()); |
|
|
|
currentSheetHolder.setHasBeenInitializedTable(new HashMap<Integer, TableHolder>()); |
|
|
|
currentWorkbookHolder.getHasBeenInitializedSheet().put(sheet.getSheetNo(), currentSheetHolder); |
|
|
|
currentWorkbookHolder.getHasBeenInitializedSheet().put(sheet.getSheetNo(), currentSheetHolder); |
|
|
|
|
|
|
|
currentTableHolder = null; |
|
|
|
currentConfigurationSelector = currentSheetHolder; |
|
|
|
currentConfigurationSelector = currentSheetHolder; |
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
LOGGER.debug("CurrentConfigurationSelector is currentSheetHolder"); |
|
|
|
LOGGER.debug("CurrentConfigurationSelector is currentSheetHolder"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void initCurrentSheet(com.alibaba.excel.metadata.Sheet sheet) { |
|
|
|
/** |
|
|
|
|
|
|
|
* Compatible with old code |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Deprecated |
|
|
|
|
|
|
|
private void compatibleOldCode(com.alibaba.excel.metadata.Sheet sheet) { |
|
|
|
|
|
|
|
if (sheet.getColumnWidthMap() != null && !sheet.getColumnWidthMap().isEmpty()) { |
|
|
|
|
|
|
|
final Map<Integer, Integer> columnWidthMap = sheet.getColumnWidthMap(); |
|
|
|
|
|
|
|
if (sheet.getCustomWriteHandlerList() == null) { |
|
|
|
|
|
|
|
sheet.setCustomWriteHandlerList(new ArrayList<WriteHandler>()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
sheet.getCustomWriteHandlerList().add(new AbstractHeadColumnWidthStyleStrategy() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
protected int columnWidth(Head head) { |
|
|
|
|
|
|
|
if (columnWidthMap.containsKey(head.getColumnIndex())) { |
|
|
|
|
|
|
|
columnWidthMap.get(head.getColumnIndex()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return 20; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (sheet.getTableStyle() != null) { |
|
|
|
|
|
|
|
final TableStyle tableStyle = sheet.getTableStyle(); |
|
|
|
|
|
|
|
if (sheet.getCustomWriteHandlerList() == null) { |
|
|
|
|
|
|
|
sheet.setCustomWriteHandlerList(new ArrayList<WriteHandler>()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
CellStyle headCellStyle = new CellStyle(); |
|
|
|
|
|
|
|
headCellStyle.setFont(tableStyle.getTableHeadFont()); |
|
|
|
|
|
|
|
headCellStyle.setIndexedColors(tableStyle.getTableContentBackGroundColor()); |
|
|
|
|
|
|
|
CellStyle contentCellStyle = new CellStyle(); |
|
|
|
|
|
|
|
contentCellStyle.setFont(tableStyle.getTableContentFont()); |
|
|
|
|
|
|
|
contentCellStyle.setIndexedColors(tableStyle.getTableContentBackGroundColor()); |
|
|
|
|
|
|
|
sheet.getCustomWriteHandlerList().add(new RowCellStyleStrategy(headCellStyle, contentCellStyle)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void initSheet(com.alibaba.excel.metadata.Sheet sheet) { |
|
|
|
Sheet currentSheet; |
|
|
|
Sheet currentSheet; |
|
|
|
try { |
|
|
|
try { |
|
|
|
currentSheet = currentWorkbookHolder.getWorkbook().getSheetAt(sheet.getSheetNo()); |
|
|
|
currentSheet = currentWorkbookHolder.getWorkbook().getSheetAt(sheet.getSheetNo()); |
|
|
@ -257,57 +285,26 @@ public class WriteContextImpl implements WriteContext { |
|
|
|
// Initialization head
|
|
|
|
// Initialization head
|
|
|
|
currentSheetHolder.setExcelHeadProperty(new ExcelHeadProperty(sheet.getClazz(), sheet.getHead())); |
|
|
|
currentSheetHolder.setExcelHeadProperty(new ExcelHeadProperty(sheet.getClazz(), sheet.getHead())); |
|
|
|
// Initialization head
|
|
|
|
// Initialization head
|
|
|
|
initHead(); |
|
|
|
initHead(currentSheetHolder.getExcelHeadProperty()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void initHead(ExcelHeadProperty excelHeadPropert) { |
|
|
|
public void initHead(ExcelHeadProperty excelHeadProperty) { |
|
|
|
if (!currentConfigurationSelector.needHead() || !currentSheetHolder.getExcelHeadProperty().hasHead()) { |
|
|
|
if (!currentConfigurationSelector.needHead() || !currentSheetHolder.getExcelHeadProperty().hasHead()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
int startRow = getCurrentSheet().getLastRowNum(); |
|
|
|
int startRow = currentSheetHolder.getSheet().getLastRowNum(); |
|
|
|
startRow += currentConfigurationSelector.writeRelativeHeadRowIndex(); |
|
|
|
startRow += currentConfigurationSelector.writeRelativeHeadRowIndex(); |
|
|
|
// Combined head
|
|
|
|
// Combined head
|
|
|
|
addMergedRegionToCurrentSheet(startRow); |
|
|
|
addMergedRegionToCurrentSheet(excelHeadProperty, startRow); |
|
|
|
for (int i = startRow; i < currentSheetHolder.getExcelHeadProperty().getHeadRowNumber() + startRow; i++) { |
|
|
|
for (int i = startRow; i < excelHeadProperty.getHeadRowNumber() + startRow; i++) { |
|
|
|
Row row = WorkBookUtil.createRow(getCurrentSheet(), i); |
|
|
|
Row row = WorkBookUtil.createRow(currentSheetHolder.getSheet(), i); |
|
|
|
// Set the row height of the header
|
|
|
|
addOneRowOfHeadDataToExcel(row, i, excelHeadProperty.getHeadList()); |
|
|
|
currentSheetHolder.getRowHighStyleStrategy().headColumnHigh(i); |
|
|
|
|
|
|
|
if (null != writeHandler) { |
|
|
|
|
|
|
|
this.writeHandler.row(i, row); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
addOneRowOfHeadDataToExcel(row, i, currentSheetHolder.getExcelHeadProperty().getHeadList()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void initTableHead() { |
|
|
|
|
|
|
|
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(ExcelHeadProperty excelHeadProperty, int startRow) { |
|
|
|
for (com.alibaba.excel.metadata.CellRange cellRangeModel : currentSheetHolder.getExcelHeadProperty() |
|
|
|
for (com.alibaba.excel.metadata.CellRange cellRangeModel : excelHeadProperty.getCellRangeModels()) { |
|
|
|
.getCellRangeModels()) { |
|
|
|
currentSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(cellRangeModel.getFirstRow() + startRow, |
|
|
|
currentSheet.addMergedRegion(new CellRangeAddress(cellRangeModel.getFirstRow() + startRow, |
|
|
|
|
|
|
|
cellRangeModel.getLastRow() + startRow, cellRangeModel.getFirstCol(), cellRangeModel.getLastCol())); |
|
|
|
cellRangeModel.getLastRow() + startRow, cellRangeModel.getFirstCol(), cellRangeModel.getLastCol())); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -315,127 +312,121 @@ public class WriteContextImpl implements WriteContext { |
|
|
|
private void addOneRowOfHeadDataToExcel(Row row, int rowIndex, List<Head> headList) { |
|
|
|
private void addOneRowOfHeadDataToExcel(Row row, int rowIndex, List<Head> headList) { |
|
|
|
for (int i = 0; i < headList.size(); i++) { |
|
|
|
for (int i = 0; i < headList.size(); i++) { |
|
|
|
Head head = headList.get(i); |
|
|
|
Head head = headList.get(i); |
|
|
|
Cell cell = WorkBookUtil.createCell(row, i, |
|
|
|
// TODO 创建cell
|
|
|
|
currentSheetHolder.getCellStyleStrategy().headCellStyle(rowIndex, headList.get(i)), |
|
|
|
Cell cell = WorkBookUtil.createCell(row, i, null, head.getHeadName(i)); |
|
|
|
head.getHeadName(i)); |
|
|
|
|
|
|
|
if (null != writeHandler) { |
|
|
|
|
|
|
|
this.writeHandler.cell(i, cell); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void cleanCurrentTable() { |
|
|
|
|
|
|
|
this.excelHeadProperty = null; |
|
|
|
|
|
|
|
this.currentHeadCellStyle = null; |
|
|
|
|
|
|
|
this.currentContentCellStyle = null; |
|
|
|
|
|
|
|
this.currentTable = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void currentTable(Table table) { |
|
|
|
public void currentTable(Table table) { |
|
|
|
if (table == null) { |
|
|
|
if (table == null) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
if (currentSheetHolder == null) { |
|
|
|
if (table.getTableNo() == null || table.getTableNo() <= 0) { |
|
|
|
throw new IllegalStateException("Must first call WriteContextImpl#currentSheet"); |
|
|
|
table.setTableNo(0); |
|
|
|
} |
|
|
|
} |
|
|
|
if(currentSheetHolder.getHasBeenInitializedTable().containsKey(table.getTableNo())){ |
|
|
|
if (currentSheetHolder.getHasBeenInitializedTable().containsKey(table.getTableNo())) { |
|
|
|
currentTableHolder=currentSheetHolder.getHasBeenInitializedTable().get(table.getTableNo()); |
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
|
|
|
|
LOGGER.debug("Table:{} is already existed", table.getTableNo()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
currentTableHolder = currentSheetHolder.getHasBeenInitializedTable().get(table.getTableNo()); |
|
|
|
|
|
|
|
currentConfigurationSelector = currentTableHolder; |
|
|
|
|
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
|
|
|
|
LOGGER.debug("CurrentConfigurationSelector is currentTableHolder"); |
|
|
|
|
|
|
|
} |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
initCurrentTableHolder(table); |
|
|
|
|
|
|
|
// Initialization head
|
|
|
|
|
|
|
|
currentTableHolder.setExcelHeadProperty(new ExcelHeadProperty(table.getClazz(), table.getHead())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initHead(currentTableHolder.getExcelHeadProperty()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void initCurrentTableHolder(com.alibaba.excel.metadata.Table table) { |
|
|
|
|
|
|
|
currentTableHolder = new TableHolder(); |
|
|
|
currentTable.getTableNo() |
|
|
|
currentTableHolder.setTableParam(table); |
|
|
|
|
|
|
|
currentTableHolder.setParentSheet(currentSheetHolder); |
|
|
|
|
|
|
|
currentTableHolder.setTableNo(table.getTableNo()); |
|
|
|
if (null == currentTable || currentTable.getTableNo() != table.getTableNo()) { |
|
|
|
if (table.getNeedHead() == null) { |
|
|
|
cleanCurrentTable(); |
|
|
|
currentTableHolder.setNeedHead(currentConfigurationSelector.needHead()); |
|
|
|
this.currentTable = table; |
|
|
|
} else { |
|
|
|
this.initExcelHeadProperty(table.getHead(), table.getClazz()); |
|
|
|
currentTableHolder.setNeedHead(table.getNeedHead()); |
|
|
|
this.initTableStyle(table.getTableStyle()); |
|
|
|
} |
|
|
|
this.initTableHead(); |
|
|
|
if (table.getWriteRelativeHeadRowIndex() == null) { |
|
|
|
|
|
|
|
currentTableHolder.setWriteRelativeHeadRowIndex(currentConfigurationSelector.writeRelativeHeadRowIndex()); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
currentTableHolder.setWriteRelativeHeadRowIndex(table.getWriteRelativeHeadRowIndex()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Compatible with old code
|
|
|
|
|
|
|
|
compatibleOldCode(table); |
|
|
|
|
|
|
|
List<WriteHandler> handlerList = new ArrayList<WriteHandler>(); |
|
|
|
|
|
|
|
if (table.getCustomWriteHandlerList() != null && !table.getCustomWriteHandlerList().isEmpty()) { |
|
|
|
|
|
|
|
handlerList.addAll(table.getCustomWriteHandlerList()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
handlerList.addAll(currentConfigurationSelector.writeHandlerList()); |
|
|
|
|
|
|
|
currentTableHolder.setWriteHandlerList(sortAndClearUpHandler(handlerList)); |
|
|
|
|
|
|
|
Map<Class, Converter> converterMap = new HashMap<Class, Converter>(currentConfigurationSelector.converterMap()); |
|
|
|
|
|
|
|
if (table.getCustomConverterMap() != null && !table.getCustomConverterMap().isEmpty()) { |
|
|
|
|
|
|
|
converterMap.putAll(table.getCustomConverterMap()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
currentTableHolder.setConverterMap(converterMap); |
|
|
|
|
|
|
|
currentConfigurationSelector = currentTableHolder; |
|
|
|
|
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
|
|
|
|
LOGGER.debug("CurrentConfigurationSelector is currentTableHolder"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
/** |
|
|
|
public ExcelHeadProperty getExcelHeadProperty() { |
|
|
|
* Compatible with old code |
|
|
|
return currentSheetHolder.getExcelHeadProperty(); |
|
|
|
*/ |
|
|
|
|
|
|
|
@Deprecated |
|
|
|
|
|
|
|
private void compatibleOldCode(com.alibaba.excel.metadata.Table table) { |
|
|
|
|
|
|
|
if (table.getTableStyle() != null) { |
|
|
|
|
|
|
|
final TableStyle tableStyle = table.getTableStyle(); |
|
|
|
|
|
|
|
if (table.getCustomWriteHandlerList() == null) { |
|
|
|
|
|
|
|
table.setCustomWriteHandlerList(new ArrayList<WriteHandler>()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
CellStyle headCellStyle = new CellStyle(); |
|
|
|
|
|
|
|
headCellStyle.setFont(tableStyle.getTableHeadFont()); |
|
|
|
|
|
|
|
headCellStyle.setIndexedColors(tableStyle.getTableContentBackGroundColor()); |
|
|
|
|
|
|
|
CellStyle contentCellStyle = new CellStyle(); |
|
|
|
|
|
|
|
contentCellStyle.setFont(tableStyle.getTableContentFont()); |
|
|
|
|
|
|
|
contentCellStyle.setIndexedColors(tableStyle.getTableContentBackGroundColor()); |
|
|
|
|
|
|
|
table.getCustomWriteHandlerList().add(new RowCellStyleStrategy(headCellStyle, contentCellStyle)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean needHead() { |
|
|
|
public ConfigurationSelector currentConfigurationSelector() { |
|
|
|
return this.needHead; |
|
|
|
return currentConfigurationSelector; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getCurrentSheetName() { |
|
|
|
|
|
|
|
return currentSheetHolder.getCurrentSheetParam(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setCurrentSheetName(String currentSheetName) { |
|
|
|
|
|
|
|
this.currentSheetName = currentSheetName; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ExcelTypeEnum getExcelType() { |
|
|
|
|
|
|
|
return excelType; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setExcelType(ExcelTypeEnum excelType) { |
|
|
|
|
|
|
|
this.excelType = excelType; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public OutputStream getOutputStream() { |
|
|
|
public WorkbookHolder currentWorkbookHolder() { |
|
|
|
return outputStream; |
|
|
|
return currentWorkbookHolder; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CellStyle getCurrentHeadCellStyle() { |
|
|
|
|
|
|
|
return this.currentHeadCellStyle == null ? defaultCellStyle : this.currentHeadCellStyle; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CellStyle getCurrentContentStyle() { |
|
|
|
|
|
|
|
return this.currentContentCellStyle; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Workbook getWorkbook() { |
|
|
|
public SheetHolder currentSheetHolder() { |
|
|
|
return workbook; |
|
|
|
return currentSheetHolder; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public com.alibaba.excel.metadata.Sheet getCurrentSheetParam() { |
|
|
|
|
|
|
|
return currentSheetHolder.getCurrentSheetParam(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setCurrentSheetParam(com.alibaba.excel.metadata.Sheet currentSheetParam) { |
|
|
|
|
|
|
|
this.currentSheetParam = currentSheetParam; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Table getCurrentTableParam() { |
|
|
|
|
|
|
|
return currentTableHolder.getCurrentTableParam(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Table getCurrentTable() { |
|
|
|
|
|
|
|
return currentTable; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public ConverterRegistryCenter getConverterRegistryCenter() { |
|
|
|
public TableHolder currentTableHolder() { |
|
|
|
return registryCenter; |
|
|
|
return currentTableHolder; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void finish() { |
|
|
|
public void finish() { |
|
|
|
try { |
|
|
|
try { |
|
|
|
workbook.write(outputStream); |
|
|
|
currentWorkbookHolder.getWorkbook().write(currentWorkbookHolder.getOutputStream()); |
|
|
|
workbook.close(); |
|
|
|
currentWorkbookHolder.getWorkbook().close(); |
|
|
|
if (outputStream != null) { |
|
|
|
if (currentWorkbookHolder.getOutputStream() != null) { |
|
|
|
outputStream.close(); |
|
|
|
currentWorkbookHolder.getOutputStream().close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (templateInputStream != null) { |
|
|
|
if (currentWorkbookHolder.getTemplateInputStream() != null) { |
|
|
|
templateInputStream.close(); |
|
|
|
currentWorkbookHolder.getTemplateInputStream().close(); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
throw new ExcelGenerateException("Can not close IO", e); |
|
|
|
throw new ExcelGenerateException("Can not close IO", e); |
|
|
|