Explorar el Código

Excel 导出样式的设置,自定义接口

4.1.3.A
jueyue hace 10 años
padre
commit
fe142ab57d
Se han modificado 20 ficheros con 470 adiciones y 191 borrados
  1. +1
    -2
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelExportUtil.java
  2. +14
    -1
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ExportParams.java
  3. +22
    -5
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/TemplateExportParams.java
  4. +34
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/enmus/ExcelStyleType.java
  5. +1
    -1
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/enmus/ExcelType.java
  6. +27
    -107
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java
  7. +37
    -19
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java
  8. +54
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/styler/AbstractExcelExportStyler.java
  9. +68
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/styler/ExcelExportStylerBorderImpl.java
  10. +76
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/styler/ExcelExportStylerColorImpl.java
  11. +63
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/styler/ExcelExportStylerDefaultImpl.java
  12. +52
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/styler/IExcelExportStyler.java
  13. +6
    -2
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java
  14. +1
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/verifys/VerifyHandlerServer.java
  15. +4
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/handler/inter/IExcelDataHandler.java
  16. +0
    -41
      easypoi-test/src/main/java/org/jeecgframework/poi/excel/MyExcelExport.java
  17. +5
    -1
      easypoi-test/src/main/java/org/jeecgframework/poi/excel/template/TemplateExcelExportTest.java
  18. +3
    -1
      easypoi-test/src/main/java/org/jeecgframework/poi/excel/test/ExcelExportStatisticTest.java
  19. +2
    -0
      easypoi-test/src/main/java/org/jeecgframework/poi/excel/test/ExcelExportTemplateTest.java
  20. +0
    -11
      pom.xml

+ 1
- 2
easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelExportUtil.java Ver fichero

@@ -12,7 +12,6 @@ import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants;
import org.jeecgframework.poi.excel.export.ExcelExportServer;
import org.jeecgframework.poi.excel.export.template.ExcelExportOfTemplateUtil;
@@ -85,8 +84,8 @@ public final class ExcelExportUtil {
} else {
workbook = new XSSFWorkbook();
}
ExcelExportServer server = new ExcelExportServer();
for (Map<String, Object> map : list) {
ExcelExportServer server = new ExcelExportServer();
server.createSheet(workbook, (ExportParams) map.get("title"),
(Class<?>) map.get("entity"), (Collection<?>) map.get("data"));
}


+ 14
- 1
easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ExportParams.java Ver fichero

@@ -2,6 +2,7 @@ package org.jeecgframework.poi.excel.entity;
import org.apache.poi.hssf.util.HSSFColor;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerDefaultImpl;
/**
* Excel 导出参数
@@ -19,7 +20,7 @@ public class ExportParams extends ExcelBaseParams {
/**
* 表格名称
*/
private short titleHeight = 20;
private short titleHeight = 10;
/**
* 第二行名称
@@ -58,6 +59,10 @@ public class ExportParams extends ExcelBaseParams {
* Excel 导出版本
*/
private ExcelType type = ExcelType.HSSF;
/**
* Excel 导出style
*/
private Class<?> style = ExcelExportStylerDefaultImpl.class;
public ExportParams() {
@@ -168,4 +173,12 @@ public class ExportParams extends ExcelBaseParams {
this.indexName = indexName;
}
public Class<?> getStyle() {
return style;
}
public void setStyle(Class<?> style) {
this.style = style;
}
}

+ 22
- 5
easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/TemplateExportParams.java Ver fichero

