@@ -13,10 +13,6 @@ import java.lang.annotation.Target; | |||
@Retention(RetentionPolicy.RUNTIME) | |||
@Target(ElementType.FIELD) | |||
public @interface Excel { | |||
/** | |||
* cell 写入的设置的函数 | |||
*/ | |||
public String cellFormula() default ""; | |||
/** | |||
* 导出时间设置,如果字段是Date类型则不需要设置 数据库如果是string 类型,这个需要设置这个数据库格式 | |||
@@ -112,10 +108,4 @@ public @interface Excel { | |||
* @return | |||
*/ | |||
public boolean isStatistics() default false; | |||
/** | |||
* 冰冻列 | |||
* @return | |||
*/ | |||
public boolean isFreeze() default false; | |||
} |
@@ -9,7 +9,7 @@ import java.util.ArrayList; | |||
/** | |||
* 导出的集合 | |||
* | |||
* @author jueyue 2013年8月24日 | |||
* @author JueYue 2013年8月24日 | |||
*/ | |||
@Retention(RetentionPolicy.RUNTIME) | |||
@Target(ElementType.FIELD) | |||
@@ -8,7 +8,7 @@ import java.lang.annotation.Target; | |||
/** | |||
* 标记是不是导出excel 标记为实体类 | |||
* | |||
* @author jueyue | |||
* @author JueYue | |||
* | |||
*/ | |||
@Retention(RetentionPolicy.RUNTIME) | |||
@@ -8,7 +8,7 @@ import java.lang.annotation.Target; | |||
/** | |||
* excel 导出是用于标记id的 | |||
* | |||
* @author jueyue | |||
* @author JueYue | |||
* | |||
*/ | |||
@Retention(RetentionPolicy.RUNTIME) | |||
@@ -18,7 +18,7 @@ import org.jeecgframework.poi.excel.export.template.ExcelExportOfTemplateUtil; | |||
/** | |||
* excel 导出工具类 | |||
* | |||
* @author jueyue | |||
* @author JueYue | |||
* @version 1.0 | |||
* @date 2013-10-17 | |||
*/ | |||
@@ -7,7 +7,7 @@ import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerDefaultImpl; | |||
/** | |||
* Excel 导出参数 | |||
* | |||
* @author jueyue | |||
* @author JueYue | |||
* @version 1.0 2013年8月24日 | |||
*/ | |||
public class ExportParams extends ExcelBaseParams { | |||
@@ -47,6 +47,10 @@ public class ExportParams extends ExcelBaseParams { | |||
* 是否添加需要需要 | |||
*/ | |||
private String indexName = "序号"; | |||
/** | |||
* 冰冻列 | |||
*/ | |||
private int freezeCol; | |||
/** | |||
* 表头颜色 | |||
*/ | |||
@@ -181,4 +185,12 @@ public class ExportParams extends ExcelBaseParams { | |||
this.style = style; | |||
} | |||
public int getFreezeCol() { | |||
return freezeCol; | |||
} | |||
public void setFreezeCol(int freezeCol) { | |||
this.freezeCol = freezeCol; | |||
} | |||
} |
@@ -4,7 +4,7 @@ import java.util.Comparator; | |||
/** | |||
* 按照升序排序 | |||
* @author jueyue | |||
* @author JueYue | |||
* | |||
*/ | |||
public class ComparatorExcelField implements Comparator<ExcelExportEntity> { | |||
@@ -2,10 +2,12 @@ package org.jeecgframework.poi.excel.entity.params; | |||
import java.util.List; | |||
import org.jeecgframework.poi.excel.entity.enmus.CellValueType; | |||
/** | |||
* excel 导出工具类,对cell类型做映射 | |||
* | |||
* @author jueyue | |||
* @author JueYue | |||
* @version 1.0 2013年8月24日 | |||
*/ | |||
public class ExcelExportEntity extends ExcelBaseEntity { | |||
@@ -58,10 +60,6 @@ public class ExcelExportEntity extends ExcelBaseEntity { | |||
* 统计 | |||
*/ | |||
private boolean isStatistics; | |||
/** | |||
* 统计 | |||
*/ | |||
private boolean isFreeze; | |||
private List<ExcelExportEntity> list; | |||
@@ -188,12 +186,4 @@ public class ExcelExportEntity extends ExcelBaseEntity { | |||
this.isStatistics = isStatistics; | |||
} | |||
public boolean isFreeze() { | |||
return isFreeze; | |||
} | |||
public void setFreeze(boolean isFreeze) { | |||
this.isFreeze = isFreeze; | |||
} | |||
} |
@@ -4,7 +4,7 @@ import java.util.List; | |||
/** | |||
* excel 导入工具类,对cell类型做映射 | |||
* @author jueyue | |||
* @author JueYue | |||
* @version 1.0 2013年8月24日 | |||
*/ | |||
public class ExcelImportEntity extends ExcelBaseEntity { | |||
@@ -140,10 +140,9 @@ public class ExcelExportServer extends ExcelExportBase { | |||
break; | |||
} | |||
mergeCells(sheet, excelParams, titleHeight); | |||
for (int i = 0; i < excelParams.size(); i++) { | |||
if (excelParams.get(i).isFreeze()) { | |||
sheet.createFreezePane(i + 1, 0, i + 1, 0); | |||
} | |||
if (entity.getFreezeCol() != 0) { | |||
sheet.createFreezePane(1, 0, entity.getFreezeCol(), 0); | |||
} | |||
its = dataSet.iterator(); | |||
@@ -152,7 +151,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||
its.remove(); | |||
} | |||
// 创建合计信息 | |||
addStatisticsRow(getExcelExportStyler().getStyles(true, true), sheet); | |||
addStatisticsRow(getExcelExportStyler().getStyles(true, null), sheet); | |||
// 发现还有剩余list 继续循环创建Sheet | |||
if (dataSet.size() > 0) { | |||
@@ -51,7 +51,7 @@ public abstract class ExcelExportBase extends ExportBase { | |||
private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00"); | |||
private IExcelExportStyler excelExportStyler; | |||
private IExcelExportStyler excelExportStyler; | |||
private boolean checkIsEqualByCellContents(MergeEntity mergeEntity, String text, Cell cell, | |||
int[] delys, int rowNum) { | |||
@@ -105,12 +105,8 @@ public abstract class ExcelExportBase extends ExportBase { | |||
} else { | |||
Object value = getCellValue(entity, t); | |||
if (entity.getType() == 1) { | |||
createStringCell( | |||
row, | |||
cellNum++, | |||
value == null ? "" : value.toString(), | |||
index % 2 == 0 ? getStyles(false, entity.isWrap()) : getStyles(true, | |||
entity.isWrap()), entity); | |||
createStringCell(row, cellNum++, value == null ? "" : value.toString(), | |||
index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity); | |||
} else { | |||
createImageCell(patriarch, entity, row, cellNum++, | |||
value == null ? "" : value.toString(), t); | |||
@@ -126,8 +122,7 @@ public abstract class ExcelExportBase extends ExportBase { | |||
} else if (entity.isNeedMerge()) { | |||
for (int i = index + 1; i < index + maxHeight; i++) { | |||
sheet.getRow(i).createCell(cellNum); | |||
sheet.getRow(i).getCell(cellNum) | |||
.setCellStyle(getStyles(false, entity.isWrap())); | |||
sheet.getRow(i).getCell(cellNum).setCellStyle(getStyles(false, entity)); | |||
} | |||
sheet.addMergedRegion(new CellRangeAddress(index, index + maxHeight - 1, cellNum, | |||
cellNum)); | |||
@@ -196,8 +191,8 @@ public abstract class ExcelExportBase extends ExportBase { | |||
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(false, false) | |||
: getStyles(true, false), null); | |||
createStringCell(row, 0, currentIndex + "", index % 2 == 0 ? getStyles(false, null) | |||
: getStyles(true, null), null); | |||
currentIndex = currentIndex + 1; | |||
return 1; | |||
} | |||
@@ -224,12 +219,9 @@ 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(false, entity.isWrap()) : getStyles(true, | |||
entity.isWrap()), entity); | |||
createStringCell(row, cellNum++, value == null ? "" : value.toString(), | |||
row.getRowNum() % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), | |||
entity); | |||
} else { | |||
createImageCell(patriarch, entity, row, cellNum++, | |||
value == null ? "" : value.toString(), obj); | |||
@@ -265,7 +257,7 @@ public abstract class ExcelExportBase extends ExportBase { | |||
} else { | |||
Rtext = new XSSFRichTextString(text); | |||
} | |||
cell.setCellValue(Rtext); | |||
cell.setCellValue(text); | |||
if (style != null) { | |||
cell.setCellStyle(style); | |||
} | |||
@@ -382,12 +374,12 @@ public abstract class ExcelExportBase extends ExportBase { | |||
/** | |||
* 获取样式 | |||
* @param entity | |||
* @param needOne | |||
* @param isWrap | |||
* @return | |||
*/ | |||
public CellStyle getStyles(boolean needOne, boolean isWrap) { | |||
return excelExportStyler.getStyles(needOne, isWrap); | |||
public CellStyle getStyles(boolean needOne, ExcelExportEntity entity) { | |||
return excelExportStyler.getStyles(needOne, entity); | |||
} | |||
/** | |||
@@ -204,7 +204,6 @@ public class ExportBase { | |||
excelEntity.setFormat(StringUtils.isNotEmpty(excel.exportFormat()) ? excel.exportFormat() | |||
: excel.format()); | |||
excelEntity.setStatistics(excel.isStatistics()); | |||
excelEntity.setFreeze(excel.isFreeze()); | |||
String fieldname = field.getName(); | |||
excelEntity.setMethod(POIPublicUtil.getMethod(fieldname, pojoClass)); | |||
} | |||
@@ -3,6 +3,7 @@ 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; | |||
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | |||
/** | |||
* 抽象接口提供两个公共方法 | |||
@@ -10,44 +11,45 @@ import org.apache.poi.ss.usermodel.Workbook; | |||
* @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 CellStyle stringNoneStyle; | |||
protected CellStyle stringNoneWrapStyle; | |||
//间隔行 | |||
protected CellStyle stringSeptailStyle; | |||
protected CellStyle stringSeptailWrapStyle; | |||
protected Workbook workbook; | |||
protected static final short cellFormat = (short) BuiltinFormats.getBuiltinFormat("TEXT"); | |||
protected static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT"); | |||
protected static final short NUMBER_FORMAT = (short) BuiltinFormats.getBuiltinFormat("0.00"); | |||
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.stringNoneStyle = stringNoneStyle(workbook, false); | |||
this.stringNoneWrapStyle = stringNoneStyle(workbook, true); | |||
this.stringSeptailStyle = stringSeptailStyle(workbook, false); | |||
this.stringSeptailWrapStyle = stringSeptailStyle(workbook, true); | |||
this.workbook = workbook; | |||
} | |||
@Override | |||
public CellStyle getStyles(boolean needOne, boolean isWrap) { | |||
if (needOne && isWrap) { | |||
return oneWrapStyle; | |||
public CellStyle getStyles(boolean noneStyler, ExcelExportEntity entity) { | |||
if (noneStyler && (entity == null || entity.isWrap())) { | |||
return stringNoneWrapStyle; | |||
} | |||
if (needOne) { | |||
return oneStyle; | |||
if (noneStyler) { | |||
return stringNoneStyle; | |||
} | |||
if (needOne == false && isWrap) { | |||
return doubleWrapStyle; | |||
if (noneStyler == false && (entity == null || entity.isWrap())) { | |||
return stringSeptailWrapStyle; | |||
} | |||
return doubleStyle; | |||
return stringSeptailStyle; | |||
} | |||
@Override | |||
public CellStyle createOneStyle(Workbook workbook, boolean isWarp) { | |||
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) { | |||
return null; | |||
} | |||
@Override | |||
public CellStyle createDoubleStyle(Workbook workbook, boolean isWarp) { | |||
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) { | |||
return null; | |||
} | |||
@@ -32,7 +32,7 @@ public class ExcelExportStylerBorderImpl extends AbstractExcelExportStyler imple | |||
} | |||
@Override | |||
public CellStyle createOneStyle(Workbook workbook, boolean isWarp) { | |||
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) { | |||
CellStyle style = workbook.createCellStyle(); | |||
style.setBorderLeft((short) 1); // 左边框 | |||
style.setBorderRight((short) 1); // 右边框 | |||
@@ -40,7 +40,7 @@ public class ExcelExportStylerBorderImpl extends AbstractExcelExportStyler imple | |||
style.setBorderTop((short) 1); | |||
style.setAlignment(CellStyle.ALIGN_CENTER); | |||
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); | |||
style.setDataFormat(cellFormat); | |||
style.setDataFormat(STRING_FORMAT); | |||
if (isWarp) { | |||
style.setWrapText(true); | |||
} | |||
@@ -61,8 +61,8 @@ public class ExcelExportStylerBorderImpl extends AbstractExcelExportStyler imple | |||
} | |||
@Override | |||
public CellStyle createDoubleStyle(Workbook workbook, boolean isWarp) { | |||
return isWarp ? oneWrapStyle : oneStyle; | |||
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) { | |||
return isWarp ? stringNoneWrapStyle : stringNoneStyle; | |||
} | |||
} |
@@ -29,7 +29,7 @@ public class ExcelExportStylerColorImpl extends AbstractExcelExportStyler implem | |||
} | |||
@Override | |||
public CellStyle createOneStyle(Workbook workbook, boolean isWarp) { | |||
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) { | |||
CellStyle style = workbook.createCellStyle(); | |||
style.setBorderLeft((short) 1); // 左边框 | |||
style.setBorderRight((short) 1); // 右边框 | |||
@@ -37,7 +37,7 @@ public class ExcelExportStylerColorImpl extends AbstractExcelExportStyler implem | |||
style.setBorderTop((short) 1); | |||
style.setAlignment(CellStyle.ALIGN_CENTER); | |||
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); | |||
style.setDataFormat(cellFormat); | |||
style.setDataFormat(STRING_FORMAT); | |||
if (isWarp) { | |||
style.setWrapText(true); | |||
} | |||
@@ -56,7 +56,7 @@ public class ExcelExportStylerColorImpl extends AbstractExcelExportStyler implem | |||
} | |||
@Override | |||
public CellStyle createDoubleStyle(Workbook workbook, boolean isWarp) { | |||
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) { | |||
CellStyle style = workbook.createCellStyle(); | |||
style.setBorderLeft((short) 1); // 左边框 | |||
style.setBorderRight((short) 1); // 右边框 | |||
@@ -66,7 +66,7 @@ public class ExcelExportStylerColorImpl extends AbstractExcelExportStyler implem | |||
style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充图案 | |||
style.setAlignment(CellStyle.ALIGN_CENTER); | |||
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); | |||
style.setDataFormat(cellFormat); | |||
style.setDataFormat(STRING_FORMAT); | |||
if (isWarp) { | |||
style.setWrapText(true); | |||
} | |||
@@ -11,7 +11,7 @@ import org.apache.poi.ss.usermodel.Workbook; | |||
*/ | |||
public class ExcelExportStylerDefaultImpl extends AbstractExcelExportStyler implements | |||
IExcelExportStyler { | |||
public ExcelExportStylerDefaultImpl(Workbook workbook) { | |||
super.createStyles(workbook); | |||
} | |||
@@ -26,11 +26,11 @@ public class ExcelExportStylerDefaultImpl extends AbstractExcelExportStyler impl | |||
} | |||
@Override | |||
public CellStyle createDoubleStyle(Workbook workbook, boolean isWarp) { | |||
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) { | |||
CellStyle style = workbook.createCellStyle(); | |||
style.setAlignment(CellStyle.ALIGN_CENTER); | |||
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); | |||
style.setDataFormat(cellFormat); | |||
style.setDataFormat(STRING_FORMAT); | |||
if (isWarp) { | |||
style.setWrapText(true); | |||
} | |||
@@ -49,11 +49,11 @@ public class ExcelExportStylerDefaultImpl extends AbstractExcelExportStyler impl | |||
} | |||
@Override | |||
public CellStyle createOneStyle(Workbook workbook, boolean isWarp) { | |||
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) { | |||
CellStyle style = workbook.createCellStyle(); | |||
style.setAlignment(CellStyle.ALIGN_CENTER); | |||
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); | |||
style.setDataFormat(cellFormat); | |||
style.setDataFormat(STRING_FORMAT); | |||
if (isWarp) { | |||
style.setWrapText(true); | |||
} | |||
@@ -1,7 +1,7 @@ | |||
package org.jeecgframework.poi.excel.export.styler; | |||
import org.apache.poi.ss.usermodel.CellStyle; | |||
import org.apache.poi.ss.usermodel.Workbook; | |||
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | |||
/** | |||
* Excel导出样式接口 | |||
@@ -26,27 +26,10 @@ public interface IExcelExportStyler { | |||
/** | |||
* 获取样式方法 | |||
* @param map | |||
* @param needOne | |||
* @param isWrap | |||
* @param noneStyler | |||
* @param entity | |||
* @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); | |||
public CellStyle getStyles(boolean noneStyler, ExcelExportEntity entity); | |||
} |
@@ -22,5 +22,5 @@ | |||
<attribute name="maven.pomderived" value="true"/> | |||
</attributes> | |||
</classpathentry> | |||
<classpathentry kind="output" path="target/test-classes"/> | |||
<classpathentry kind="output" path="target/classes"/> | |||
</classpath> |
@@ -11,7 +11,7 @@ import org.jeecgframework.poi.excel.annotation.ExcelVerify; | |||
/** | |||
* @Title: Entity | |||
* @Description: 课程 | |||
* @author jueyue | |||
* @author JueYue | |||
* @date 2013-08-31 22:53:07 | |||
* @version V1.0 | |||
* | |||
@@ -18,7 +18,7 @@ public class MsgClient implements java.io.Serializable { | |||
/** id */ | |||
private java.lang.String id; | |||
// 电话号码(主键) | |||
@Excel(name = "电话号码", isFreeze = true) | |||
@Excel(name = "电话号码") | |||
private String clientPhone = null; | |||
// 客户姓名 | |||
@Excel(name = "姓名") | |||
@@ -5,7 +5,7 @@ import java.util.Date; | |||
import org.jeecgframework.poi.excel.annotation.Excel; | |||
/** | |||
* @author jueyue | |||
* @author JueYue | |||
* @version V1.0 | |||
* @Title: Entity | |||
* @Description: 学生 | |||
@@ -6,7 +6,7 @@ import org.jeecgframework.poi.excel.annotation.ExcelTarget; | |||
/** | |||
* @Title: Entity | |||
* @Description: 课程老师 | |||
* @author jueyue | |||
* @author JueYue | |||
* @date 2013-08-31 22:52:17 | |||
* @version V1.0 | |||
* | |||
@@ -18,7 +18,7 @@ public class TeacherEntity implements java.io.Serializable { | |||
@Excel(name = "老师ID_teacherEntity,老师属性_courseEntity", orderNum = "2", needMerge = false) | |||
private String id; | |||
/** name */ | |||
@Excel(name = "语文老师_yuwen,数学老师_shuxue", orderNum = "2", mergeVertical = true, isFreeze = true) | |||
@Excel(name = "语文老师_yuwen,数学老师_shuxue", orderNum = "2", mergeVertical = true) | |||
private String name; | |||
/* | |||
@@ -42,7 +42,7 @@ public class ExcelExportMsgClient { | |||
client.setBirthday(new Date()); | |||
client.setClientName("小明" + i); | |||
client.setClientPhone("18797" + i); | |||
client.setCreateBy("jueyue"); | |||
client.setCreateBy("JueYue"); | |||
client.setId("1" + i); | |||
client.setRemark("测试" + i); | |||
MsgClientGroup group = new MsgClientGroup(); | |||
@@ -52,6 +52,7 @@ public class ExcelExportMsgClient { | |||
} | |||
Date start = new Date(); | |||
ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF); | |||
params.setFreezeCol(2); | |||
Workbook workbook = ExcelExportUtil.exportExcel(params, MsgClient.class, list); | |||
System.out.println(new Date().getTime() - start.getTime()); | |||
File savefile = new File("d:/"); | |||