| @@ -1,2 +1,3 @@ | |||||
| eclipse.preferences.version=1 | eclipse.preferences.version=1 | ||||
| encoding//src/main/java=UTF-8 | encoding//src/main/java=UTF-8 | ||||
| encoding//src/test/java=UTF-8 | |||||
| @@ -4,7 +4,7 @@ | |||||
| <parent> | <parent> | ||||
| <groupId>org.jeecgframework</groupId> | <groupId>org.jeecgframework</groupId> | ||||
| <artifactId>easypoi</artifactId> | <artifactId>easypoi</artifactId> | ||||
| <version>2.0.1</version> | |||||
| <version>2.0.2</version> | |||||
| </parent> | </parent> | ||||
| <artifactId>easypoi-base</artifactId> | <artifactId>easypoi-base</artifactId> | ||||
| <dependencies> | <dependencies> | ||||
| @@ -22,6 +22,7 @@ import org.apache.poi.ss.util.CellRangeAddress; | |||||
| import org.jeecgframework.poi.excel.annotation.ExcelTarget; | import org.jeecgframework.poi.excel.annotation.ExcelTarget; | ||||
| import org.jeecgframework.poi.excel.entity.ExportParams; | import org.jeecgframework.poi.excel.entity.ExportParams; | ||||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | ||||
| import org.jeecgframework.poi.excel.export.base.ExcelExportBase; | |||||
| import org.jeecgframework.poi.exception.excel.ExcelExportException; | import org.jeecgframework.poi.exception.excel.ExcelExportException; | ||||
| import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum; | import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum; | ||||
| import org.jeecgframework.poi.util.POIPublicUtil; | import org.jeecgframework.poi.util.POIPublicUtil; | ||||
| @@ -1,15 +1,12 @@ | |||||
| package org.jeecgframework.poi.excel.export; | |||||
| package org.jeecgframework.poi.excel.export.base; | |||||
| import java.awt.image.BufferedImage; | import java.awt.image.BufferedImage; | ||||
| import java.io.ByteArrayOutputStream; | import java.io.ByteArrayOutputStream; | ||||
| import java.io.File; | import java.io.File; | ||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.lang.reflect.Field; | |||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||
| import java.lang.reflect.ParameterizedType; | |||||
| import java.text.SimpleDateFormat; | import java.text.SimpleDateFormat; | ||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||
| import java.util.Arrays; | |||||
| import java.util.Collection; | import java.util.Collection; | ||||
| import java.util.Collections; | import java.util.Collections; | ||||
| import java.util.Date; | import java.util.Date; | ||||
| @@ -33,10 +30,6 @@ 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.usermodel.Workbook; | ||||
| import org.apache.poi.ss.util.CellRangeAddress; | import org.apache.poi.ss.util.CellRangeAddress; | ||||
| import org.jeecgframework.poi.excel.annotation.Excel; | |||||
| import org.jeecgframework.poi.excel.annotation.ExcelCollection; | |||||
| import org.jeecgframework.poi.excel.annotation.ExcelEntity; | |||||
| import org.jeecgframework.poi.excel.entity.params.ComparatorExcelField; | |||||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | ||||
| import org.jeecgframework.poi.excel.entity.params.MergeEntity; | import org.jeecgframework.poi.excel.entity.params.MergeEntity; | ||||
| import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | ||||
| @@ -48,184 +41,7 @@ import org.jeecgframework.poi.util.POIPublicUtil; | |||||
| * @author JueYue | * @author JueYue | ||||
| * @date 2014年6月17日 下午6:15:13 | * @date 2014年6月17日 下午6:15:13 | ||||
| */ | */ | ||||
| public abstract class ExcelExportBase { | |||||
| protected IExcelDataHandler dataHanlder; | |||||
| protected List<String> needHanlderList; | |||||
| /** | |||||
| * 获取需要导出的全部字段 | |||||
| * | |||||
| * @param exclusions | |||||
| * @param targetId | |||||
| * 目标ID | |||||
| * @param fields | |||||
| * @throws Exception | |||||
| */ | |||||
| public void getAllExcelField(String[] exclusions, String targetId, | |||||
| Field[] fields, List<ExcelExportEntity> excelParams, | |||||
| Class<?> pojoClass, List<Method> getMethods) throws Exception { | |||||
| List<String> exclusionsList = exclusions != null ? Arrays | |||||
| .asList(exclusions) : null; | |||||
| ExcelExportEntity excelEntity; | |||||
| // 遍历整个filed | |||||
| for (int i = 0; i < fields.length; i++) { | |||||
| Field field = fields[i]; | |||||
| // 先判断是不是collection,在判断是不是java自带对象,之后就是我们自己的对象了 | |||||
| if (POIPublicUtil.isNotUserExcelUserThis(exclusionsList, field, | |||||
| targetId)) { | |||||
| continue; | |||||
| } | |||||
| if (POIPublicUtil.isCollection(field.getType())) { | |||||
| ExcelCollection excel = field | |||||
| .getAnnotation(ExcelCollection.class); | |||||
| ParameterizedType pt = (ParameterizedType) field | |||||
| .getGenericType(); | |||||
| Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0]; | |||||
| List<ExcelExportEntity> list = new ArrayList<ExcelExportEntity>(); | |||||
| getAllExcelField(exclusions, | |||||
| StringUtils.isNotEmpty(excel.id()) ? excel.id() | |||||
| : targetId, | |||||
| POIPublicUtil.getClassFields(clz), list, clz, null); | |||||
| excelEntity = new ExcelExportEntity(); | |||||
| excelEntity.setName(getExcelName(excel.name(), targetId)); | |||||
| excelEntity | |||||
| .setOrderNum(getCellOrder(excel.orderNum(), targetId)); | |||||
| excelEntity.setMethod(POIPublicUtil.getMethod( | |||||
| field.getName(), pojoClass)); | |||||
| excelEntity.setList(list); | |||||
| excelParams.add(excelEntity); | |||||
| } else if (POIPublicUtil.isJavaClass(field)) { | |||||
| excelParams.add(createExcelExportEntity(field, targetId, | |||||
| pojoClass, getMethods)); | |||||
| } else { | |||||
| List<Method> newMethods = new ArrayList<Method>(); | |||||
| if (getMethods != null) { | |||||
| newMethods.addAll(getMethods); | |||||
| } | |||||
| newMethods.add(POIPublicUtil.getMethod(field.getName(), | |||||
| pojoClass)); | |||||
| ExcelEntity excel = field.getAnnotation(ExcelEntity.class); | |||||
| getAllExcelField(exclusions, | |||||
| StringUtils.isNotEmpty(excel.id()) ? excel.id() | |||||
| : targetId, | |||||
| POIPublicUtil.getClassFields(field.getType()), | |||||
| excelParams, field.getType(), newMethods); | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 创建导出实体对象 | |||||
| * | |||||
| * @param field | |||||
| * @param targetId | |||||
| * @param pojoClass | |||||
| * @param getMethods | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| private ExcelExportEntity createExcelExportEntity(Field field, | |||||
| String targetId, Class<?> pojoClass, List<Method> getMethods) | |||||
| throws Exception { | |||||
| Excel excel = field.getAnnotation(Excel.class); | |||||
| ExcelExportEntity excelEntity = new ExcelExportEntity(); | |||||
| excelEntity.setType(excel.type()); | |||||
| getExcelField(targetId, field, excelEntity, excel, pojoClass); | |||||
| if (getMethods != null) { | |||||
| List<Method> newMethods = new ArrayList<Method>(); | |||||
| newMethods.addAll(getMethods); | |||||
| newMethods.add(excelEntity.getMethod()); | |||||
| excelEntity.setMethods(newMethods); | |||||
| } | |||||
| return excelEntity; | |||||
| } | |||||
| /** | |||||
| * 判断在这个单元格显示的名称 | |||||
| * | |||||
| * @param exportName | |||||
| * @param targetId | |||||
| * @return | |||||
| */ | |||||
| public String getExcelName(String exportName, String targetId) { | |||||
| if (exportName.indexOf(",") < 0) { | |||||
| return exportName; | |||||
| } | |||||
| String[] arr = exportName.split(","); | |||||
| for (String str : arr) { | |||||
| if (str.indexOf(targetId) != -1) { | |||||
| return str.split("_")[0]; | |||||
| } | |||||
| } | |||||
| return null; | |||||
| } | |||||
| /** | |||||
| * 获取这个字段的顺序 | |||||
| * | |||||
| * @param orderNum | |||||
| * @param targetId | |||||
| * @return | |||||
| */ | |||||
| public int getCellOrder(String orderNum, String targetId) { | |||||
| if (isInteger(orderNum) || targetId == null) { | |||||
| return Integer.valueOf(orderNum); | |||||
| } | |||||
| String[] arr = orderNum.split(","); | |||||
| String[] temp; | |||||
| for (String str : arr) { | |||||
| temp = str.split("_"); | |||||
| if (targetId.equals(temp[1])) { | |||||
| return Integer.valueOf(temp[0]); | |||||
| } | |||||
| } | |||||
| return 0; | |||||
| } | |||||
| /** | |||||
| * 判断字符串是否是整数 | |||||
| */ | |||||
| public boolean isInteger(String value) { | |||||
| try { | |||||
| Integer.parseInt(value); | |||||
| return true; | |||||
| } catch (NumberFormatException e) { | |||||
| return false; | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 注解到导出对象的转换 | |||||
| * | |||||
| * @param targetId | |||||
| * @param field | |||||
| * @param excelEntity | |||||
| * @param excel | |||||
| * @param pojoClass | |||||
| * @throws Exception | |||||
| */ | |||||
| private void getExcelField(String targetId, Field field, | |||||
| ExcelExportEntity excelEntity, Excel excel, Class<?> pojoClass) | |||||
| throws Exception { | |||||
| excelEntity.setName(getExcelName(excel.name(), targetId)); | |||||
| excelEntity.setWidth(excel.width()); | |||||
| excelEntity.setHeight(excel.height()); | |||||
| excelEntity.setNeedMerge(excel.needMerge()); | |||||
| excelEntity.setMergeVertical(excel.mergeVertical()); | |||||
| excelEntity.setMergeRely(excel.mergeRely()); | |||||
| excelEntity.setReplace(excel.replace()); | |||||
| excelEntity.setOrderNum(getCellOrder(excel.orderNum(), targetId)); | |||||
| excelEntity.setWrap(excel.isWrap()); | |||||
| excelEntity.setExportImageType(excel.imageType()); | |||||
| excelEntity.setDatabaseFormat(excel.databaseFormat()); | |||||
| excelEntity | |||||
| .setFormat(StringUtils.isNotEmpty(excel.exportFormat()) ? excel | |||||
| .exportFormat() : excel.format()); | |||||
| String fieldname = field.getName(); | |||||
| excelEntity.setMethod(POIPublicUtil.getMethod(fieldname, pojoClass)); | |||||
| } | |||||
| public abstract class ExcelExportBase extends ExportBase { | |||||
| /** | /** | ||||
| * 合并单元格 | * 合并单元格 | ||||
| @@ -454,60 +270,6 @@ public abstract class ExcelExportBase { | |||||
| return null; | return null; | ||||
| } | } | ||||
| /** | |||||
| * 获取填如这个cell的值,提供一些附加功能 | |||||
| * | |||||
| * @param entity | |||||
| * @param obj | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| public Object getCellValue(ExcelExportEntity entity, Object obj) | |||||
| throws Exception { | |||||
| Object value = entity.getMethods() != null ? getFieldBySomeMethod( | |||||
| entity.getMethods(), obj) : entity.getMethod().invoke(obj, | |||||
| new Object[] {}); | |||||
| if (needHanlderList != null | |||||
| && needHanlderList.contains(entity.getName())) { | |||||
| value = dataHanlder.exportHandler(obj, entity.getName(), value); | |||||
| } else if (StringUtils.isNotEmpty(entity.getFormat())) { | |||||
| value = formatValue(value, entity); | |||||
| } else if (entity.getReplace() != null | |||||
| && entity.getReplace().length > 0) { | |||||
| value = replaceValue(entity.getReplace(), String.valueOf(value)); | |||||
| } | |||||
| return value == null ? "" : value.toString(); | |||||
| } | |||||
| private Object replaceValue(String[] replace, String value) { | |||||
| String[] temp; | |||||
| for (String str : replace) { | |||||
| temp = str.split("_"); | |||||
| if (value.equals(temp[1])) { | |||||
| value = temp[0]; | |||||
| break; | |||||
| } | |||||
| } | |||||
| return value; | |||||
| } | |||||
| private Object formatValue(Object value, ExcelExportEntity entity) | |||||
| throws Exception { | |||||
| Date temp = null; | |||||
| if (value instanceof String) { | |||||
| SimpleDateFormat format = new SimpleDateFormat( | |||||
| entity.getDatabaseFormat()); | |||||
| temp = format.parse(value.toString()); | |||||
| } else if (value instanceof Date) { | |||||
| temp = (Date) value; | |||||
| } | |||||
| if (temp != null) { | |||||
| SimpleDateFormat format = new SimpleDateFormat(entity.getFormat()); | |||||
| value = format.format(temp); | |||||
| } | |||||
| return value; | |||||
| } | |||||
| /** | /** | ||||
| * 创建List之后的各个Cells | * 创建List之后的各个Cells | ||||
| * | * | ||||
| @@ -543,54 +305,6 @@ public abstract class ExcelExportBase { | |||||
| } | } | ||||
| } | } | ||||
| public short getRowHeight(List<ExcelExportEntity> excelParams) { | |||||
| int maxHeight = 0; | |||||
| for (int i = 0; i < excelParams.size(); i++) { | |||||
| maxHeight = maxHeight > excelParams.get(i).getHeight() ? maxHeight | |||||
| : excelParams.get(i).getHeight(); | |||||
| if (excelParams.get(i).getList() != null) { | |||||
| for (int j = 0; j < excelParams.get(i).getList().size(); j++) { | |||||
| maxHeight = maxHeight > excelParams.get(i).getList().get(j) | |||||
| .getHeight() ? maxHeight : excelParams.get(i) | |||||
| .getList().get(j).getHeight(); | |||||
| } | |||||
| } | |||||
| } | |||||
| return (short) (maxHeight * 50); | |||||
| } | |||||
| /** | |||||
| * 对字段根据用户设置排序 | |||||
| */ | |||||
| public void sortAllParams(List<ExcelExportEntity> excelParams) { | |||||
| Collections.sort(excelParams, new ComparatorExcelField()); | |||||
| for (ExcelExportEntity entity : excelParams) { | |||||
| if (entity.getList() != null) { | |||||
| Collections.sort(entity.getList(), new ComparatorExcelField()); | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 多个反射获取值 | |||||
| * | |||||
| * @param list | |||||
| * @param t | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| public Object getFieldBySomeMethod(List<Method> list, Object t) | |||||
| throws Exception { | |||||
| for (Method m : list) { | |||||
| if (t == null) { | |||||
| t = ""; | |||||
| break; | |||||
| } | |||||
| t = m.invoke(t, new Object[] {}); | |||||
| } | |||||
| return t; | |||||
| } | |||||
| public void setCellWith(List<ExcelExportEntity> excelParams, Sheet sheet) { | public void setCellWith(List<ExcelExportEntity> excelParams, Sheet sheet) { | ||||
| int index = 0; | int index = 0; | ||||
| for (int i = 0; i < excelParams.size(); i++) { | for (int i = 0; i < excelParams.size(); i++) { | ||||
| @@ -0,0 +1,314 @@ | |||||
| package org.jeecgframework.poi.excel.export.base; | |||||
| import java.lang.reflect.Field; | |||||
| import java.lang.reflect.Method; | |||||
| import java.lang.reflect.ParameterizedType; | |||||
| import java.text.SimpleDateFormat; | |||||
| import java.util.ArrayList; | |||||
| import java.util.Arrays; | |||||
| import java.util.Collections; | |||||
| import java.util.Date; | |||||
| import java.util.List; | |||||
| import org.apache.commons.lang.StringUtils; | |||||
| import org.jeecgframework.poi.excel.annotation.Excel; | |||||
| import org.jeecgframework.poi.excel.annotation.ExcelCollection; | |||||
| import org.jeecgframework.poi.excel.annotation.ExcelEntity; | |||||
| import org.jeecgframework.poi.excel.entity.params.ComparatorExcelField; | |||||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | |||||
| import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | |||||
| import org.jeecgframework.poi.util.POIPublicUtil; | |||||
| /** | |||||
| * 导出基础处理,不设计POI,只设计对象,保证复用性 | |||||
| * | |||||
| * @author JueYue | |||||
| * @date 2014年8月9日 下午11:01:32 | |||||
| */ | |||||
| public class ExportBase { | |||||
| protected IExcelDataHandler dataHanlder; | |||||
| protected List<String> needHanlderList; | |||||
| /** | |||||
| * 获取需要导出的全部字段 | |||||
| * | |||||
| * @param exclusions | |||||
| * @param targetId | |||||
| * 目标ID | |||||
| * @param fields | |||||
| * @throws Exception | |||||
| */ | |||||
| public void getAllExcelField(String[] exclusions, String targetId, | |||||
| Field[] fields, List<ExcelExportEntity> excelParams, | |||||
| Class<?> pojoClass, List<Method> getMethods) throws Exception { | |||||
| List<String> exclusionsList = exclusions != null ? Arrays | |||||
| .asList(exclusions) : null; | |||||
| ExcelExportEntity excelEntity; | |||||
| // 遍历整个filed | |||||
| for (int i = 0; i < fields.length; i++) { | |||||
| Field field = fields[i]; | |||||
| // 先判断是不是collection,在判断是不是java自带对象,之后就是我们自己的对象了 | |||||
| if (POIPublicUtil.isNotUserExcelUserThis(exclusionsList, field, | |||||
| targetId)) { | |||||
| continue; | |||||
| } | |||||
| if (POIPublicUtil.isCollection(field.getType())) { | |||||
| ExcelCollection excel = field | |||||
| .getAnnotation(ExcelCollection.class); | |||||
| ParameterizedType pt = (ParameterizedType) field | |||||
| .getGenericType(); | |||||
| Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0]; | |||||
| List<ExcelExportEntity> list = new ArrayList<ExcelExportEntity>(); | |||||
| getAllExcelField(exclusions, | |||||
| StringUtils.isNotEmpty(excel.id()) ? excel.id() | |||||
| : targetId, POIPublicUtil.getClassFields(clz), | |||||
| list, clz, null); | |||||
| excelEntity = new ExcelExportEntity(); | |||||
| excelEntity.setName(getExcelName(excel.name(), targetId)); | |||||
| excelEntity | |||||
| .setOrderNum(getCellOrder(excel.orderNum(), targetId)); | |||||
| excelEntity.setMethod(POIPublicUtil.getMethod(field.getName(), | |||||
| pojoClass)); | |||||
| excelEntity.setList(list); | |||||
| excelParams.add(excelEntity); | |||||
| } else if (POIPublicUtil.isJavaClass(field)) { | |||||
| excelParams.add(createExcelExportEntity(field, targetId, | |||||
| pojoClass, getMethods)); | |||||
| } else { | |||||
| List<Method> newMethods = new ArrayList<Method>(); | |||||
| if (getMethods != null) { | |||||
| newMethods.addAll(getMethods); | |||||
| } | |||||
| newMethods.add(POIPublicUtil.getMethod(field.getName(), | |||||
| pojoClass)); | |||||
| ExcelEntity excel = field.getAnnotation(ExcelEntity.class); | |||||
| getAllExcelField(exclusions, | |||||
| StringUtils.isNotEmpty(excel.id()) ? excel.id() | |||||
| : targetId, POIPublicUtil.getClassFields(field | |||||
| .getType()), excelParams, field.getType(), | |||||
| newMethods); | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 创建导出实体对象 | |||||
| * | |||||
| * @param field | |||||
| * @param targetId | |||||
| * @param pojoClass | |||||
| * @param getMethods | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| private ExcelExportEntity createExcelExportEntity(Field field, | |||||
| String targetId, Class<?> pojoClass, List<Method> getMethods) | |||||
| throws Exception { | |||||
| Excel excel = field.getAnnotation(Excel.class); | |||||
| ExcelExportEntity excelEntity = new ExcelExportEntity(); | |||||
| excelEntity.setType(excel.type()); | |||||
| getExcelField(targetId, field, excelEntity, excel, pojoClass); | |||||
| if (getMethods != null) { | |||||
| List<Method> newMethods = new ArrayList<Method>(); | |||||
| newMethods.addAll(getMethods); | |||||
| newMethods.add(excelEntity.getMethod()); | |||||
| excelEntity.setMethods(newMethods); | |||||
| } | |||||
| return excelEntity; | |||||
| } | |||||
| /** | |||||
| * 判断在这个单元格显示的名称 | |||||
| * | |||||
| * @param exportName | |||||
| * @param targetId | |||||
| * @return | |||||
| */ | |||||
| public String getExcelName(String exportName, String targetId) { | |||||
| if (exportName.indexOf(",") < 0) { | |||||
| return exportName; | |||||
| } | |||||
| String[] arr = exportName.split(","); | |||||
| for (String str : arr) { | |||||
| if (str.indexOf(targetId) != -1) { | |||||
| return str.split("_")[0]; | |||||
| } | |||||
| } | |||||
| return null; | |||||
| } | |||||
| /** | |||||
| * 获取这个字段的顺序 | |||||
| * | |||||
| * @param orderNum | |||||
| * @param targetId | |||||
| * @return | |||||
| */ | |||||
| public int getCellOrder(String orderNum, String targetId) { | |||||
| if (isInteger(orderNum) || targetId == null) { | |||||
| return Integer.valueOf(orderNum); | |||||
| } | |||||
| String[] arr = orderNum.split(","); | |||||
| String[] temp; | |||||
| for (String str : arr) { | |||||
| temp = str.split("_"); | |||||
| if (targetId.equals(temp[1])) { | |||||
| return Integer.valueOf(temp[0]); | |||||
| } | |||||
| } | |||||
| return 0; | |||||
| } | |||||
| /** | |||||
| * 判断字符串是否是整数 | |||||
| */ | |||||
| public boolean isInteger(String value) { | |||||
| try { | |||||
| Integer.parseInt(value); | |||||
| return true; | |||||
| } catch (NumberFormatException e) { | |||||
| return false; | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 注解到导出对象的转换 | |||||
| * | |||||
| * @param targetId | |||||
| * @param field | |||||
| * @param excelEntity | |||||
| * @param excel | |||||
| * @param pojoClass | |||||
| * @throws Exception | |||||
| */ | |||||
| private void getExcelField(String targetId, Field field, | |||||
| ExcelExportEntity excelEntity, Excel excel, Class<?> pojoClass) | |||||
| throws Exception { | |||||
| excelEntity.setName(getExcelName(excel.name(), targetId)); | |||||
| excelEntity.setWidth(excel.width()); | |||||
| excelEntity.setHeight(excel.height()); | |||||
| excelEntity.setNeedMerge(excel.needMerge()); | |||||
| excelEntity.setMergeVertical(excel.mergeVertical()); | |||||
| excelEntity.setMergeRely(excel.mergeRely()); | |||||
| excelEntity.setReplace(excel.replace()); | |||||
| excelEntity.setOrderNum(getCellOrder(excel.orderNum(), targetId)); | |||||
| excelEntity.setWrap(excel.isWrap()); | |||||
| excelEntity.setExportImageType(excel.imageType()); | |||||
| excelEntity.setDatabaseFormat(excel.databaseFormat()); | |||||
| excelEntity | |||||
| .setFormat(StringUtils.isNotEmpty(excel.exportFormat()) ? excel | |||||
| .exportFormat() : excel.format()); | |||||
| String fieldname = field.getName(); | |||||
| excelEntity.setMethod(POIPublicUtil.getMethod(fieldname, pojoClass)); | |||||
| } | |||||
| /** | |||||
| * 根据注解获取行高 | |||||
| * @param excelParams | |||||
| * @return | |||||
| */ | |||||
| public short getRowHeight(List<ExcelExportEntity> excelParams) { | |||||
| int maxHeight = 0; | |||||
| for (int i = 0; i < excelParams.size(); i++) { | |||||
| maxHeight = maxHeight > excelParams.get(i).getHeight() ? maxHeight | |||||
| : excelParams.get(i).getHeight(); | |||||
| if (excelParams.get(i).getList() != null) { | |||||
| for (int j = 0; j < excelParams.get(i).getList().size(); j++) { | |||||
| maxHeight = maxHeight > excelParams.get(i).getList().get(j) | |||||
| .getHeight() ? maxHeight : excelParams.get(i) | |||||
| .getList().get(j).getHeight(); | |||||
| } | |||||
| } | |||||
| } | |||||
| return (short) (maxHeight * 50); | |||||
| } | |||||
| /** | |||||
| * 对字段根据用户设置排序 | |||||
| */ | |||||
| public void sortAllParams(List<ExcelExportEntity> excelParams) { | |||||
| Collections.sort(excelParams, new ComparatorExcelField()); | |||||
| for (ExcelExportEntity entity : excelParams) { | |||||
| if (entity.getList() != null) { | |||||
| Collections.sort(entity.getList(), new ComparatorExcelField()); | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 获取填如这个cell的值,提供一些附加功能 | |||||
| * | |||||
| * @param entity | |||||
| * @param obj | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| public Object getCellValue(ExcelExportEntity entity, Object obj) | |||||
| throws Exception { | |||||
| Object value = entity.getMethods() != null ? getFieldBySomeMethod( | |||||
| entity.getMethods(), obj) : entity.getMethod().invoke(obj, | |||||
| new Object[] {}); | |||||
| if (needHanlderList != null | |||||
| && needHanlderList.contains(entity.getName())) { | |||||
| value = dataHanlder.exportHandler(obj, entity.getName(), value); | |||||
| } else if (StringUtils.isNotEmpty(entity.getFormat())) { | |||||
| value = formatValue(value, entity); | |||||
| } else if (entity.getReplace() != null | |||||
| && entity.getReplace().length > 0) { | |||||
| value = replaceValue(entity.getReplace(), String.valueOf(value)); | |||||
| } | |||||
| return value == null ? "" : value.toString(); | |||||
| } | |||||
| private Object replaceValue(String[] replace, String value) { | |||||
| String[] temp; | |||||
| for (String str : replace) { | |||||
| temp = str.split("_"); | |||||
| if (value.equals(temp[1])) { | |||||
| value = temp[0]; | |||||
| break; | |||||
| } | |||||
| } | |||||
| return value; | |||||
| } | |||||
| private Object formatValue(Object value, ExcelExportEntity entity) | |||||
| throws Exception { | |||||
| Date temp = null; | |||||
| if (value instanceof String) { | |||||
| SimpleDateFormat format = new SimpleDateFormat( | |||||
| entity.getDatabaseFormat()); | |||||
| temp = format.parse(value.toString()); | |||||
| } else if (value instanceof Date) { | |||||
| temp = (Date) value; | |||||
| } | |||||
| if (temp != null) { | |||||
| SimpleDateFormat format = new SimpleDateFormat(entity.getFormat()); | |||||
| value = format.format(temp); | |||||
| } | |||||
| return value; | |||||
| } | |||||
| /** | |||||
| * 多个反射获取值 | |||||
| * | |||||
| * @param list | |||||
| * @param t | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | |||||
| public Object getFieldBySomeMethod(List<Method> list, Object t) | |||||
| throws Exception { | |||||
| for (Method m : list) { | |||||
| if (t == null) { | |||||
| t = ""; | |||||
| break; | |||||
| } | |||||
| t = m.invoke(t, new Object[] {}); | |||||
| } | |||||
| return t; | |||||
| } | |||||
| } | |||||
| @@ -19,7 +19,7 @@ import org.jeecgframework.poi.cache.ExcelCache; | |||||
| import org.jeecgframework.poi.excel.annotation.ExcelTarget; | import org.jeecgframework.poi.excel.annotation.ExcelTarget; | ||||
| import org.jeecgframework.poi.excel.entity.TemplateExportParams; | import org.jeecgframework.poi.excel.entity.TemplateExportParams; | ||||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | ||||
| import org.jeecgframework.poi.excel.export.ExcelExportBase; | |||||
| import org.jeecgframework.poi.excel.export.base.ExcelExportBase; | |||||
| import org.jeecgframework.poi.exception.excel.ExcelExportException; | import org.jeecgframework.poi.exception.excel.ExcelExportException; | ||||
| import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum; | import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum; | ||||
| import org.jeecgframework.poi.util.POIPublicUtil; | import org.jeecgframework.poi.util.POIPublicUtil; | ||||
| @@ -0,0 +1,27 @@ | |||||
| package org.jeecgframework.poi.exception.word; | |||||
| import org.jeecgframework.poi.exception.word.enmus.WordExportEnum; | |||||
| /** | |||||
| * word导出异常 | |||||
| * | |||||
| * @author JueYue | |||||
| * @date 2014年8月9日 下午10:32:51 | |||||
| */ | |||||
| public class WordExportException extends RuntimeException { | |||||
| private static final long serialVersionUID = 1L; | |||||
| public WordExportException() { | |||||
| super(); | |||||
| } | |||||
| public WordExportException(String msg) { | |||||
| super(msg); | |||||
| } | |||||
| public WordExportException(WordExportEnum exception) { | |||||
| super(exception.getMsg()); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,29 @@ | |||||
| package org.jeecgframework.poi.exception.word.enmus; | |||||
| /** | |||||
| * 导出异常枚举 | |||||
| * | |||||
| * @author JueYue | |||||
| * @date 2014年8月9日 下午10:34:58 | |||||
| */ | |||||
| public enum WordExportEnum { | |||||
| EXCEL_PARAMS_ERROR("Excel 导出 参数错误"), | |||||
| EXCEL_HEAD_HAVA_NULL("Excel 表头 有的字段为空"), | |||||
| EXCEL_NO_HEAD("Excel 没有表头"); | |||||
| private String msg; | |||||
| WordExportEnum(String msg) { | |||||
| this.msg = msg; | |||||
| } | |||||
| public String getMsg() { | |||||
| return msg; | |||||
| } | |||||
| public void setMsg(String msg) { | |||||
| this.msg = msg; | |||||
| } | |||||
| } | |||||
| @@ -29,6 +29,7 @@ public abstract class ExcelDataHandlerDefaultImpl implements IExcelDataHandler { | |||||
| return needHandlerFields; | return needHandlerFields; | ||||
| } | } | ||||
| @Override | |||||
| public void setNeedHandlerFields(String[] needHandlerFields) { | public void setNeedHandlerFields(String[] needHandlerFields) { | ||||
| this.needHandlerFields = needHandlerFields; | this.needHandlerFields = needHandlerFields; | ||||
| } | } | ||||
| @@ -9,13 +9,14 @@ package org.jeecgframework.poi.handler.inter; | |||||
| public interface IExcelDataHandler { | public interface IExcelDataHandler { | ||||
| /** | /** | ||||
| * 获取需要处理的字段,导入和导出统一处理了, | |||||
| * 减少书写的字段 | |||||
| * 获取需要处理的字段,导入和导出统一处理了, 减少书写的字段 | |||||
| * | * | ||||
| * @return | * @return | ||||
| */ | */ | ||||
| public String[] getNeedHandlerFields(); | public String[] getNeedHandlerFields(); | ||||
| public void setNeedHandlerFields(String[] fields); | |||||
| /** | /** | ||||
| * 导出处理方法 | * 导出处理方法 | ||||
| * | * | ||||
| @@ -1,12 +1,22 @@ | |||||
| package org.jeecgframework.poi.handler.inter; | package org.jeecgframework.poi.handler.inter; | ||||
| import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult; | import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult; | ||||
| /** | /** | ||||
| * 导入校验接口 | * 导入校验接口 | ||||
| * | |||||
| * @author JueYue | * @author JueYue | ||||
| * @date 2014年6月23日 下午11:08:21 | * @date 2014年6月23日 下午11:08:21 | ||||
| */ | */ | ||||
| public interface IExcelVerifyHandler { | public interface IExcelVerifyHandler { | ||||
| /** | |||||
| * 获取需要处理的字段,导入和导出统一处理了, 减少书写的字段 | |||||
| * | |||||
| * @return | |||||
| */ | |||||
| public String[] getNeedVerifyFields(); | |||||
| /** | /** | ||||
| * 导出处理方法 | * 导出处理方法 | ||||
| * | * | ||||
| @@ -21,4 +31,11 @@ public interface IExcelVerifyHandler { | |||||
| public ExcelVerifyHanlderResult verifyHandler(Object obj, String name, | public ExcelVerifyHanlderResult verifyHandler(Object obj, String name, | ||||
| Object value); | Object value); | ||||
| /** | |||||
| * 获取需要处理的字段,导入和导出统一处理了, 减少书写的字段 | |||||
| * | |||||
| * @return | |||||
| */ | |||||
| public void setNeedVerifyFields(String[] arr); | |||||
| } | } | ||||
| @@ -1,12 +1,14 @@ | |||||
| package org.jeecgframework.poi.word.entity; | package org.jeecgframework.poi.word.entity; | ||||
| /** | /** | ||||
| * word导出,图片设置和图片信息 | * word导出,图片设置和图片信息 | ||||
| * | |||||
| * @author JueYue | * @author JueYue | ||||
| * @date 2013-11-17 | * @date 2013-11-17 | ||||
| * @version 1.0 | * @version 1.0 | ||||
| */ | */ | ||||
| public class WordImageEntity { | public class WordImageEntity { | ||||
| public static String URL = "url"; | public static String URL = "url"; | ||||
| public static String Data = "data"; | public static String Data = "data"; | ||||
| /** | /** | ||||
| @@ -17,13 +19,30 @@ public class WordImageEntity { | |||||
| * 图片宽度 | * 图片宽度 | ||||
| */ | */ | ||||
| private int width; | private int width; | ||||
| //图片高度 | |||||
| // 图片高度 | |||||
| private int height; | private int height; | ||||
| //图片地址 | |||||
| // 图片地址 | |||||
| private String url; | private String url; | ||||
| //图片信息 | |||||
| // 图片信息 | |||||
| private byte[] data; | private byte[] data; | ||||
| public WordImageEntity() { | |||||
| } | |||||
| public WordImageEntity(String url, int width, int height) { | |||||
| this.url = url; | |||||
| this.width = width; | |||||
| this.height = height; | |||||
| } | |||||
| public WordImageEntity(byte[] data, int width, int height) { | |||||
| this.data = data; | |||||
| this.width = width; | |||||
| this.height = height; | |||||
| this.type = Data; | |||||
| } | |||||
| public String getType() { | public String getType() { | ||||
| return type; | return type; | ||||
| } | } | ||||
| @@ -0,0 +1,69 @@ | |||||
| package org.jeecgframework.poi.word.entity.params; | |||||
| import java.util.List; | |||||
| import org.jeecgframework.poi.excel.entity.ExcelBaseParams; | |||||
| import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | |||||
| /** | |||||
| * Excel 导出对象 | |||||
| * | |||||
| * @author JueYue | |||||
| * @date 2014年8月9日 下午10:21:13 | |||||
| */ | |||||
| public class ExcelListEntity extends ExcelBaseParams { | |||||
| public ExcelListEntity() { | |||||
| } | |||||
| public ExcelListEntity(List<?> list, Class<?> clazz) { | |||||
| this.list = list; | |||||
| this.clazz = clazz; | |||||
| } | |||||
| public ExcelListEntity(List<?> list, Class<?> clazz, | |||||
| IExcelDataHandler dataHanlder) { | |||||
| this.list = list; | |||||
| this.clazz = clazz; | |||||
| setDataHanlder(dataHanlder); | |||||
| } | |||||
| /** | |||||
| * 数据源 | |||||
| */ | |||||
| private List<?> list; | |||||
| /** | |||||
| * 实体类对象 | |||||
| */ | |||||
| private Class<?> clazz; | |||||
| /** | |||||
| * 过滤的属性 | |||||
| */ | |||||
| private String[] exclusions; | |||||
| public List<?> getList() { | |||||
| return list; | |||||
| } | |||||
| public void setList(List<?> list) { | |||||
| this.list = list; | |||||
| } | |||||
| public Class<?> getClazz() { | |||||
| return clazz; | |||||
| } | |||||
| public void setClazz(Class<?> clazz) { | |||||
| this.clazz = clazz; | |||||
| } | |||||
| public String[] getExclusions() { | |||||
| return exclusions; | |||||
| } | |||||
| public void setExclusions(String[] exclusions) { | |||||
| this.exclusions = exclusions; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,78 @@ | |||||
| package org.jeecgframework.poi.word.entity.params; | |||||
| /** | |||||
| * Excel 对象导出结构 | |||||
| * | |||||
| * @author JueYue | |||||
| * @date 2014年7月26日 下午11:14:48 | |||||
| */ | |||||
| public class ListParamEntity { | |||||
| // 唯一值,在遍历中重复使用 | |||||
| public static final String SINGLE = "single"; | |||||
| // 属于数组类型 | |||||
| public static final String LIST = "list"; | |||||
| /** | |||||
| * 属性名称 | |||||
| */ | |||||
| private String name; | |||||
| /** | |||||
| * 目标 | |||||
| */ | |||||
| private String target; | |||||
| /** | |||||
| * 当是唯一值的时候直接求出值 | |||||
| */ | |||||
| private Object value; | |||||
| /** | |||||
| * 数据类型,SINGLE || LIST | |||||
| */ | |||||
| private String type; | |||||
| public ListParamEntity() { | |||||
| } | |||||
| public ListParamEntity(String name, String target) { | |||||
| this.name = name; | |||||
| this.target = target; | |||||
| this.type = LIST; | |||||
| } | |||||
| public ListParamEntity(String name, Object value) { | |||||
| this.name = name; | |||||
| this.value = value; | |||||
| this.type = LIST; | |||||
| } | |||||
| public String getName() { | |||||
| return name; | |||||
| } | |||||
| public void setName(String name) { | |||||
| this.name = name; | |||||
| } | |||||
| public String getTarget() { | |||||
| return target; | |||||
| } | |||||
| public void setTarget(String target) { | |||||
| this.target = target; | |||||
| } | |||||
| public String getType() { | |||||
| return type; | |||||
| } | |||||
| public void setType(String type) { | |||||
| this.type = type; | |||||
| } | |||||
| public Object getValue() { | |||||
| return value; | |||||
| } | |||||
| public void setValue(Object value) { | |||||
| this.value = value; | |||||
| } | |||||
| } | |||||
| @@ -14,6 +14,9 @@ import org.apache.poi.xwpf.usermodel.XWPFTableRow; | |||||
| import org.jeecgframework.poi.cache.WordCache; | import org.jeecgframework.poi.cache.WordCache; | ||||
| import org.jeecgframework.poi.word.entity.JeecgXWPFDocument; | import org.jeecgframework.poi.word.entity.JeecgXWPFDocument; | ||||
| import org.jeecgframework.poi.word.entity.WordImageEntity; | import org.jeecgframework.poi.word.entity.WordImageEntity; | ||||
| import org.jeecgframework.poi.word.entity.params.ExcelListEntity; | |||||
| import org.jeecgframework.poi.word.parse.excel.ExcelEntityParse; | |||||
| import org.jeecgframework.poi.word.parse.excel.ExcelMapParse; | |||||
| import org.jeecgframework.poi.word.util.ParseWordUtil; | import org.jeecgframework.poi.word.util.ParseWordUtil; | ||||
| /** | /** | ||||
| @@ -23,7 +26,7 @@ import org.jeecgframework.poi.word.util.ParseWordUtil; | |||||
| * @date 2013-11-16 | * @date 2013-11-16 | ||||
| * @version 1.0 | * @version 1.0 | ||||
| */ | */ | ||||
| @SuppressWarnings({"unchecked","rawtypes"}) | |||||
| @SuppressWarnings({ "unchecked", "rawtypes" }) | |||||
| public class ParseWord07 { | public class ParseWord07 { | ||||
| /** | /** | ||||
| @@ -34,28 +37,31 @@ public class ParseWord07 { | |||||
| * @return | * @return | ||||
| * @throws Exception | * @throws Exception | ||||
| */ | */ | ||||
| public XWPFDocument parseWord(String url, Map<String, Object> map) | |||||
| public XWPFDocument parseWord(String url, Map<String, Object> map) | |||||
| throws Exception { | throws Exception { | ||||
| JeecgXWPFDocument doc = WordCache.getXWPFDocumen(url); | JeecgXWPFDocument doc = WordCache.getXWPFDocumen(url); | ||||
| parseWordSetValue(doc, map); | parseWordSetValue(doc, map); | ||||
| return doc; | return doc; | ||||
| } | } | ||||
| /** | /** | ||||
| * 按用户模板导出 | |||||
| * @param document | |||||
| * @param map | |||||
| * 解析07版的Word并且进行赋值 | |||||
| * | |||||
| * @Author JueYue | |||||
| * @date 2013-11-16 | |||||
| * @return | |||||
| * @throws Exception | * @throws Exception | ||||
| */ | */ | ||||
| public void parseWord(XWPFDocument document, Map<String, Object> map) throws Exception { | |||||
| parseWordSetValue((JeecgXWPFDocument)document, map); | |||||
| public void parseWord(XWPFDocument document, Map<String, Object> map) | |||||
| throws Exception { | |||||
| parseWordSetValue((JeecgXWPFDocument) document, map); | |||||
| } | } | ||||
| private void parseWordSetValue(JeecgXWPFDocument doc, | |||||
| private void parseWordSetValue(JeecgXWPFDocument doc, | |||||
| Map<String, Object> map) throws Exception { | Map<String, Object> map) throws Exception { | ||||
| //第一步解析文档 | |||||
| parseAllParagraphic(doc.getParagraphs(),map); | |||||
| //第二步解析所有表格 | |||||
| // 第一步解析文档 | |||||
| parseAllParagraphic(doc.getParagraphs(), map); | |||||
| // 第二步解析所有表格 | |||||
| XWPFTable table; | XWPFTable table; | ||||
| Iterator<XWPFTable> itTable = doc.getTablesIterator(); | Iterator<XWPFTable> itTable = doc.getTablesIterator(); | ||||
| while (itTable.hasNext()) { | while (itTable.hasNext()) { | ||||
| @@ -64,18 +70,19 @@ public class ParseWord07 { | |||||
| parseThisTable(table, map); | parseThisTable(table, map); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * 解析所有的文本 | * 解析所有的文本 | ||||
| *@Author JueYue | |||||
| *@date 2013-11-17 | |||||
| *@param paragraphs | |||||
| *@param map | |||||
| * | |||||
| * @Author JueYue | |||||
| * @date 2013-11-17 | |||||
| * @param paragraphs | |||||
| * @param map | |||||
| */ | */ | ||||
| private void parseAllParagraphic(List<XWPFParagraph> paragraphs, | |||||
| Map<String, Object> map) throws Exception { | |||||
| private void parseAllParagraphic(List<XWPFParagraph> paragraphs, | |||||
| Map<String, Object> map) throws Exception { | |||||
| XWPFParagraph paragraph; | XWPFParagraph paragraph; | ||||
| for (int i = 0; i < paragraphs.size(); i++) { | for (int i = 0; i < paragraphs.size(); i++) { | ||||
| paragraph = paragraphs.get(i); | paragraph = paragraphs.get(i); | ||||
| @@ -84,7 +91,7 @@ public class ParseWord07 { | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| @@ -95,92 +102,59 @@ public class ParseWord07 { | |||||
| * @param table | * @param table | ||||
| * @param map | * @param map | ||||
| */ | */ | ||||
| private void parseThisTable(XWPFTable table, Map<String, Object> map) throws Exception { | |||||
| private void parseThisTable(XWPFTable table, Map<String, Object> map) | |||||
| throws Exception { | |||||
| XWPFTableRow row; | XWPFTableRow row; | ||||
| List<XWPFTableCell> cells; | List<XWPFTableCell> cells; | ||||
| Object listobj; | Object listobj; | ||||
| for (int i = 0; i < table.getNumberOfRows(); i++) { | |||||
| row = table.getRow(i); | |||||
| cells = row.getTableCells(); | |||||
| if(cells.size() == 1){ | |||||
| listobj = checkThisTableIsNeedIterator(cells.get(0),map); | |||||
| if(listobj==null){ | |||||
| parseThisRow(cells,map); | |||||
| }else{ | |||||
| table.removeRow(i);//删除这一行 | |||||
| parseNextRowAndAddRow(table,i,(List) listobj); | |||||
| } | |||||
| }else{ | |||||
| parseThisRow(cells,map); | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 解析下一行,并且生成更多的行 | |||||
| *@Author JueYue | |||||
| *@date 2013-11-18 | |||||
| *@param table | |||||
| *@param listobj2 | |||||
| */ | |||||
| private void parseNextRowAndAddRow(XWPFTable table,int index, List<Object> list) throws Exception { | |||||
| XWPFTableRow currentRow = table.getRow(index); | |||||
| String[] params = parseCurrentRowGetParams(currentRow); | |||||
| table.removeRow(index);//移除这一行 | |||||
| int cellIndex = 0;//创建完成对象一行好像多了一个cell | |||||
| for(Object obj :list){ | |||||
| currentRow = table.createRow(); | |||||
| for(cellIndex = 0;cellIndex<currentRow.getTableCells().size();cellIndex++){ | |||||
| currentRow.getTableCells().get(cellIndex).setText( | |||||
| ParseWordUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0).toString()); | |||||
| ExcelEntityParse excelEntityParse = new ExcelEntityParse(); | |||||
| for (int i = 0; i < table.getNumberOfRows(); i++) { | |||||
| row = table.getRow(i); | |||||
| cells = row.getTableCells(); | |||||
| if (cells.size() == 1) { | |||||
| listobj = checkThisTableIsNeedIterator(cells.get(0), map); | |||||
| if (listobj == null) { | |||||
| parseThisRow(cells, map); | |||||
| } else if (listobj instanceof ExcelListEntity) { | |||||
| table.removeRow(i);// 删除这一行 | |||||
| excelEntityParse.parseNextRowAndAddRow(table, i, | |||||
| (ExcelListEntity) listobj); | |||||
| } else { | |||||
| table.removeRow(i);// 删除这一行 | |||||
| ExcelMapParse.parseNextRowAndAddRow(table, i, | |||||
| (List) listobj); | |||||
| } | |||||
| } else { | |||||
| parseThisRow(cells, map); | |||||
| } | } | ||||
| for (;cellIndex<params.length;cellIndex++) { | |||||
| currentRow.createCell().setText( | |||||
| ParseWordUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0).toString()); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| /** | |||||
| * 解析参数行,获取参数列表 | |||||
| *@Author JueYue | |||||
| *@date 2013-11-18 | |||||
| *@param currentRow | |||||
| *@return | |||||
| */ | |||||
| private String[] parseCurrentRowGetParams(XWPFTableRow currentRow) { | |||||
| List<XWPFTableCell> cells = currentRow.getTableCells(); | |||||
| String[] params = new String[cells.size()]; | |||||
| String text; | |||||
| for (int i=0;i<cells.size();i++) { | |||||
| text = cells.get(i).getText(); | |||||
| params[i] = text==null?"":text.trim().replace("{{","").replace("}}",""); | |||||
| } | |||||
| return params; | |||||
| } | |||||
| private void parseThisRow(List<XWPFTableCell> cells, | |||||
| Map<String, Object> map) throws Exception { | |||||
| private void parseThisRow(List<XWPFTableCell> cells, Map<String, Object> map) | |||||
| throws Exception { | |||||
| for (XWPFTableCell cell : cells) { | for (XWPFTableCell cell : cells) { | ||||
| parseAllParagraphic(cell.getParagraphs(),map); | |||||
| } | |||||
| parseAllParagraphic(cell.getParagraphs(), map); | |||||
| } | |||||
| } | } | ||||
| /** | /** | ||||
| *判断是不是迭代输出 | |||||
| *@Author JueYue | |||||
| *@date 2013-11-18 | |||||
| *@return | |||||
| * @throws Exception | |||||
| * 判断是不是迭代输出 | |||||
| * | |||||
| * @Author JueYue | |||||
| * @date 2013-11-18 | |||||
| * @return | |||||
| * @throws Exception | |||||
| */ | */ | ||||
| private Object checkThisTableIsNeedIterator(XWPFTableCell cell | |||||
| , Map<String, Object> map) throws Exception { | |||||
| private Object checkThisTableIsNeedIterator(XWPFTableCell cell, | |||||
| Map<String, Object> map) throws Exception { | |||||
| String text = cell.getText().trim(); | String text = cell.getText().trim(); | ||||
| //判断是不是迭代输出 | |||||
| if(text.startsWith("{{")&&text.endsWith("}}")&&text.indexOf("in ")!=-1){ | |||||
| return ParseWordUtil.getRealValue(text.replace("in ", "").trim(),map); | |||||
| // 判断是不是迭代输出 | |||||
| if (text.startsWith("{{") && text.endsWith("}}") | |||||
| && text.indexOf("in ") != -1) { | |||||
| /*text = text.replace("{{", "").replace("}}", "") | |||||
| .replaceAll("\\s{1,}", " ").trim();*/ | |||||
| return ParseWordUtil.getRealValue(text.replace("in ", "").trim(), | |||||
| map); | |||||
| } | } | ||||
| return null; | return null; | ||||
| } | } | ||||
| @@ -193,8 +167,8 @@ public class ParseWord07 { | |||||
| * @param paragraph | * @param paragraph | ||||
| * @param map | * @param map | ||||
| */ | */ | ||||
| private void parseThisParagraph(XWPFParagraph paragraph, | |||||
| Map<String, Object> map) throws Exception { | |||||
| private void parseThisParagraph(XWPFParagraph paragraph, | |||||
| Map<String, Object> map) throws Exception { | |||||
| XWPFRun run; | XWPFRun run; | ||||
| XWPFRun currentRun = null;// 拿到的第一个run,用来set值,可以保存格式 | XWPFRun currentRun = null;// 拿到的第一个run,用来set值,可以保存格式 | ||||
| String currentText = "";// 存放当前的text | String currentText = "";// 存放当前的text | ||||
| @@ -225,7 +199,7 @@ public class ParseWord07 { | |||||
| currentText = text; | currentText = text; | ||||
| isfinde = true; | isfinde = true; | ||||
| currentRun = run; | currentRun = run; | ||||
| }else{ | |||||
| } else { | |||||
| currentText = ""; | currentText = ""; | ||||
| } | } | ||||
| if (currentText.indexOf("}}") != -1) { | if (currentText.indexOf("}}") != -1) { | ||||
| @@ -243,16 +217,16 @@ public class ParseWord07 { | |||||
| * @Author JueYue | * @Author JueYue | ||||
| * @date 2013-11-16 | * @date 2013-11-16 | ||||
| */ | */ | ||||
| private void changeValues(XWPFParagraph paragraph, | |||||
| XWPFRun currentRun, String currentText, List<Integer> runIndex, | |||||
| Map<String, Object> map) throws Exception { | |||||
| private void changeValues(XWPFParagraph paragraph, XWPFRun currentRun, | |||||
| String currentText, List<Integer> runIndex, Map<String, Object> map) | |||||
| throws Exception { | |||||
| Object obj = ParseWordUtil.getRealValue(currentText, map); | Object obj = ParseWordUtil.getRealValue(currentText, map); | ||||
| if(obj instanceof WordImageEntity){//如果是图片就设置为图片 | |||||
| if (obj instanceof WordImageEntity) {// 如果是图片就设置为图片 | |||||
| currentRun.setText("", 0); | currentRun.setText("", 0); | ||||
| addAnImage((WordImageEntity)obj,currentRun); | |||||
| }else{ | |||||
| addAnImage((WordImageEntity) obj, currentRun); | |||||
| } else { | |||||
| currentText = obj.toString(); | currentText = obj.toString(); | ||||
| currentRun.setText(currentText,0); | |||||
| currentRun.setText(currentText, 0); | |||||
| } | } | ||||
| for (int k = 0; k < runIndex.size(); k++) { | for (int k = 0; k < runIndex.size(); k++) { | ||||
| paragraph.getRuns().get(runIndex.get(k)).setText("", 0); | paragraph.getRuns().get(runIndex.get(k)).setText("", 0); | ||||
| @@ -262,28 +236,38 @@ public class ParseWord07 { | |||||
| /** | /** | ||||
| * 添加图片 | * 添加图片 | ||||
| *@Author JueYue | |||||
| *@date 2013-11-20 | |||||
| *@param obj | |||||
| *@param currentRun | |||||
| * @throws Exception | |||||
| * | |||||
| * @Author JueYue | |||||
| * @date 2013-11-20 | |||||
| * @param obj | |||||
| * @param currentRun | |||||
| * @throws Exception | |||||
| */ | */ | ||||
| private void addAnImage(WordImageEntity obj, XWPFRun currentRun) throws Exception { | |||||
| Object [] isAndType = ParseWordUtil.getIsAndType(obj); | |||||
| private void addAnImage(WordImageEntity obj, XWPFRun currentRun) | |||||
| throws Exception { | |||||
| Object[] isAndType = ParseWordUtil.getIsAndType(obj); | |||||
| String picId; | String picId; | ||||
| try { | try { | ||||
| picId = currentRun.getParagraph().getDocument().addPictureData( | |||||
| (byte[]) isAndType[0], | |||||
| (Integer) isAndType[1]); | |||||
| ((JeecgXWPFDocument) currentRun.getParagraph().getDocument()).createPicture( | |||||
| currentRun,picId, | |||||
| currentRun.getParagraph().getDocument().getNextPicNameNumber((Integer) isAndType[1]), | |||||
| obj.getWidth(),obj.getHeight()); | |||||
| picId = currentRun | |||||
| .getParagraph() | |||||
| .getDocument() | |||||
| .addPictureData((byte[]) isAndType[0], | |||||
| (Integer) isAndType[1]); | |||||
| ((JeecgXWPFDocument) currentRun.getParagraph().getDocument()) | |||||
| .createPicture( | |||||
| currentRun, | |||||
| picId, | |||||
| currentRun | |||||
| .getParagraph() | |||||
| .getDocument() | |||||
| .getNextPicNameNumber( | |||||
| (Integer) isAndType[1]), | |||||
| obj.getWidth(), obj.getHeight()); | |||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| e.printStackTrace(); | e.printStackTrace(); | ||||
| } | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -0,0 +1,208 @@ | |||||
| package org.jeecgframework.poi.word.parse.excel; | |||||
| import java.lang.reflect.Field; | |||||
| import java.util.ArrayList; | |||||
| import java.util.Collection; | |||||
| import java.util.HashMap; | |||||
| import java.util.Iterator; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| import org.apache.commons.lang.StringUtils; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFTable; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFTableCell; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFTableRow; | |||||
| import org.jeecgframework.poi.excel.annotation.ExcelTarget; | |||||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | |||||
| import org.jeecgframework.poi.excel.export.base.ExportBase; | |||||
| import org.jeecgframework.poi.exception.word.WordExportException; | |||||
| import org.jeecgframework.poi.exception.word.enmus.WordExportEnum; | |||||
| import org.jeecgframework.poi.util.POIPublicUtil; | |||||
| import org.jeecgframework.poi.word.entity.params.ExcelListEntity; | |||||
| /** | |||||
| * 解析实体类对象 复用注解 | |||||
| * | |||||
| * @author JueYue | |||||
| * @date 2014年8月9日 下午10:30:57 | |||||
| */ | |||||
| public class ExcelEntityParse extends ExportBase { | |||||
| /** | |||||
| * 解析上一行并生成更多行 | |||||
| * | |||||
| * @param table | |||||
| * @param i | |||||
| * @param listobj | |||||
| */ | |||||
| public void parseNextRowAndAddRow(XWPFTable table, int index, | |||||
| ExcelListEntity entity) { | |||||
| checkExcelParams(entity); | |||||
| // 获取表头数据 | |||||
| Map<String, Integer> titlemap = getTitleMap(table, index); | |||||
| try { | |||||
| // 得到所有字段 | |||||
| Field fileds[] = POIPublicUtil.getClassFields(entity.getClazz()); | |||||
| ExcelTarget etarget = entity.getClazz().getAnnotation( | |||||
| ExcelTarget.class); | |||||
| String targetId = null; | |||||
| if (etarget != null) { | |||||
| targetId = etarget.value(); | |||||
| } | |||||
| // 获取实体对象的导出数据 | |||||
| List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | |||||
| getAllExcelField(null, targetId, fileds, excelParams, | |||||
| entity.getClazz(), null); | |||||
| // 根据表头进行筛选排序 | |||||
| sortAndFilterExportField(excelParams, titlemap); | |||||
| short rowHeight = getRowHeight(excelParams); | |||||
| Iterator<?> its = entity.getList().iterator(); | |||||
| while (its.hasNext()) { | |||||
| Object t = its.next(); | |||||
| index += createCells(index, t, excelParams, table, rowHeight); | |||||
| } | |||||
| } catch (Exception e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| } | |||||
| private int createCells(int index, Object t, | |||||
| List<ExcelExportEntity> excelParams, XWPFTable table, | |||||
| short rowHeight) throws Exception { | |||||
| ExcelExportEntity entity; | |||||
| XWPFTableRow row = table.createRow(); | |||||
| row.setHeight(rowHeight); | |||||
| int maxHeight = 1, cellNum = 0; | |||||
| for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| if (entity.getList() != null) { | |||||
| Collection<?> list = (Collection<?>) entity.getMethod().invoke( | |||||
| t, new Object[] {}); | |||||
| int listC = 0; | |||||
| for (Object obj : list) { | |||||
| createListCells(index + listC, cellNum, obj, | |||||
| entity.getList(), table); | |||||
| listC++; | |||||
| } | |||||
| cellNum += entity.getList().size(); | |||||
| if (list != null && list.size() > maxHeight) { | |||||
| maxHeight = list.size(); | |||||
| } | |||||
| } else { | |||||
| Object value = getCellValue(entity, t); | |||||
| if (entity.getType() == 1) { | |||||
| setCellValue(row, value, cellNum++); | |||||
| } | |||||
| } | |||||
| } | |||||
| // 合并需要合并的单元格 | |||||
| cellNum = 0; | |||||
| for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| if (entity.getList() != null) { | |||||
| cellNum += entity.getList().size(); | |||||
| } else if (entity.isNeedMerge()) { | |||||
| table.setCellMargins(index, index + maxHeight - 1, cellNum, | |||||
| cellNum); | |||||
| cellNum++; | |||||
| } | |||||
| } | |||||
| return maxHeight; | |||||
| } | |||||
| /** | |||||
| * 创建List之后的各个Cells | |||||
| * | |||||
| * @param styles | |||||
| */ | |||||
| public void createListCells(int index, int cellNum, Object obj, | |||||
| List<ExcelExportEntity> excelParams, XWPFTable table) | |||||
| throws Exception { | |||||
| ExcelExportEntity entity; | |||||
| XWPFTableRow row; | |||||
| if (table.getRow(index) == null) { | |||||
| row = table.createRow(); | |||||
| row.setHeight(getRowHeight(excelParams)); | |||||
| } else { | |||||
| row = table.getRow(index); | |||||
| } | |||||
| for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { | |||||
| entity = excelParams.get(k); | |||||
| Object value = getCellValue(entity, obj); | |||||
| if (entity.getType() == 1) { | |||||
| setCellValue(row, value, cellNum++); | |||||
| } | |||||
| } | |||||
| } | |||||
| private void setCellValue(XWPFTableRow row, Object value, int cellNum) { | |||||
| if (row.getCell(cellNum++) != null) { | |||||
| row.getCell(cellNum - 1).setText( | |||||
| value == null ? "" : value.toString()); | |||||
| } else { | |||||
| row.createCell().setText(value == null ? "" : value.toString()); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 对导出序列进行排序和塞选 | |||||
| * | |||||
| * @param excelParams | |||||
| * @param titlemap | |||||
| * @return | |||||
| */ | |||||
| private void sortAndFilterExportField(List<ExcelExportEntity> excelParams, | |||||
| Map<String, Integer> titlemap) { | |||||
| for (int i = excelParams.size() - 1; i >= 0; i--) { | |||||
| if (excelParams.get(i).getList() != null | |||||
| && excelParams.get(i).getList().size() > 0) { | |||||
| sortAndFilterExportField(excelParams.get(i).getList(), titlemap); | |||||
| if (excelParams.get(i).getList().size() == 0) { | |||||
| excelParams.remove(i); | |||||
| } else { | |||||
| excelParams.get(i).setOrderNum(i); | |||||
| } | |||||
| } else { | |||||
| if (titlemap.containsKey(excelParams.get(i).getName())) { | |||||
| excelParams.get(i).setOrderNum(i); | |||||
| } else { | |||||
| excelParams.remove(i); | |||||
| } | |||||
| } | |||||
| } | |||||
| sortAllParams(excelParams); | |||||
| } | |||||
| /** | |||||
| * 获取表头数据 | |||||
| * | |||||
| * @param table | |||||
| * @param index | |||||
| * @return | |||||
| */ | |||||
| private Map<String, Integer> getTitleMap(XWPFTable table, int index) { | |||||
| if (index == 0) { | |||||
| throw new WordExportException(WordExportEnum.EXCEL_NO_HEAD); | |||||
| } | |||||
| List<XWPFTableCell> cells = table.getRow(index - 1).getTableCells(); | |||||
| Map<String, Integer> map = new HashMap<String, Integer>(cells.size()); | |||||
| String text; | |||||
| for (int i = 0; i < cells.size(); i++) { | |||||
| text = cells.get(i).getText(); | |||||
| if (StringUtils.isEmpty(text)) { | |||||
| throw new WordExportException( | |||||
| WordExportEnum.EXCEL_HEAD_HAVA_NULL); | |||||
| } | |||||
| map.put(text, i); | |||||
| } | |||||
| return map; | |||||
| } | |||||
| private static void checkExcelParams(ExcelListEntity entity) { | |||||
| if (entity.getList() == null || entity.getClazz() == null) { | |||||
| throw new WordExportException(WordExportEnum.EXCEL_PARAMS_ERROR); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,71 @@ | |||||
| package org.jeecgframework.poi.word.parse.excel; | |||||
| import java.util.List; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFTable; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFTableCell; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFTableRow; | |||||
| import org.jeecgframework.poi.word.util.ParseWordUtil; | |||||
| /** | |||||
| * 处理和生成Map 类型的数据变成表格 | |||||
| * @author JueYue | |||||
| * @date 2014年8月9日 下午10:28:46 | |||||
| */ | |||||
| public final class ExcelMapParse { | |||||
| /** | |||||
| * 解析下一行,并且生成更多的行 | |||||
| * | |||||
| * @Author JueYue | |||||
| * @date 2013-11-18 | |||||
| * @param table | |||||
| * @param listobj2 | |||||
| */ | |||||
| public static void parseNextRowAndAddRow(XWPFTable table, int index, | |||||
| List<Object> list) throws Exception { | |||||
| XWPFTableRow currentRow = table.getRow(index); | |||||
| String[] params = parseCurrentRowGetParams(currentRow); | |||||
| table.removeRow(index);// 移除这一行 | |||||
| int cellIndex = 0;// 创建完成对象一行好像多了一个cell | |||||
| for (Object obj : list) { | |||||
| currentRow = table.createRow(); | |||||
| for (cellIndex = 0; cellIndex < currentRow.getTableCells().size(); cellIndex++) { | |||||
| currentRow | |||||
| .getTableCells() | |||||
| .get(cellIndex) | |||||
| .setText( | |||||
| ParseWordUtil.getValueDoWhile(obj, | |||||
| params[cellIndex].split("\\."), 0) | |||||
| .toString()); | |||||
| } | |||||
| for (; cellIndex < params.length; cellIndex++) { | |||||
| currentRow.createCell().setText( | |||||
| ParseWordUtil.getValueDoWhile(obj, | |||||
| params[cellIndex].split("\\."), 0).toString()); | |||||
| } | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 解析参数行,获取参数列表 | |||||
| * | |||||
| * @Author JueYue | |||||
| * @date 2013-11-18 | |||||
| * @param currentRow | |||||
| * @return | |||||
| */ | |||||
| private static String[] parseCurrentRowGetParams(XWPFTableRow currentRow) { | |||||
| List<XWPFTableCell> cells = currentRow.getTableCells(); | |||||
| String[] params = new String[cells.size()]; | |||||
| String text; | |||||
| for (int i = 0; i < cells.size(); i++) { | |||||
| text = cells.get(i).getText(); | |||||
| params[i] = text == null ? "" : text.trim().replace("{{", "") | |||||
| .replace("}}", ""); | |||||
| } | |||||
| return params; | |||||
| } | |||||
| } | |||||
| @@ -12,10 +12,12 @@ import javax.imageio.ImageIO; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFDocument; | import org.apache.poi.xwpf.usermodel.XWPFDocument; | ||||
| import org.jeecgframework.poi.util.POIPublicUtil; | import org.jeecgframework.poi.util.POIPublicUtil; | ||||
| import org.jeecgframework.poi.word.entity.WordImageEntity; | import org.jeecgframework.poi.word.entity.WordImageEntity; | ||||
| import org.jeecgframework.poi.word.entity.params.ExcelListEntity; | |||||
| /** | /** | ||||
| * 解析公告类 | * 解析公告类 | ||||
| * Excel 实体注解,暂时不支持图片 | |||||
| * @author JueYue | * @author JueYue | ||||
| * @date 2014-7-24 | * @date 2014-7-24 | ||||
| * @version 1.1 | * @version 1.1 | ||||
| @@ -37,7 +39,7 @@ public class ParseWordUtil { | |||||
| currentText.indexOf("}}")); | currentText.indexOf("}}")); | ||||
| Object obj = getParamsValue(params.trim(),map); | Object obj = getParamsValue(params.trim(),map); | ||||
| //判断图片或者是集合 | //判断图片或者是集合 | ||||
| if(obj instanceof WordImageEntity||obj instanceof List){ | |||||
| if(obj instanceof WordImageEntity||obj instanceof List ||obj instanceof ExcelListEntity){ | |||||
| return obj; | return obj; | ||||
| }else{ | }else{ | ||||
| currentText = currentText.replace("{{" + params + "}}", | currentText = currentText.replace("{{" + params + "}}", | ||||
| @@ -21,5 +21,11 @@ | |||||
| <attribute name="maven.pomderived" value="true"/> | <attribute name="maven.pomderived" value="true"/> | ||||
| </attributes> | </attributes> | ||||
| </classpathentry> | </classpathentry> | ||||
| <classpathentry kind="src" output="target/test-classes" path="src/test/java"> | |||||
| <attributes> | |||||
| <attribute name="optional" value="true"/> | |||||
| <attribute name="maven.pomderived" value="true"/> | |||||
| </attributes> | |||||
| </classpathentry> | |||||
| <classpathentry kind="output" path="target/classes"/> | <classpathentry kind="output" path="target/classes"/> | ||||
| </classpath> | </classpath> | ||||
| @@ -4,7 +4,7 @@ | |||||
| <parent> | <parent> | ||||
| <groupId>org.jeecgframework</groupId> | <groupId>org.jeecgframework</groupId> | ||||
| <artifactId>easypoi</artifactId> | <artifactId>easypoi</artifactId> | ||||
| <version>2.0.1</version> | |||||
| <version>2.0.2</version> | |||||
| </parent> | </parent> | ||||
| <artifactId>easypoi-test</artifactId> | <artifactId>easypoi-test</artifactId> | ||||
| <dependencies> | <dependencies> | ||||
| @@ -0,0 +1,13 @@ | |||||
| package org.jeecgframework.poi; | |||||
| public class UtilTest { | |||||
| public static void main(String[] args) { | |||||
| String text = " {{ p in pList}}"; | |||||
| text = text.replace("{{", "").replace("}}", "").replaceAll("\\s{1,}", " ").trim(); | |||||
| System.out.println(text); | |||||
| System.out.println(text.length()); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,119 @@ | |||||
| package org.jeecgframework.poi.word; | |||||
| import static org.junit.Assert.*; | |||||
| import java.io.FileOutputStream; | |||||
| import java.text.SimpleDateFormat; | |||||
| import java.util.ArrayList; | |||||
| import java.util.Date; | |||||
| import java.util.HashMap; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFDocument; | |||||
| import org.jeecgframework.poi.entity.CourseEntity; | |||||
| import org.jeecgframework.poi.entity.StudentEntity; | |||||
| import org.jeecgframework.poi.entity.TeacherEntity; | |||||
| import org.jeecgframework.poi.word.entity.Person; | |||||
| import org.jeecgframework.poi.word.entity.WordImageEntity; | |||||
| import org.jeecgframework.poi.word.entity.params.ExcelListEntity; | |||||
| import org.junit.Before; | |||||
| import org.junit.Test; | |||||
| /** | |||||
| * 注解导出测试 | |||||
| * | |||||
| * @author JueYue | |||||
| * @date 2014年8月9日 下午11:36:33 | |||||
| */ | |||||
| public class WordExportUtilAnnExcelTest { | |||||
| private static SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd"); | |||||
| List<CourseEntity> list = new ArrayList<CourseEntity>(); | |||||
| CourseEntity courseEntity; | |||||
| @Before | |||||
| public void testBefore() { | |||||
| courseEntity = new CourseEntity(); | |||||
| courseEntity.setId("1131"); | |||||
| courseEntity.setName("小白"); | |||||
| TeacherEntity teacherEntity = new TeacherEntity(); | |||||
| teacherEntity.setId("12131231"); | |||||
| teacherEntity.setName("你们"); | |||||
| courseEntity.setTeacher(teacherEntity); | |||||
| teacherEntity = new TeacherEntity(); | |||||
| teacherEntity.setId("121312314312421131"); | |||||
| teacherEntity.setName("老王"); | |||||
| courseEntity.setShuxueteacher(teacherEntity); | |||||
| StudentEntity studentEntity = new StudentEntity(); | |||||
| studentEntity.setId("11231"); | |||||
| studentEntity.setName("撒旦法司法局"); | |||||
| studentEntity.setBirthday(new Date()); | |||||
| studentEntity.setSex(1); | |||||
| List<StudentEntity> studentList = new ArrayList<StudentEntity>(); | |||||
| studentList.add(studentEntity); | |||||
| studentList.add(studentEntity); | |||||
| courseEntity.setStudents(studentList); | |||||
| for (int i = 0; i < 3; i++) { | |||||
| list.add(courseEntity); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 简单导出没有图片和Excel | |||||
| */ | |||||
| //@Test | |||||
| public void SimpleWordExport() { | |||||
| Map<String, Object> map = new HashMap<String, Object>(); | |||||
| map.put("department", "Jeecg"); | |||||
| map.put("person", "JueYue"); | |||||
| map.put("auditPerson", "JueYue"); | |||||
| map.put("time", format.format(new Date())); | |||||
| List<Person> list = new ArrayList<Person>(); | |||||
| Person p = new Person(); | |||||
| p.setName("小明"); | |||||
| p.setTel("18711111111"); | |||||
| p.setEmail("18711111111@139.com"); | |||||
| list.add(p); | |||||
| p = new Person(); | |||||
| p.setName("小红"); | |||||
| p.setTel("18711111112"); | |||||
| p.setEmail("18711111112@139.com"); | |||||
| list.add(p); | |||||
| map.put("pList", new ExcelListEntity(list, Person.class)); | |||||
| try { | |||||
| XWPFDocument doc = WordExportUtil.exportWord07( | |||||
| "org/jeecgframework/poi/word/doc/SimpleExcel.docx", map); | |||||
| FileOutputStream fos = new FileOutputStream("d:/simpleExcel.docx"); | |||||
| doc.write(fos); | |||||
| fos.close(); | |||||
| } catch (Exception e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| } | |||||
| @Test | |||||
| public void WordExport() { | |||||
| Map<String, Object> map = new HashMap<String, Object>(); | |||||
| map.put("department", "Jeecg"); | |||||
| map.put("person", "JueYue"); | |||||
| map.put("auditPerson", "JueYue"); | |||||
| map.put("time", format.format(new Date())); | |||||
| map.put("cs", new ExcelListEntity(list, CourseEntity.class)); | |||||
| try { | |||||
| XWPFDocument doc = WordExportUtil.exportWord07( | |||||
| "org/jeecgframework/poi/word/doc/Excel.docx", map); | |||||
| FileOutputStream fos = new FileOutputStream("d:/Excel.docx"); | |||||
| doc.write(fos); | |||||
| fos.close(); | |||||
| } catch (Exception e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,79 @@ | |||||
| package org.jeecgframework.poi.word; | |||||
| import static org.junit.Assert.*; | |||||
| import java.io.FileOutputStream; | |||||
| import java.text.SimpleDateFormat; | |||||
| import java.util.ArrayList; | |||||
| import java.util.Date; | |||||
| import java.util.HashMap; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFDocument; | |||||
| import org.jeecgframework.poi.word.entity.Person; | |||||
| import org.jeecgframework.poi.word.entity.WordImageEntity; | |||||
| import org.junit.Test; | |||||
| public class WordExportUtilBaseExcelTest { | |||||
| private static SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd"); | |||||
| /** | |||||
| * 简单导出没有图片和Excel | |||||
| */ | |||||
| @Test | |||||
| public void SimpleWordExport() { | |||||
| Map<String, Object> map = new HashMap<String, Object>(); | |||||
| map.put("department", "Jeecg"); | |||||
| map.put("person", "JueYue"); | |||||
| map.put("auditPerson", "JueYue"); | |||||
| map.put("time", format.format(new Date())); | |||||
| List<Person> list = new ArrayList<Person>(); | |||||
| Person p = new Person(); | |||||
| p.setName("小明"); | |||||
| p.setTel("18711111111"); | |||||
| p.setEmail("18711111111@139.com"); | |||||
| list.add(p); | |||||
| p = new Person(); | |||||
| p.setName("小红"); | |||||
| p.setTel("18711111112"); | |||||
| p.setEmail("18711111112@139.com"); | |||||
| list.add(p); | |||||
| map.put("pList", list); | |||||
| try { | |||||
| XWPFDocument doc = WordExportUtil.exportWord07( | |||||
| "org/jeecgframework/poi/word/doc/SimpleExcel.docx", map); | |||||
| FileOutputStream fos = new FileOutputStream("d:/simpleExcel.docx"); | |||||
| doc.write(fos); | |||||
| fos.close(); | |||||
| } catch (Exception e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 简单导出包含图片 | |||||
| */ | |||||
| //@Test | |||||
| public void imageWordExport() { | |||||
| Map<String, Object> map = new HashMap<String, Object>(); | |||||
| map.put("department", "Jeecg"); | |||||
| map.put("person", "JueYue"); | |||||
| map.put("time", format.format(new Date())); | |||||
| WordImageEntity image = new WordImageEntity(); | |||||
| image.setHeight(200); | |||||
| image.setWidth(500); | |||||
| image.setUrl("org/jeecgframework/poi/word/img/testCode.png"); | |||||
| image.setType(WordImageEntity.URL); | |||||
| map.put("testCode", image); | |||||
| try { | |||||
| XWPFDocument doc = WordExportUtil.exportWord07( | |||||
| "org/jeecgframework/poi/word/doc/Image.docx", map); | |||||
| FileOutputStream fos = new FileOutputStream("d:/image.docx"); | |||||
| doc.write(fos); | |||||
| fos.close(); | |||||
| } catch (Exception e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,43 @@ | |||||
| package org.jeecgframework.poi.word.entity; | |||||
| import org.jeecgframework.poi.excel.annotation.Excel; | |||||
| /** | |||||
| * 测试人员类 | |||||
| * @author JueYue | |||||
| * @date 2014年7月26日 下午10:51:30 | |||||
| */ | |||||
| public class Person { | |||||
| @Excel(name = "姓名") | |||||
| private String name; | |||||
| @Excel(name = "手机") | |||||
| private String tel; | |||||
| @Excel(name = "Email") | |||||
| private String email; | |||||
| public String getName() { | |||||
| return name; | |||||
| } | |||||
| public void setName(String name) { | |||||
| this.name = name; | |||||
| } | |||||
| public String getTel() { | |||||
| return tel; | |||||
| } | |||||
| public void setTel(String tel) { | |||||
| this.tel = tel; | |||||
| } | |||||
| public String getEmail() { | |||||
| return email; | |||||
| } | |||||
| public void setEmail(String email) { | |||||
| this.email = email; | |||||
| } | |||||
| } | |||||
| @@ -4,7 +4,7 @@ | |||||
| <parent> | <parent> | ||||
| <groupId>org.jeecgframework</groupId> | <groupId>org.jeecgframework</groupId> | ||||
| <artifactId>easypoi</artifactId> | <artifactId>easypoi</artifactId> | ||||
| <version>2.0.1</version> | |||||
| <version>2.0.2</version> | |||||
| </parent> | </parent> | ||||
| <artifactId>easypoi-web</artifactId> | <artifactId>easypoi-web</artifactId> | ||||
| @@ -3,7 +3,7 @@ | |||||
| <modelVersion>4.0.0</modelVersion> | <modelVersion>4.0.0</modelVersion> | ||||
| <groupId>org.jeecgframework</groupId> | <groupId>org.jeecgframework</groupId> | ||||
| <artifactId>easypoi</artifactId> | <artifactId>easypoi</artifactId> | ||||
| <version>2.0.1</version> | |||||
| <version>2.0.2</version> | |||||
| <packaging>pom</packaging> | <packaging>pom</packaging> | ||||
| <name>easypoi</name> | <name>easypoi</name> | ||||
| <modules> | <modules> | ||||
| @@ -122,7 +122,7 @@ | |||||
| <dependency> | <dependency> | ||||
| <groupId>org.jeecgframework</groupId> | <groupId>org.jeecgframework</groupId> | ||||
| <artifactId>easypoi-base</artifactId> | <artifactId>easypoi-base</artifactId> | ||||
| <version>2.0.1</version> | |||||
| <version>2.0.2</version> | |||||
| </dependency> | </dependency> | ||||
| </dependencies> | </dependencies> | ||||
| </dependencyManagement> | </dependencyManagement> | ||||