|
|
|
package com.alibaba.excel.write;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
|
|
|
|
import com.alibaba.excel.context.WriteContext;
|
|
|
|
import com.alibaba.excel.context.WriteContextImpl;
|
|
|
|
import com.alibaba.excel.enums.WriteTypeEnum;
|
|
|
|
import com.alibaba.excel.exception.ExcelGenerateException;
|
|
|
|
import com.alibaba.excel.util.FileUtils;
|
|
|
|
import com.alibaba.excel.write.executor.ExcelWriteAddExecutor;
|
|
|
|
import com.alibaba.excel.write.executor.ExcelWriteFillExecutor;
|
|
|
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
|
|
|
import com.alibaba.excel.write.metadata.WriteTable;
|
|
|
|
import com.alibaba.excel.write.metadata.WriteWorkbook;
|
|
|
|
import com.alibaba.excel.write.metadata.fill.FillConfig;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author jipengfei
|
|
|
|
*/
|
|
|
|
public class ExcelBuilderImpl implements ExcelBuilder {
|
|
|
|
|
|
|
|
private WriteContext context;
|
|
|
|
private ExcelWriteFillExecutor excelWriteFillExecutor;
|
|
|
|
private ExcelWriteAddExecutor excelWriteAddExecutor;
|
|
|
|
|
|
|
|
public ExcelBuilderImpl(WriteWorkbook writeWorkbook) {
|
|
|
|
try {
|
|
|
|
// Create temporary cache directory at initialization time to avoid POI concurrent write bugs
|
|
|
|
FileUtils.createPoiFilesDirectory();
|
|
|
|
context = new WriteContextImpl(writeWorkbook);
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
finish();
|
|
|
|
throw e;
|
|
|
|
} catch (Throwable e) {
|
|
|
|
finish();
|
|
|
|
throw new ExcelGenerateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addContent(List data, WriteSheet writeSheet) {
|
|
|
|
addContent(data, writeSheet, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable) {
|
|
|
|
try {
|
|
|
|
if (data == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
context.currentSheet(writeSheet, WriteTypeEnum.ADD);
|
|
|
|
context.currentTable(writeTable);
|
|
|
|
if (excelWriteAddExecutor == null) {
|
|
|
|
excelWriteAddExecutor = new ExcelWriteAddExecutor(context);
|
|
|
|
}
|
|
|
|
excelWriteAddExecutor.add(data);
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
finish();
|
|
|
|
throw e;
|
|
|
|
} catch (Throwable e) {
|
|
|
|
finish();
|
|
|
|
throw new ExcelGenerateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fill(Object data, FillConfig fillConfig, WriteSheet writeSheet) {
|
|
|
|
try {
|
|
|
|
if (data == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (context.writeWorkbookHolder().getTempTemplateInputStream() == null) {
|
|
|
|
throw new ExcelGenerateException("Calling the 'fill' method must use a template.");
|
|
|
|
}
|
|
|
|
context.currentSheet(writeSheet, WriteTypeEnum.FILL);
|
|
|
|
if (excelWriteFillExecutor == null) {
|
|
|
|
excelWriteFillExecutor = new ExcelWriteFillExecutor(context);
|
|
|
|
}
|
|
|
|
excelWriteFillExecutor.fill(data, fillConfig);
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
finish();
|
|
|
|
throw e;
|
|
|
|
} catch (Throwable e) {
|
|
|
|
finish();
|
|
|
|
throw new ExcelGenerateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setPassword(String password) {
|
|
|
|
context.setPassword(password);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void finish() {
|
|
|
|
if (context != null) {
|
|
|
|
context.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void merge(int firstRow, int lastRow, int firstCol, int lastCol) {
|
|
|
|
CellRangeAddress cra = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
|
|
|
|
context.writeSheetHolder().getSheet().addMergedRegion(cra);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public WriteContext writeContext() {
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
}
|