@@ -1,5 +1,7 @@
package org.jeecgframework.poi.excel.entity;
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerDefaultImpl;
/**
* 模板导出参数设置
*
@@ -12,27 +14,31 @@ public class TemplateExportParams extends ExcelBaseParams {
/**
* 模板的路径
*/
private String templateUrl;
private String templateUrl;
/**
* 需要导出的第几个 sheetNum,默认是第0个
*/
private int sheetNum = 0;
private int sheetNum = 0;
/**
* 这只sheetName 不填就使用原来的
*/
private String sheetName;
private String sheetName;
/**
* 表格列标题行数,默认1
*/
private int headingRows = 1;
private int headingRows = 1;
/**
* 表格列标题开始行,默认1
*/
private int headingStartRow = 1;
private int headingStartRow = 1;
/**
* Excel 导出style
*/
private Class<?> style = ExcelExportStylerDefaultImpl.class;
public TemplateExportParams() {
@@ -41,14 +47,17 @@ public class TemplateExportParams extends ExcelBaseParams {
public TemplateExportParams(String templateUrl) {
this.templateUrl = templateUrl;
}
public TemplateExportParams(String templateUrl, int sheetNum) {
this.templateUrl = templateUrl;
this.sheetNum = sheetNum;
}
public TemplateExportParams(String templateUrl, String sheetName) {
this.templateUrl = templateUrl;
this.sheetName = sheetName;
}
public TemplateExportParams(String templateUrl, String sheetName, int sheetNum) {
this.templateUrl = templateUrl;
this.sheetName = sheetName;
@@ -95,4 +104,12 @@ public class TemplateExportParams extends ExcelBaseParams {
this.templateUrl = templateUrl;
}
public Class<?> getStyle() {
return style;
}
public void setStyle(Class<?> style) {
this.style = style;
}
}

+ 34
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/enmus/ExcelStyleType.java Ver fichero

@@ -0,0 +1,34 @@
package org.jeecgframework.poi.excel.entity.enmus;
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerBorderImpl;
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerColorImpl;
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerDefaultImpl;
/**
* 插件提供的几个默认样式
* @author JueYue
* @date 2015年1月9日 下午9:02:24
*/
public enum ExcelStyleType {
NONE("默认样式", ExcelExportStylerDefaultImpl.class),
BORDER("边框样式", ExcelExportStylerBorderImpl.class),
COLOR("间隔行样式", ExcelExportStylerColorImpl.class);
private String name;
private Class<?> clazz;
ExcelStyleType(String name, Class<?> clazz) {
this.name = name;
this.clazz = clazz;
}
public Class<?> getClazz() {
return clazz;
}
public String getName() {
return name;
}
}

+ 1
- 1
easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/enmus/ExcelType.java Ver fichero

@@ -8,5 +8,5 @@ package org.jeecgframework.poi.excel.entity.enmus;
public enum ExcelType {
HSSF, XSSF;
}

+ 27
- 107
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/ExcelExportServer.java Ver fichero

@@ -4,15 +4,12 @@ import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
@@ -23,6 +20,8 @@ import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants;
import org.jeecgframework.poi.excel.export.base.ExcelExportBase;
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerDefaultImpl;
import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler;
import org.jeecgframework.poi.exception.excel.ExcelExportException;
import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum;
import org.jeecgframework.poi.util.POIPublicUtil;
@@ -37,12 +36,10 @@ import org.slf4j.LoggerFactory;
*/
public class ExcelExportServer extends ExcelExportBase {
private final static Logger logger = LoggerFactory.getLogger(ExcelExportServer.class);
private static final short cellFormat = (short) BuiltinFormats.getBuiltinFormat("TEXT");
private final static Logger logger = LoggerFactory.getLogger(ExcelExportServer.class);
// 最大行数,超过自动多Sheet
private int MAX_NUM = 60000;
private int MAX_NUM = 60000;
private int createHeaderAndTitle(ExportParams entity, Sheet sheet, Workbook workbook,
List<ExcelExportEntity> excelParams) {
@@ -66,7 +63,12 @@ public class ExcelExportServer extends ExcelExportBase {
public int createHeaderRow(ExportParams entity, Sheet sheet, Workbook workbook, int feildWidth) {
Row row = sheet.createRow(0);
row.setHeight(entity.getTitleHeight());
createStringCell(row, 0, entity.getTitle(), getHeaderStyle(workbook, entity), null);
createStringCell(row, 0, entity.getTitle(),
getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
for (int i = 1; i <= feildWidth; i++) {
createStringCell(row, i, "",
getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
}
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, feildWidth));
if (entity.getSecondTitle() != null) {
row = sheet.createRow(1);
@@ -74,6 +76,10 @@ public class ExcelExportServer extends ExcelExportBase {
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_RIGHT);
createStringCell(row, 0, entity.getSecondTitle(), style, null);
for (int i = 1; i <= feildWidth; i++) {
createStringCell(row, i, "",
getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
}
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, feildWidth));
return 2;
}
@@ -106,8 +112,9 @@ public class ExcelExportServer extends ExcelExportBase {
if (dataHanlder != null) {
needHanlderList = Arrays.asList(dataHanlder.getNeedHandlerFields());
}
// 创建表格属性
Map<String, CellStyle> styles = createStyles(workbook);
// 创建表格样式
setExcelExportStyler((IExcelExportStyler) entity.getStyle()
.getConstructor(Workbook.class).newInstance(workbook));
Drawing patriarch = sheet.createDrawingPatriarch();
List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
if (entity.isAddIndex()) {
@@ -128,8 +135,7 @@ public class ExcelExportServer extends ExcelExportBase {
List<Object> tempList = new ArrayList<Object>();
while (its.hasNext()) {
Object t = its.next();
index += createCells(patriarch, index, t, excelParams, sheet, workbook, styles,
rowHeight);
index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight);
tempList.add(t);
if (index >= MAX_NUM)
break;
@@ -142,7 +148,7 @@ public class ExcelExportServer extends ExcelExportBase {
its.remove();
}
// 创建合计信息
addStatisticsRow(styles.get("one"), sheet);
addStatisticsRow(getExcelExportStyler().getStyles(true, true), sheet);
// 发现还有剩余list 继续循环创建Sheet
if (dataSet.size() > 0) {
@@ -151,7 +157,7 @@ public class ExcelExportServer extends ExcelExportBase {
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(),e);
logger.error(e.getMessage(), e);
throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause());
}
}
@@ -182,8 +188,9 @@ public class ExcelExportServer extends ExcelExportBase {
if (dataHanlder != null) {
needHanlderList = Arrays.asList(dataHanlder.getNeedHandlerFields());
}
// 创建表格属性
Map<String, CellStyle> styles = createStyles(workbook);
// 创建表格样式
setExcelExportStyler((IExcelExportStyler) entity.getStyle()
.getConstructor(Workbook.class).newInstance(workbook));
Drawing patriarch = sheet.createDrawingPatriarch();
List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
if (entity.isAddIndex()) {
@@ -200,8 +207,7 @@ public class ExcelExportServer extends ExcelExportBase {
List<Object> tempList = new ArrayList<Object>();
while (its.hasNext()) {
Object t = its.next();
index += createCells(patriarch, index, t, excelParams, sheet, workbook, styles,
rowHeight);
index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight);
tempList.add(t);
if (index >= MAX_NUM)
break;
@@ -219,20 +225,11 @@ public class ExcelExportServer extends ExcelExportBase {
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
logger.error(e.getMessage(), e);
throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause());
}
}
private Map<String, CellStyle> createStyles(Workbook workbook) {
Map<String, CellStyle> map = new HashMap<String, CellStyle>();
map.put("one", getOneStyle(workbook, false));
map.put("oneWrap", getOneStyle(workbook, true));
map.put("two", getTwoStyle(workbook, false));
map.put("twoWrap", getTwoStyle(workbook, true));
return map;
}
/**
* 创建表头
*
@@ -250,7 +247,7 @@ public class ExcelExportServer extends ExcelExportBase {
listRow.setHeight((short) 450);
}
int cellIndex = 0;
CellStyle titleStyle = getTitleStyle(workbook, title);
CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor());
for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) {
ExcelExportEntity entity = excelParams.get(i);
createStringCell(row, cellIndex, entity.getName(), titleStyle, entity);
@@ -265,6 +262,7 @@ public class ExcelExportServer extends ExcelExportBase {
cellIndex++;
}
} else if (rows == 2) {
createStringCell(listRow, cellIndex, "", titleStyle, entity);
sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex, cellIndex));
}
cellIndex++;
@@ -273,38 +271,6 @@ public class ExcelExportServer extends ExcelExportBase {
}
/**
* 表明的Style
*
* @param workbook
* @return
*/
public CellStyle getHeaderStyle(Workbook workbook, ExportParams entity) {
CellStyle titleStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 24);
titleStyle.setFont(font);
titleStyle.setFillForegroundColor(entity.getColor());
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return titleStyle;
}
public CellStyle getOneStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setBorderLeft((short) 1); // 左边框
style.setBorderRight((short) 1); // 右边框
style.setBorderBottom((short) 1);
style.setBorderTop((short) 1);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setDataFormat(cellFormat);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
/**
* 判断表头是只有一行还是两行
*
@@ -320,52 +286,6 @@ public class ExcelExportServer extends ExcelExportBase {
return 1;
}
public CellStyle getStyles(Map<String, CellStyle> map, boolean needOne, boolean isWrap) {
if (needOne && isWrap) {
return map.get("oneWrap");
}
if (needOne) {
return map.get("one");
}
if (needOne == false && isWrap) {
return map.get("twoWrap");
}
return map.get("two");
}
/**
* 字段说明的Style
*
* @param workbook
* @return
*/
public CellStyle getTitleStyle(Workbook workbook, ExportParams entity) {
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFillForegroundColor(entity.getHeaderColor()); // 填充的背景颜色
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充图案
titleStyle.setWrapText(true);
return titleStyle;
}
public CellStyle getTwoStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setBorderLeft((short) 1); // 左边框
style.setBorderRight((short) 1); // 右边框
style.setBorderBottom((short) 1);
style.setBorderTop((short) 1);
style.setFillForegroundColor((short) 41); // 填充的背景颜色
style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充图案
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setDataFormat(cellFormat);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
private ExcelExportEntity indexExcelEntity(ExportParams entity) {
ExcelExportEntity exportEntity = new ExcelExportEntity();
exportEntity.setOrderNum(0);


+ 37
- 19
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java Ver fichero

@@ -32,10 +32,11 @@ import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.entity.params.MergeEntity;
import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants;
import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler;
import org.jeecgframework.poi.util.POIPublicUtil;
/**
* 导出基础服务
* 提供POI基础操作服务
*
* @author JueYue
* @date 2014年6月17日 下午6:15:13
@@ -50,6 +51,8 @@ public abstract class ExcelExportBase extends ExportBase {
private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00");
private IExcelExportStyler excelExportStyler;
private boolean checkIsEqualByCellContents(MergeEntity mergeEntity, String text, Cell cell,
int[] delys, int rowNum) {
// 没有依赖关系
@@ -78,12 +81,12 @@ public abstract class ExcelExportBase extends ExportBase {
*/
public int createCells(Drawing patriarch, int index, Object t,
List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook,
Map<String, CellStyle> styles, short rowHeight) throws Exception {
short rowHeight) throws Exception {
ExcelExportEntity entity;
Row row = sheet.createRow(index);
row.setHeight(rowHeight);
int maxHeight = 1, cellNum = 0;
int indexKey = createIndexCell(row, styles, index, excelParams.get(0));
int indexKey = createIndexCell(row, index, excelParams.get(0));
cellNum += indexKey;
for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) {
entity = excelParams.get(k);
@@ -92,7 +95,7 @@ public abstract class ExcelExportBase extends ExportBase {
int listC = 0;
for (Object obj : list) {
createListCells(patriarch, index + listC, cellNum, obj, entity.getList(),
sheet, workbook, styles);
sheet, workbook);
listC++;
}
cellNum += entity.getList().size();
@@ -106,8 +109,8 @@ public abstract class ExcelExportBase extends ExportBase {
row,
cellNum++,
value == null ? "" : value.toString(),
index % 2 == 0 ? getStyles(styles, false, entity.isWrap()) : getStyles(
styles, true, entity.isWrap()), entity);
index % 2 == 0 ? getStyles(false, entity.isWrap()) : getStyles(true,
entity.isWrap()), entity);
} else {
createImageCell(patriarch, entity, row, cellNum++,
value == null ? "" : value.toString(), t);
@@ -124,7 +127,7 @@ public abstract class ExcelExportBase extends ExportBase {
for (int i = index + 1; i < index + maxHeight; i++) {
sheet.getRow(i).createCell(cellNum);
sheet.getRow(i).getCell(cellNum)
.setCellStyle(getStyles(styles, false, entity.isWrap()));
.setCellStyle(getStyles(false, entity.isWrap()));
}
sheet.addMergedRegion(new CellRangeAddress(index, index + maxHeight - 1, cellNum,
cellNum));
@@ -190,13 +193,11 @@ public abstract class ExcelExportBase extends ExportBase {
}
private int createIndexCell(Row row, Map<String, CellStyle> styles, int index,
ExcelExportEntity excelExportEntity) {
private int createIndexCell(Row row, int index, ExcelExportEntity excelExportEntity) {
if (excelExportEntity.getName().equals("序号")
&& excelExportEntity.getFormat().equals(PoiBaseConstants.IS_ADD_INDEX)) {
createStringCell(row, 0, currentIndex + "",
index % 2 == 0 ? getStyles(styles, false, false) : getStyles(styles, true, false),
null);
createStringCell(row, 0, currentIndex + "", index % 2 == 0 ? getStyles(false, false)
: getStyles(true, false), null);
currentIndex = currentIndex + 1;
return 1;
}
@@ -209,8 +210,8 @@ public abstract class ExcelExportBase extends ExportBase {
* @param styles
*/
public void createListCells(Drawing patriarch, int index, int cellNum, Object obj,
List<ExcelExportEntity> excelParams, Sheet sheet,
Workbook workbook, Map<String, CellStyle> styles) throws Exception {
List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook)
throws Exception {
ExcelExportEntity entity;
Row row;
if (sheet.getRow(index) == null) {
@@ -223,9 +224,12 @@ public abstract class ExcelExportBase extends ExportBase {
entity = excelParams.get(k);
Object value = getCellValue(entity, obj);
if (entity.getType() == 1) {
createStringCell(row, cellNum++, value == null ? "" : value.toString(),
row.getRowNum() % 2 == 0 ? getStyles(styles, false, entity.isWrap())
: getStyles(styles, true, entity.isWrap()), entity);
createStringCell(
row,
cellNum++,
value == null ? "" : value.toString(),
row.getRowNum() % 2 == 0 ? getStyles(false, entity.isWrap()) : getStyles(true,
entity.isWrap()), entity);
} else {
createImageCell(patriarch, entity, row, cellNum++,
value == null ? "" : value.toString(), obj);
@@ -376,8 +380,14 @@ public abstract class ExcelExportBase extends ExportBase {
return mergeMap;
}
public CellStyle getStyles(Map<String, CellStyle> styles, boolean needOne, boolean wrap) {
return null;
/**
* 获取样式
* @param needOne
* @param isWrap
* @return
*/
public CellStyle getStyles(boolean needOne, boolean isWrap) {
return excelExportStyler.getStyles(needOne, isWrap);
}
/**
@@ -485,4 +495,12 @@ public abstract class ExcelExportBase extends ExportBase {
this.currentIndex = currentIndex;
}
public void setExcelExportStyler(IExcelExportStyler excelExportStyler) {
this.excelExportStyler = excelExportStyler;
}
public IExcelExportStyler getExcelExportStyler() {
return excelExportStyler;
}
}

+ 54
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/styler/AbstractExcelExportStyler.java Ver fichero

@@ -0,0 +1,54 @@
package org.jeecgframework.poi.excel.export.styler;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
/**
* 抽象接口提供两个公共方法
* @author JueYue
* @date 2015年1月9日 下午5:48:55
*/
public abstract class AbstractExcelExportStyler implements IExcelExportStyler {
protected CellStyle oneStyle;
protected CellStyle oneWrapStyle;
protected CellStyle doubleStyle;
protected CellStyle doubleWrapStyle;
protected Workbook workbook;
protected static final short cellFormat = (short) BuiltinFormats.getBuiltinFormat("TEXT");
protected void createStyles(Workbook workbook) {
this.oneStyle = createOneStyle(workbook, false);
this.oneWrapStyle = createOneStyle(workbook, true);
this.doubleStyle = createDoubleStyle(workbook, false);
this.doubleWrapStyle = createDoubleStyle(workbook, true);
this.workbook = workbook;
}
@Override
public CellStyle getStyles(boolean needOne, boolean isWrap) {
if (needOne && isWrap) {
return oneWrapStyle;
}
if (needOne) {
return oneStyle;
}
if (needOne == false && isWrap) {
return doubleWrapStyle;
}
return doubleStyle;
}
@Override
public CellStyle createOneStyle(Workbook workbook, boolean isWarp) {
return null;
}
@Override
public CellStyle createDoubleStyle(Workbook workbook, boolean isWarp) {
return null;
}
}

+ 68
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/styler/ExcelExportStylerBorderImpl.java Ver fichero

@@ -0,0 +1,68 @@
package org.jeecgframework.poi.excel.export.styler;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
/**
* 带有边框的Excel样式
* @author JueYue
* @date 2015年1月9日 下午5:55:29
*/
public class ExcelExportStylerBorderImpl extends AbstractExcelExportStyler implements
IExcelExportStyler {
public ExcelExportStylerBorderImpl(Workbook workbook) {
super.createStyles(workbook);
}
@Override
public CellStyle getHeaderStyle(short color) {
CellStyle titleStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
titleStyle.setFont(font);
titleStyle.setBorderLeft((short) 1); // 左边框
titleStyle.setBorderRight((short) 1); // 右边框
titleStyle.setBorderBottom((short) 1);
titleStyle.setBorderTop((short) 1);
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return titleStyle;
}
@Override
public CellStyle createOneStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setBorderLeft((short) 1); // 左边框
style.setBorderRight((short) 1); // 右边框
style.setBorderBottom((short) 1);
style.setBorderTop((short) 1);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setDataFormat(cellFormat);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
@Override
public CellStyle getTitleStyle(short color) {
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setBorderLeft((short) 1); // 左边框
titleStyle.setBorderRight((short) 1); // 右边框
titleStyle.setBorderBottom((short) 1);
titleStyle.setBorderTop((short) 1);
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
titleStyle.setWrapText(true);
return titleStyle;
}
@Override
public CellStyle createDoubleStyle(Workbook workbook, boolean isWarp) {
return isWarp ? oneWrapStyle : oneStyle;
}
}

+ 76
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/styler/ExcelExportStylerColorImpl.java Ver fichero

@@ -0,0 +1,76 @@
package org.jeecgframework.poi.excel.export.styler;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
/**
* 带有样式的导出服务
* @author JueYue
* @date 2015年1月9日 下午4:54:15
*/
public class ExcelExportStylerColorImpl extends AbstractExcelExportStyler implements
IExcelExportStyler {
public ExcelExportStylerColorImpl(Workbook workbook) {
super.createStyles(workbook);
}
@Override
public CellStyle getHeaderStyle(short headerColor) {
CellStyle titleStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 24);
titleStyle.setFont(font);
titleStyle.setFillForegroundColor(headerColor);
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return titleStyle;
}
@Override
public CellStyle createOneStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setBorderLeft((short) 1); // 左边框
style.setBorderRight((short) 1); // 右边框
style.setBorderBottom((short) 1);
style.setBorderTop((short) 1);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setDataFormat(cellFormat);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
@Override
public CellStyle getTitleStyle(short color) {
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFillForegroundColor(color); // 填充的背景颜色
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充图案
titleStyle.setWrapText(true);
return titleStyle;
}
@Override
public CellStyle createDoubleStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setBorderLeft((short) 1); // 左边框
style.setBorderRight((short) 1); // 右边框
style.setBorderBottom((short) 1);
style.setBorderTop((short) 1);
style.setFillForegroundColor((short) 41); // 填充的背景颜色
style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充图案
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setDataFormat(cellFormat);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
}

+ 63
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/styler/ExcelExportStylerDefaultImpl.java Ver fichero

@@ -0,0 +1,63 @@
package org.jeecgframework.poi.excel.export.styler;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
/**
* 样式的默认实现
* @author JueYue
* @date 2015年1月9日 下午5:36:08
*/
public class ExcelExportStylerDefaultImpl extends AbstractExcelExportStyler implements
IExcelExportStyler {
public ExcelExportStylerDefaultImpl(Workbook workbook) {
super.createStyles(workbook);
}
@Override
public CellStyle getTitleStyle(short color) {
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
titleStyle.setWrapText(true);
return titleStyle;
}
@Override
public CellStyle createDoubleStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setDataFormat(cellFormat);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
@Override
public CellStyle getHeaderStyle(short color) {
CellStyle titleStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
titleStyle.setFont(font);
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return titleStyle;
}
@Override
public CellStyle createOneStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setDataFormat(cellFormat);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
}

+ 52
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/styler/IExcelExportStyler.java Ver fichero

@@ -0,0 +1,52 @@
package org.jeecgframework.poi.excel.export.styler;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
/**
* Excel导出样式接口
* @author JueYue
* @date 2015年1月9日 下午5:32:30
*/
public interface IExcelExportStyler {
/**
* 列表头样式
* @param headerColor
* @return
*/
public CellStyle getHeaderStyle(short headerColor);
/**
* 标题样式
* @param color
* @return
*/
public CellStyle getTitleStyle(short color);
/**
* 获取样式方法
* @param map
* @param needOne
* @param isWrap
* @return
*/
public CellStyle getStyles(boolean needOne, boolean isWrap);
/**
* 创建样式
* @param workbook
* @param isWarp
* @return
*/
public CellStyle createOneStyle(Workbook workbook, boolean isWarp);
/**
* 创建双行样式
* @param workbook
* @param isWarp
* @return
*/
public CellStyle createDoubleStyle(Workbook workbook, boolean isWarp);
}

+ 6
- 2
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/template/ExcelExportOfTemplateUtil.java Ver fichero

@@ -22,6 +22,7 @@ import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.export.base.ExcelExportBase;
import org.jeecgframework.poi.excel.export.styler.IExcelExportStyler;
import org.jeecgframework.poi.exception.excel.ExcelExportException;
import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum;
import org.jeecgframework.poi.util.POIPublicUtil;
@@ -64,6 +65,9 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
if (etarget != null) {
targetId = etarget.value();
}
// 创建表格样式
setExcelExportStyler((IExcelExportStyler) params.getStyle().getConstructor(Workbook.class)
.newInstance(workbook));
// 获取实体对象的导出数据
List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
getAllExcelField(null, targetId, fileds, excelParams, pojoClass, null);
@@ -78,7 +82,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
Iterator<?> its = dataSet.iterator();
while (its.hasNext()) {
Object t = its.next();
index += createCells(patriarch, index, t, excelParams, sheet, workbook, null, rowHeight);
index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight);
}
// 合并同类项
mergeCells(sheet, excelParams, titleHeight);
@@ -148,7 +152,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase {
addDataToSheet(params, pojoClass, dataSet, wb.getSheetAt(0), wb);
}
} catch (Exception e) {
LOGGER.error(e.getMessage(),e);
LOGGER.error(e.getMessage(), e);
return null;
}
return wb;


+ 1
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/verifys/VerifyHandlerServer.java Ver fichero

@@ -12,6 +12,7 @@ import org.jeecgframework.poi.handler.inter.IExcelVerifyHandler;
* @date 2014年6月29日 下午4:37:56
*/
public class VerifyHandlerServer {
private void addVerifyResult(ExcelVerifyHanlderResult temp, ExcelVerifyHanlderResult result) {
if (!temp.isSuccess()) {
result.setSuccess(false);


+ 4
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/handler/inter/IExcelDataHandler.java Ver fichero

@@ -41,6 +41,10 @@ public interface IExcelDataHandler {
*/
public Object importHandler(Object obj, String name, Object value);
/**
* 设置需要处理的属性列表
* @param fields
*/
public void setNeedHandlerFields(String[] fields);
}

+ 0
- 41
easypoi-test/src/main/java/org/jeecgframework/poi/excel/MyExcelExport.java Ver fichero

@@ -1,41 +0,0 @@
package org.jeecgframework.poi.excel;
import java.util.Map;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.export.ExcelExportServer;
/**
* 自己定义导出的样式
* @author JueYue
* @date 2014年12月2日 上午10:32:44
*/
public class MyExcelExport extends ExcelExportServer {
@Override
public CellStyle getHeaderStyle(Workbook workbook, ExportParams entity) {
return super.getHeaderStyle(workbook, entity);
}
@Override
public CellStyle getOneStyle(Workbook workbook, boolean isWarp) {
return super.getOneStyle(workbook, isWarp);
}
@Override
public CellStyle getStyles(Map<String, CellStyle> map, boolean needOne, boolean isWrap) {
return super.getStyles(map, needOne, isWrap);
}
@Override
public CellStyle getTitleStyle(Workbook workbook, ExportParams entity) {
return super.getTitleStyle(workbook, entity);
}
@Override
public CellStyle getTwoStyle(Workbook workbook, boolean isWarp) {
return super.getTwoStyle(workbook, isWarp);
}
}

+ 5
- 1
easypoi-test/src/main/java/org/jeecgframework/poi/excel/template/TemplateExcelExportTest.java Ver fichero

@@ -17,6 +17,9 @@ import org.jeecgframework.poi.entity.temp.PayeeEntity;
import org.jeecgframework.poi.entity.temp.TemplateExcelExportEntity;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerBorderImpl;
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerColorImpl;
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerDefaultImpl;
import org.junit.Test;
import com.google.common.collect.Lists;
@@ -29,6 +32,7 @@ public class TemplateExcelExportTest {
"org/jeecgframework/poi/excel/doc/专项支出用款申请书.xls");
params.setHeadingStartRow(3);
params.setHeadingRows(2);
params.setStyle(ExcelExportStylerColorImpl.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("date", "2014-12-25");
map.put("money", 2000000.00);
@@ -40,7 +44,7 @@ public class TemplateExcelExportTest {
List<TemplateExcelExportEntity> list = new ArrayList<TemplateExcelExportEntity>();
for (int i = 0; i < 1; i++) {
for (int i = 0; i < 4; i++) {
TemplateExcelExportEntity entity = new TemplateExcelExportEntity();
entity.setIndex(i + 1 + "");
entity.setAccountType("开源项目");


+ 3
- 1
easypoi-test/src/main/java/org/jeecgframework/poi/excel/test/ExcelExportStatisticTest.java Ver fichero

@@ -11,6 +11,7 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.entity.statistics.StatisticEntity;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelStyleType;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants;
import org.junit.Test;
@@ -20,7 +21,7 @@ public class ExcelExportStatisticTest {
public void test() throws Exception {
List<StatisticEntity> list = new ArrayList<StatisticEntity>();
for (int i = 0; i < 25000; i++) {
for (int i = 0; i < 20; i++) {
StatisticEntity client = new StatisticEntity();
client.setName("index" + i);
client.setIntTest(1 + i);
@@ -32,6 +33,7 @@ public class ExcelExportStatisticTest {
}
Date start = new Date();
ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF);
params.setStyle(ExcelStyleType.COLOR.getClazz());
Workbook workbook = ExcelExportUtil.exportExcel(params, StatisticEntity.class, list);
System.out.println(new Date().getTime() - start.getTime());
File savefile = new File("d:/");


+ 2
- 0
easypoi-test/src/main/java/org/jeecgframework/poi/excel/test/ExcelExportTemplateTest.java Ver fichero

@@ -14,6 +14,7 @@ import org.jeecgframework.poi.entity.StudentEntity;
import org.jeecgframework.poi.entity.TeacherEntity;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelStyleType;
import org.junit.Before;
import org.junit.Test;
@@ -27,6 +28,7 @@ public class ExcelExportTemplateTest {
TemplateExportParams params = new TemplateExportParams();
params.setHeadingRows(2);
params.setHeadingStartRow(2);
params.setStyle(ExcelStyleType.BORDER.getClazz());
Map<String, Object> map = new HashMap<String, Object>();
map.put("year", "2013");
map.put("sunCourses", list.size());


+ 0
- 11
pom.xml Ver fichero

@@ -155,15 +155,4 @@
</plugins>
</build>
<distributionManagement>
<repository>
<id>postaop-common-release</id>
<url>http://192.168.1.99:8081/nexus/content/repositories/postaop-common-release/</url>
</repository>
<snapshotRepository>
<id>postaop-common-snapshot</id>
<url>http://192.168.1.99:8081/nexus/content/repositories/postaop-common-snapshot/</url>
</snapshotRepository>
</distributionManagement>
</project>

Cargando…
Cancelar
Guardar