| @@ -7,6 +7,8 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | |||||
| import org.apache.poi.ss.usermodel.Workbook; | import org.apache.poi.ss.usermodel.Workbook; | ||||
| import org.apache.poi.ss.usermodel.WorkbookFactory; | import org.apache.poi.ss.usermodel.WorkbookFactory; | ||||
| import org.jeecgframework.poi.cache.manager.POICacheManager; | import org.jeecgframework.poi.cache.manager.POICacheManager; | ||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| /** | /** | ||||
| * Excel类型的缓存 | * Excel类型的缓存 | ||||
| @@ -17,6 +19,8 @@ import org.jeecgframework.poi.cache.manager.POICacheManager; | |||||
| */ | */ | ||||
| public final class ExcelCache { | public final class ExcelCache { | ||||
| private static final Logger LOGGER = LoggerFactory.getLogger(ExcelCache.class); | |||||
| public static Workbook getWorkbook(String url, int index) { | public static Workbook getWorkbook(String url, int index) { | ||||
| InputStream is = null; | InputStream is = null; | ||||
| try { | try { | ||||
| @@ -30,14 +34,14 @@ public final class ExcelCache { | |||||
| } | } | ||||
| return wb; | return wb; | ||||
| } catch (InvalidFormatException e) { | } catch (InvalidFormatException e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(), e.fillInStackTrace()); | |||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(), e.fillInStackTrace()); | |||||
| } finally { | } finally { | ||||
| try { | try { | ||||
| is.close(); | is.close(); | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(), e.fillInStackTrace()); | |||||
| } | } | ||||
| } | } | ||||
| return null; | return null; | ||||
| @@ -4,7 +4,9 @@ import java.io.IOException; | |||||
| import java.io.InputStream; | import java.io.InputStream; | ||||
| import org.jeecgframework.poi.cache.manager.POICacheManager; | import org.jeecgframework.poi.cache.manager.POICacheManager; | ||||
| import org.jeecgframework.poi.word.entity.JeecgXWPFDocument; | |||||
| import org.jeecgframework.poi.word.entity.MyXWPFDocument; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| /** | /** | ||||
| * word 缓存中心 | * word 缓存中心 | ||||
| @@ -14,19 +16,21 @@ import org.jeecgframework.poi.word.entity.JeecgXWPFDocument; | |||||
| */ | */ | ||||
| public class WordCache { | public class WordCache { | ||||
| public static JeecgXWPFDocument getXWPFDocumen(String url) { | |||||
| private static final Logger LOGGER = LoggerFactory.getLogger(WordCache.class); | |||||
| public static MyXWPFDocument getXWPFDocumen(String url) { | |||||
| InputStream is = null; | InputStream is = null; | ||||
| try { | try { | ||||
| is = POICacheManager.getFile(url); | is = POICacheManager.getFile(url); | ||||
| JeecgXWPFDocument doc = new JeecgXWPFDocument(is); | |||||
| MyXWPFDocument doc = new MyXWPFDocument(is); | |||||
| return doc; | return doc; | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(), e.fillInStackTrace()); | |||||
| } finally { | } finally { | ||||
| try { | try { | ||||
| is.close(); | is.close(); | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(), e.fillInStackTrace()); | |||||
| } | } | ||||
| } | } | ||||
| return null; | return null; | ||||
| @@ -17,7 +17,7 @@ import org.slf4j.LoggerFactory; | |||||
| */ | */ | ||||
| class FileLoade { | class FileLoade { | ||||
| private static final Logger logger = LoggerFactory.getLogger(FileLoade.class); | |||||
| private static final Logger LOGGER = LoggerFactory.getLogger(FileLoade.class); | |||||
| public byte[] getFile(String url) { | public byte[] getFile(String url) { | ||||
| FileInputStream fileis = null; | FileInputStream fileis = null; | ||||
| @@ -34,9 +34,9 @@ class FileLoade { | |||||
| baos.flush(); | baos.flush(); | ||||
| return baos.toByteArray(); | return baos.toByteArray(); | ||||
| } catch (FileNotFoundException e) { | } catch (FileNotFoundException e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(), e.fillInStackTrace()); | |||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(), e.fillInStackTrace()); | |||||
| } finally { | } finally { | ||||
| try { | try { | ||||
| if (fileis != null) | if (fileis != null) | ||||
| @@ -44,10 +44,10 @@ class FileLoade { | |||||
| if (fileis != null) | if (fileis != null) | ||||
| baos.close(); | baos.close(); | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(), e.fillInStackTrace()); | |||||
| } | } | ||||
| } | } | ||||
| logger.error(fileis + "这个路径文件没有找到,请查询"); | |||||
| LOGGER.error(fileis + "这个路径文件没有找到,请查询"); | |||||
| return null; | return null; | ||||
| } | } | ||||
| @@ -6,6 +6,9 @@ import java.util.Arrays; | |||||
| import java.util.concurrent.ExecutionException; | import java.util.concurrent.ExecutionException; | ||||
| import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | ||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import com.google.common.cache.CacheBuilder; | import com.google.common.cache.CacheBuilder; | ||||
| import com.google.common.cache.CacheLoader; | import com.google.common.cache.CacheLoader; | ||||
| import com.google.common.cache.LoadingCache; | import com.google.common.cache.LoadingCache; | ||||
| @@ -19,6 +22,9 @@ import com.google.common.cache.LoadingCache; | |||||
| */ | */ | ||||
| public final class POICacheManager { | public final class POICacheManager { | ||||
| private static final Logger LOGGER = LoggerFactory | |||||
| .getLogger(POICacheManager.class); | |||||
| private static LoadingCache<String, byte[]> loadingCache; | private static LoadingCache<String, byte[]> loadingCache; | ||||
| static { | static { | ||||
| @@ -37,7 +43,7 @@ public final class POICacheManager { | |||||
| byte[] result = Arrays.copyOf(loadingCache.get(id), loadingCache.get(id).length); | byte[] result = Arrays.copyOf(loadingCache.get(id), loadingCache.get(id).length); | ||||
| return new ByteArrayInputStream(result); | return new ByteArrayInputStream(result); | ||||
| } catch (ExecutionException e) { | } catch (ExecutionException e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(), e.fillInStackTrace()); | |||||
| } | } | ||||
| return null; | return null; | ||||
| } | } | ||||
| @@ -9,6 +9,8 @@ import java.util.List; | |||||
| import org.jeecgframework.poi.excel.entity.ImportParams; | import org.jeecgframework.poi.excel.entity.ImportParams; | ||||
| import org.jeecgframework.poi.excel.entity.result.ExcelImportResult; | import org.jeecgframework.poi.excel.entity.result.ExcelImportResult; | ||||
| import org.jeecgframework.poi.excel.imports.ExcelImportServer; | import org.jeecgframework.poi.excel.imports.ExcelImportServer; | ||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| /** | /** | ||||
| * Excel 导入工具 | * Excel 导入工具 | ||||
| @@ -20,6 +22,8 @@ import org.jeecgframework.poi.excel.imports.ExcelImportServer; | |||||
| @SuppressWarnings({ "unchecked" }) | @SuppressWarnings({ "unchecked" }) | ||||
| public class ExcelImportUtil { | public class ExcelImportUtil { | ||||
| private static final Logger LOGGER = LoggerFactory.getLogger(ExcelImportUtil.class); | |||||
| /** | /** | ||||
| * Excel 导入 数据源本地文件,不返回校验结果 导入 字 段类型 Integer,Long,Double,Date,String,Boolean | * Excel 导入 数据源本地文件,不返回校验结果 导入 字 段类型 Integer,Long,Double,Date,String,Boolean | ||||
| * | * | ||||
| @@ -36,12 +40,12 @@ public class ExcelImportUtil { | |||||
| in = new FileInputStream(file); | in = new FileInputStream(file); | ||||
| result = new ExcelImportServer().importExcelByIs(in, pojoClass, params).getList(); | result = new ExcelImportServer().importExcelByIs(in, pojoClass, params).getList(); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(),e.fillInStackTrace()); | |||||
| } finally { | } finally { | ||||
| try { | try { | ||||
| in.close(); | in.close(); | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(),e.fillInStackTrace()); | |||||
| } | } | ||||
| } | } | ||||
| return result; | return result; | ||||
| @@ -92,12 +96,12 @@ public class ExcelImportUtil { | |||||
| in = new FileInputStream(file); | in = new FileInputStream(file); | ||||
| return new ExcelImportServer().importExcelByIs(in, pojoClass, params); | return new ExcelImportServer().importExcelByIs(in, pojoClass, params); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(),e.fillInStackTrace()); | |||||
| } finally { | } finally { | ||||
| try { | try { | ||||
| in.close(); | in.close(); | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(),e.fillInStackTrace()); | |||||
| } | } | ||||
| } | } | ||||
| return null; | return null; | ||||
| @@ -23,6 +23,8 @@ 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; | ||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| /** | /** | ||||
| * Excel 导出根据模板导出 | * Excel 导出根据模板导出 | ||||
| @@ -32,6 +34,8 @@ import org.jeecgframework.poi.util.POIPublicUtil; | |||||
| * @version 1.0 | * @version 1.0 | ||||
| */ | */ | ||||
| public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | ||||
| private static final Logger LOGGER = LoggerFactory.getLogger(ExcelExportOfTemplateUtil.class); | |||||
| /** | /** | ||||
| * 往Sheet 填充正常数据,根据表头信息 使用导入的部分逻辑,坐对象映射 | * 往Sheet 填充正常数据,根据表头信息 使用导入的部分逻辑,坐对象映射 | ||||
| @@ -48,7 +52,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| Map<String, Integer> titlemap = getTitleMap(params, sheet); | Map<String, Integer> titlemap = getTitleMap(params, sheet); | ||||
| Drawing patriarch = sheet.createDrawingPatriarch(); | Drawing patriarch = sheet.createDrawingPatriarch(); | ||||
| // 得到所有字段 | // 得到所有字段 | ||||
| Field fileds[] = POIPublicUtil.getClassFields(pojoClass); | |||||
| Field[] fileds = POIPublicUtil.getClassFields(pojoClass); | |||||
| ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); | ||||
| String targetId = null; | String targetId = null; | ||||
| if (etarget != null) { | if (etarget != null) { | ||||
| @@ -94,7 +98,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||||
| addDataToSheet(params, pojoClass, dataSet, wb.getSheetAt(0), wb); | addDataToSheet(params, pojoClass, dataSet, wb.getSheetAt(0), wb); | ||||
| } | } | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(), e.fillInStackTrace()); | |||||
| return null; | return null; | ||||
| } | } | ||||
| return wb; | return wb; | ||||
| @@ -53,7 +53,7 @@ import org.slf4j.LoggerFactory; | |||||
| @SuppressWarnings({ "rawtypes", "unchecked", "hiding" }) | @SuppressWarnings({ "rawtypes", "unchecked", "hiding" }) | ||||
| public class ExcelImportServer { | public class ExcelImportServer { | ||||
| private final static Logger logger = LoggerFactory.getLogger(ExcelImportServer.class); | |||||
| private final static Logger LOGGER = LoggerFactory.getLogger(ExcelImportServer.class); | |||||
| private CellValueServer cellValueServer; | private CellValueServer cellValueServer; | ||||
| @@ -62,8 +62,8 @@ public class ExcelImportServer { | |||||
| private boolean verfiyFail = false; | private boolean verfiyFail = false; | ||||
| public ExcelImportServer() { | public ExcelImportServer() { | ||||
| cellValueServer = new CellValueServer(); | |||||
| verifyHandlerServer = new VerifyHandlerServer(); | |||||
| this.cellValueServer = new CellValueServer(); | |||||
| this.verifyHandlerServer = new VerifyHandlerServer(); | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -412,8 +412,8 @@ public class ExcelImportServer { | |||||
| */ | */ | ||||
| public ExcelImportResult importExcelByIs(InputStream inputstream, Class<?> pojoClass, | public ExcelImportResult importExcelByIs(InputStream inputstream, Class<?> pojoClass, | ||||
| ImportParams params) throws Exception { | ImportParams params) throws Exception { | ||||
| if (logger.isDebugEnabled()) { | |||||
| logger.debug("Excel import start ,class is {}", pojoClass); | |||||
| if (LOGGER.isDebugEnabled()) { | |||||
| LOGGER.debug("Excel import start ,class is {}", pojoClass); | |||||
| } | } | ||||
| List<T> result = new ArrayList<T>(); | List<T> result = new ArrayList<T>(); | ||||
| Workbook book = null; | Workbook book = null; | ||||
| @@ -429,8 +429,8 @@ public class ExcelImportServer { | |||||
| } | } | ||||
| Map<String, PictureData> pictures; | Map<String, PictureData> pictures; | ||||
| for (int i = 0; i < params.getSheetNum(); i++) { | for (int i = 0; i < params.getSheetNum(); i++) { | ||||
| if (logger.isDebugEnabled()) { | |||||
| logger.debug(" start to read excel by is ,startTime is {}", new Date().getTime()); | |||||
| if (LOGGER.isDebugEnabled()) { | |||||
| LOGGER.debug(" start to read excel by is ,startTime is {}", new Date().getTime()); | |||||
| } | } | ||||
| if (isXSSFWorkbook) { | if (isXSSFWorkbook) { | ||||
| pictures = POIPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(i), | pictures = POIPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(i), | ||||
| @@ -439,12 +439,12 @@ public class ExcelImportServer { | |||||
| pictures = POIPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(i), | pictures = POIPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(i), | ||||
| (HSSFWorkbook) book); | (HSSFWorkbook) book); | ||||
| } | } | ||||
| if (logger.isDebugEnabled()) { | |||||
| logger.debug(" end to read excel by is ,endTime is {}", new Date().getTime()); | |||||
| if (LOGGER.isDebugEnabled()) { | |||||
| LOGGER.debug(" end to read excel by is ,endTime is {}", new Date().getTime()); | |||||
| } | } | ||||
| result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures)); | result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures)); | ||||
| if (logger.isDebugEnabled()) { | |||||
| logger.debug(" end to read excel list by pos ,endTime is {}", new Date().getTime()); | |||||
| if (LOGGER.isDebugEnabled()) { | |||||
| LOGGER.debug(" end to read excel list by pos ,endTime is {}", new Date().getTime()); | |||||
| } | } | ||||
| } | } | ||||
| if (params.isNeedSave()) { | if (params.isNeedSave()) { | ||||
| @@ -1,5 +1,8 @@ | |||||
| package org.jeecgframework.poi.util; | package org.jeecgframework.poi.util; | ||||
| import java.awt.image.BufferedImage; | |||||
| import java.io.ByteArrayOutputStream; | |||||
| import java.io.File; | |||||
| import java.lang.reflect.Field; | import java.lang.reflect.Field; | ||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||
| @@ -8,6 +11,8 @@ import java.util.HashMap; | |||||
| import java.util.List; | import java.util.List; | ||||
| import java.util.Map; | import java.util.Map; | ||||
| import javax.imageio.ImageIO; | |||||
| import org.apache.poi.POIXMLDocumentPart; | import org.apache.poi.POIXMLDocumentPart; | ||||
| import org.apache.poi.hssf.usermodel.HSSFClientAnchor; | import org.apache.poi.hssf.usermodel.HSSFClientAnchor; | ||||
| import org.apache.poi.hssf.usermodel.HSSFPicture; | import org.apache.poi.hssf.usermodel.HSSFPicture; | ||||
| @@ -22,15 +27,22 @@ import org.apache.poi.xssf.usermodel.XSSFPicture; | |||||
| import org.apache.poi.xssf.usermodel.XSSFShape; | import org.apache.poi.xssf.usermodel.XSSFShape; | ||||
| import org.apache.poi.xssf.usermodel.XSSFSheet; | import org.apache.poi.xssf.usermodel.XSSFSheet; | ||||
| import org.apache.poi.xssf.usermodel.XSSFWorkbook; | import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||||
| import org.apache.poi.xwpf.usermodel.XWPFDocument; | |||||
| import org.jeecgframework.poi.excel.annotation.Excel; | import org.jeecgframework.poi.excel.annotation.Excel; | ||||
| import org.jeecgframework.poi.excel.annotation.ExcelCollection; | import org.jeecgframework.poi.excel.annotation.ExcelCollection; | ||||
| import org.jeecgframework.poi.excel.annotation.ExcelEntity; | import org.jeecgframework.poi.excel.annotation.ExcelEntity; | ||||
| import org.jeecgframework.poi.excel.annotation.ExcelIgnore; | import org.jeecgframework.poi.excel.annotation.ExcelIgnore; | ||||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | ||||
| import org.jeecgframework.poi.word.entity.WordImageEntity; | |||||
| import org.jeecgframework.poi.word.entity.params.ExcelListEntity; | |||||
| import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; | import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; | ||||
| public class POIPublicUtil { | public class POIPublicUtil { | ||||
| private POIPublicUtil() { | |||||
| } | |||||
| /** | /** | ||||
| * 彻底创建一个对象 | * 彻底创建一个对象 | ||||
| * | * | ||||
| @@ -58,7 +70,6 @@ public class POIPublicUtil { | |||||
| } | } | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| e.printStackTrace(); | |||||
| throw new RuntimeException("创建对象异常"); | throw new RuntimeException("创建对象异常"); | ||||
| } | } | ||||
| return obj; | return obj; | ||||
| @@ -286,4 +297,125 @@ public class POIPublicUtil { | |||||
| || exportName.indexOf(targetId) != -1; | || exportName.indexOf(targetId) != -1; | ||||
| } | } | ||||
| private static Integer getImageType(String type) { | |||||
| if (type.equalsIgnoreCase("JPG") || type.equalsIgnoreCase("JPEG")) { | |||||
| return XWPFDocument.PICTURE_TYPE_JPEG; | |||||
| } | |||||
| if (type.equalsIgnoreCase("GIF")) { | |||||
| return XWPFDocument.PICTURE_TYPE_GIF; | |||||
| } | |||||
| if (type.equalsIgnoreCase("BMP")) { | |||||
| return XWPFDocument.PICTURE_TYPE_GIF; | |||||
| } | |||||
| if (type.equalsIgnoreCase("PNG")) { | |||||
| return XWPFDocument.PICTURE_TYPE_PNG; | |||||
| } | |||||
| return XWPFDocument.PICTURE_TYPE_JPEG; | |||||
| } | |||||
| /** | |||||
| * 返回流和图片类型 | |||||
| *@Author JueYue | |||||
| *@date 2013-11-20 | |||||
| *@param obj | |||||
| *@return (byte[]) isAndType[0],(Integer)isAndType[1] | |||||
| * @throws Exception | |||||
| */ | |||||
| public static Object[] getIsAndType(WordImageEntity entity) throws Exception { | |||||
| Object[] result = new Object[2]; | |||||
| String type; | |||||
| if (entity.getType().equals(WordImageEntity.URL)) { | |||||
| ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); | |||||
| BufferedImage bufferImg; | |||||
| String path = Thread.currentThread().getContextClassLoader().getResource("").toURI() | |||||
| .getPath() | |||||
| + entity.getUrl(); | |||||
| path = path.replace("WEB-INF/classes/", ""); | |||||
| path = path.replace("file:/", ""); | |||||
| bufferImg = ImageIO.read(new File(path)); | |||||
| ImageIO.write( | |||||
| bufferImg, | |||||
| entity.getUrl().substring(entity.getUrl().indexOf(".") + 1, | |||||
| entity.getUrl().length()), byteArrayOut); | |||||
| result[0] = byteArrayOut.toByteArray(); | |||||
| type = entity.getUrl().split("/.")[entity.getUrl().split("/.").length - 1]; | |||||
| } else { | |||||
| result[0] = entity.getData(); | |||||
| type = POIPublicUtil.getFileExtendName(entity.getData()); | |||||
| } | |||||
| result[1] = getImageType(type); | |||||
| return result; | |||||
| } | |||||
| /** | |||||
| * 获取参数值 | |||||
| * | |||||
| * @param params | |||||
| * @param map | |||||
| * @return | |||||
| */ | |||||
| private static Object getParamsValue(String params, Map<String, Object> map) throws Exception { | |||||
| if (params.indexOf(".") != -1) { | |||||
| String[] paramsArr = params.split("\\."); | |||||
| return getValueDoWhile(map.get(paramsArr[0]), paramsArr, 1); | |||||
| } | |||||
| return map.containsKey(params) ? map.get(params) : ""; | |||||
| } | |||||
| /** | |||||
| * 解析数据 | |||||
| * | |||||
| * @Author JueYue | |||||
| * @date 2013-11-16 | |||||
| * @return | |||||
| */ | |||||
| public static Object getRealValue(String currentText, Map<String, Object> map) throws Exception { | |||||
| String params = ""; | |||||
| while (currentText.indexOf("{{") != -1) { | |||||
| params = currentText | |||||
| .substring(currentText.indexOf("{{") + 2, currentText.indexOf("}}")); | |||||
| Object obj = getParamsValue(params.trim(), map); | |||||
| //判断图片或者是集合 | |||||
| if (obj instanceof WordImageEntity || obj instanceof List | |||||
| || obj instanceof ExcelListEntity) { | |||||
| return obj; | |||||
| } else { | |||||
| currentText = currentText.replace("{{" + params + "}}", obj.toString()); | |||||
| } | |||||
| } | |||||
| return currentText; | |||||
| } | |||||
| /** | |||||
| * 通过遍历过去对象值 | |||||
| * | |||||
| * @param object | |||||
| * @param paramsArr | |||||
| * @param index | |||||
| * @return | |||||
| * @throws Exception | |||||
| * @throws InvocationTargetException | |||||
| * @throws IllegalAccessException | |||||
| * @throws IllegalArgumentException | |||||
| */ | |||||
| @SuppressWarnings("rawtypes") | |||||
| public static Object getValueDoWhile(Object object, String[] paramsArr, int index) | |||||
| throws Exception { | |||||
| if (object == null) { | |||||
| return ""; | |||||
| } | |||||
| if (object instanceof WordImageEntity) { | |||||
| return object; | |||||
| } | |||||
| if (object instanceof Map) { | |||||
| object = ((Map) object).get(paramsArr[index]); | |||||
| } else { | |||||
| object = POIPublicUtil.getMethod(paramsArr[index], object.getClass()).invoke(object, | |||||
| new Object[] {}); | |||||
| } | |||||
| return (index == paramsArr.length - 1) ? (object == null ? "" : object) : getValueDoWhile( | |||||
| object, paramsArr, ++index); | |||||
| } | |||||
| } | } | ||||
| @@ -13,6 +13,10 @@ import org.jeecgframework.poi.word.parse.ParseWord07; | |||||
| * @version 1.0 | * @version 1.0 | ||||
| */ | */ | ||||
| public class WordExportUtil { | public class WordExportUtil { | ||||
| private WordExportUtil(){ | |||||
| } | |||||
| /** | /** | ||||
| * 解析Word2007版本 | * 解析Word2007版本 | ||||
| @@ -1,162 +0,0 @@ | |||||
| package org.jeecgframework.poi.word.entity; | |||||
| import java.io.InputStream; | |||||
| import org.apache.poi.openxml4j.opc.OPCPackage; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFDocument; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFRun; | |||||
| import org.apache.xmlbeans.XmlException; | |||||
| import org.apache.xmlbeans.XmlToken; | |||||
| import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; | |||||
| import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; | |||||
| import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; | |||||
| /** | |||||
| * 扩充document,修复图片插入失败问题问题 | |||||
| * | |||||
| * @author JueYue | |||||
| * @date 2013-11-20 | |||||
| * @version 1.0 | |||||
| */ | |||||
| public class JeecgXWPFDocument extends XWPFDocument { | |||||
| public JeecgXWPFDocument() { | |||||
| super(); | |||||
| } | |||||
| public JeecgXWPFDocument(InputStream in) throws Exception { | |||||
| super(in); | |||||
| } | |||||
| public JeecgXWPFDocument(OPCPackage opcPackage) throws Exception { | |||||
| super(opcPackage); | |||||
| } | |||||
| public void createPicture(String blipId, int id, int width, int height) { | |||||
| final int EMU = 9525; | |||||
| width *= EMU; | |||||
| height *= EMU; | |||||
| //String blipId = getAllPictures().get(id).getPackageRelationship().getId(); | |||||
| CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline(); | |||||
| String picXml = "" | |||||
| + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" | |||||
| + " <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" | |||||
| + " <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" | |||||
| + " <pic:nvPicPr>" + " <pic:cNvPr id=\"" | |||||
| + id | |||||
| + "\" name=\"Generated\"/>" | |||||
| + " <pic:cNvPicPr/>" | |||||
| + " </pic:nvPicPr>" | |||||
| + " <pic:blipFill>" | |||||
| + " <a:blip r:embed=\"" | |||||
| + blipId | |||||
| + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" | |||||
| + " <a:stretch>" | |||||
| + " <a:fillRect/>" | |||||
| + " </a:stretch>" | |||||
| + " </pic:blipFill>" | |||||
| + " <pic:spPr>" | |||||
| + " <a:xfrm>" | |||||
| + " <a:off x=\"0\" y=\"0\"/>" | |||||
| + " <a:ext cx=\"" | |||||
| + width | |||||
| + "\" cy=\"" | |||||
| + height | |||||
| + "\"/>" | |||||
| + " </a:xfrm>" | |||||
| + " <a:prstGeom prst=\"rect\">" | |||||
| + " <a:avLst/>" | |||||
| + " </a:prstGeom>" | |||||
| + " </pic:spPr>" | |||||
| + " </pic:pic>" | |||||
| + " </a:graphicData>" + "</a:graphic>"; | |||||
| //CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData(); | |||||
| XmlToken xmlToken = null; | |||||
| try { | |||||
| xmlToken = XmlToken.Factory.parse(picXml); | |||||
| } catch (XmlException xe) { | |||||
| xe.printStackTrace(); | |||||
| } | |||||
| inline.set(xmlToken); | |||||
| //graphicData.set(xmlToken); | |||||
| inline.setDistT(0); | |||||
| inline.setDistB(0); | |||||
| inline.setDistL(0); | |||||
| inline.setDistR(0); | |||||
| CTPositiveSize2D extent = inline.addNewExtent(); | |||||
| extent.setCx(width); | |||||
| extent.setCy(height); | |||||
| CTNonVisualDrawingProps docPr = inline.addNewDocPr(); | |||||
| docPr.setId(id); | |||||
| docPr.setName("Picture " + id); | |||||
| docPr.setDescr("Generated"); | |||||
| } | |||||
| public void createPicture(XWPFRun run, String blipId, int id, int width, int height) { | |||||
| final int EMU = 9525; | |||||
| width *= EMU; | |||||
| height *= EMU; | |||||
| CTInline inline = run.getCTR().addNewDrawing().addNewInline(); | |||||
| String picXml = "" | |||||
| + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" | |||||
| + " <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" | |||||
| + " <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" | |||||
| + " <pic:nvPicPr>" + " <pic:cNvPr id=\"" | |||||
| + id | |||||
| + "\" name=\"Generated\"/>" | |||||
| + " <pic:cNvPicPr/>" | |||||
| + " </pic:nvPicPr>" | |||||
| + " <pic:blipFill>" | |||||
| + " <a:blip r:embed=\"" | |||||
| + blipId | |||||
| + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" | |||||
| + " <a:stretch>" | |||||
| + " <a:fillRect/>" | |||||
| + " </a:stretch>" | |||||
| + " </pic:blipFill>" | |||||
| + " <pic:spPr>" | |||||
| + " <a:xfrm>" | |||||
| + " <a:off x=\"0\" y=\"0\"/>" | |||||
| + " <a:ext cx=\"" | |||||
| + width | |||||
| + "\" cy=\"" | |||||
| + height | |||||
| + "\"/>" | |||||
| + " </a:xfrm>" | |||||
| + " <a:prstGeom prst=\"rect\">" | |||||
| + " <a:avLst/>" | |||||
| + " </a:prstGeom>" | |||||
| + " </pic:spPr>" | |||||
| + " </pic:pic>" | |||||
| + " </a:graphicData>" + "</a:graphic>"; | |||||
| XmlToken xmlToken = null; | |||||
| try { | |||||
| xmlToken = XmlToken.Factory.parse(picXml); | |||||
| } catch (XmlException xe) { | |||||
| xe.printStackTrace(); | |||||
| } | |||||
| inline.set(xmlToken); | |||||
| inline.setDistT(0); | |||||
| inline.setDistB(0); | |||||
| inline.setDistL(0); | |||||
| inline.setDistR(0); | |||||
| CTPositiveSize2D extent = inline.addNewExtent(); | |||||
| extent.setCx(width); | |||||
| extent.setCy(height); | |||||
| CTNonVisualDrawingProps docPr = inline.addNewDocPr(); | |||||
| docPr.setId(id); | |||||
| docPr.setName("Picture " + id); | |||||
| docPr.setDescr("Generated"); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,120 @@ | |||||
| package org.jeecgframework.poi.word.entity; | |||||
| import java.io.InputStream; | |||||
| import org.apache.poi.openxml4j.opc.OPCPackage; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFDocument; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFRun; | |||||
| import org.apache.xmlbeans.XmlException; | |||||
| import org.apache.xmlbeans.XmlToken; | |||||
| import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; | |||||
| import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; | |||||
| import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| /** | |||||
| * 扩充document,修复图片插入失败问题问题 | |||||
| * | |||||
| * @author JueYue | |||||
| * @date 2013-11-20 | |||||
| * @version 1.0 | |||||
| */ | |||||
| public class MyXWPFDocument extends XWPFDocument { | |||||
| private static final Logger LOGGER = LoggerFactory.getLogger(MyXWPFDocument.class); | |||||
| private static String PICXML = "" | |||||
| + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" | |||||
| + " <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" | |||||
| + " <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" | |||||
| + " <pic:nvPicPr>" | |||||
| + " <pic:cNvPr id=\"%s\" name=\"Generated\"/>" | |||||
| + " <pic:cNvPicPr/>" | |||||
| + " </pic:nvPicPr>" | |||||
| + " <pic:blipFill>" | |||||
| + " <a:blip r:embed=\"%s\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" | |||||
| + " <a:stretch>" | |||||
| + " <a:fillRect/>" | |||||
| + " </a:stretch>" + " </pic:blipFill>" | |||||
| + " <pic:spPr>" + " <a:xfrm>" | |||||
| + " <a:off x=\"0\" y=\"0\"/>" | |||||
| + " <a:ext cx=\"%s\" cy=\"%s\"/>" | |||||
| + " </a:xfrm>" | |||||
| + " <a:prstGeom prst=\"rect\">" | |||||
| + " <a:avLst/>" | |||||
| + " </a:prstGeom>" + " </pic:spPr>" | |||||
| + " </pic:pic>" + " </a:graphicData>" | |||||
| + "</a:graphic>"; | |||||
| public MyXWPFDocument() { | |||||
| super(); | |||||
| } | |||||
| public MyXWPFDocument(InputStream in) throws Exception { | |||||
| super(in); | |||||
| } | |||||
| public MyXWPFDocument(OPCPackage opcPackage) throws Exception { | |||||
| super(opcPackage); | |||||
| } | |||||
| public void createPicture(String blipId, int id, int width, int height) { | |||||
| final int EMU = 9525; | |||||
| width *= EMU; | |||||
| height *= EMU; | |||||
| CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline(); | |||||
| String picXml = String.format(PICXML, id, blipId, width, height); | |||||
| XmlToken xmlToken = null; | |||||
| try { | |||||
| xmlToken = XmlToken.Factory.parse(picXml); | |||||
| } catch (XmlException xe) { | |||||
| LOGGER.error(xe.getMessage(), xe.fillInStackTrace()); | |||||
| } | |||||
| inline.set(xmlToken); | |||||
| inline.setDistT(0); | |||||
| inline.setDistB(0); | |||||
| inline.setDistL(0); | |||||
| inline.setDistR(0); | |||||
| CTPositiveSize2D extent = inline.addNewExtent(); | |||||
| extent.setCx(width); | |||||
| extent.setCy(height); | |||||
| CTNonVisualDrawingProps docPr = inline.addNewDocPr(); | |||||
| docPr.setId(id); | |||||
| docPr.setName("Picture " + id); | |||||
| docPr.setDescr("Generated"); | |||||
| } | |||||
| public void createPicture(XWPFRun run, String blipId, int id, int width, int height) { | |||||
| final int EMU = 9525; | |||||
| width *= EMU; | |||||
| height *= EMU; | |||||
| CTInline inline = run.getCTR().addNewDrawing().addNewInline(); | |||||
| String picXml = String.format(PICXML, id, blipId, width, height); | |||||
| XmlToken xmlToken = null; | |||||
| try { | |||||
| xmlToken = XmlToken.Factory.parse(picXml); | |||||
| } catch (XmlException xe) { | |||||
| LOGGER.error(xe.getMessage(), xe.fillInStackTrace()); | |||||
| } | |||||
| inline.set(xmlToken); | |||||
| inline.setDistT(0); | |||||
| inline.setDistB(0); | |||||
| inline.setDistL(0); | |||||
| inline.setDistR(0); | |||||
| CTPositiveSize2D extent = inline.addNewExtent(); | |||||
| extent.setCx(width); | |||||
| extent.setCy(height); | |||||
| CTNonVisualDrawingProps docPr = inline.addNewDocPr(); | |||||
| docPr.setId(id); | |||||
| docPr.setName("Picture " + id); | |||||
| docPr.setDescr("Generated"); | |||||
| } | |||||
| } | |||||
| @@ -12,12 +12,14 @@ import org.apache.poi.xwpf.usermodel.XWPFTable; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFTableCell; | import org.apache.poi.xwpf.usermodel.XWPFTableCell; | ||||
| import org.apache.poi.xwpf.usermodel.XWPFTableRow; | 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.util.POIPublicUtil; | |||||
| import org.jeecgframework.poi.word.entity.MyXWPFDocument; | |||||
| 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.entity.params.ExcelListEntity; | ||||
| import org.jeecgframework.poi.word.parse.excel.ExcelEntityParse; | import org.jeecgframework.poi.word.parse.excel.ExcelEntityParse; | ||||
| import org.jeecgframework.poi.word.parse.excel.ExcelMapParse; | import org.jeecgframework.poi.word.parse.excel.ExcelMapParse; | ||||
| import org.jeecgframework.poi.word.util.ParseWordUtil; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| /** | /** | ||||
| * 解析07版的Word,替换文字,生成表格,生成图片 | * 解析07版的Word,替换文字,生成表格,生成图片 | ||||
| @@ -29,6 +31,8 @@ import org.jeecgframework.poi.word.util.ParseWordUtil; | |||||
| @SuppressWarnings({ "unchecked", "rawtypes" }) | @SuppressWarnings({ "unchecked", "rawtypes" }) | ||||
| public class ParseWord07 { | public class ParseWord07 { | ||||
| private static final Logger LOGGER = LoggerFactory.getLogger(ParseWord07.class); | |||||
| /** | /** | ||||
| * 添加图片 | * 添加图片 | ||||
| * | * | ||||
| @@ -39,18 +43,18 @@ public class ParseWord07 { | |||||
| * @throws Exception | * @throws Exception | ||||
| */ | */ | ||||
| private void addAnImage(WordImageEntity obj, XWPFRun currentRun) throws Exception { | private void addAnImage(WordImageEntity obj, XWPFRun currentRun) throws Exception { | ||||
| Object[] isAndType = ParseWordUtil.getIsAndType(obj); | |||||
| Object[] isAndType = POIPublicUtil.getIsAndType(obj); | |||||
| String picId; | String picId; | ||||
| try { | try { | ||||
| picId = currentRun.getParagraph().getDocument() | picId = currentRun.getParagraph().getDocument() | ||||
| .addPictureData((byte[]) isAndType[0], (Integer) isAndType[1]); | .addPictureData((byte[]) isAndType[0], (Integer) isAndType[1]); | ||||
| ((JeecgXWPFDocument) currentRun.getParagraph().getDocument()).createPicture(currentRun, | |||||
| ((MyXWPFDocument) currentRun.getParagraph().getDocument()).createPicture(currentRun, | |||||
| picId, | picId, | ||||
| currentRun.getParagraph().getDocument() | currentRun.getParagraph().getDocument() | ||||
| .getNextPicNameNumber((Integer) isAndType[1]), obj.getWidth(), obj.getHeight()); | .getNextPicNameNumber((Integer) isAndType[1]), obj.getWidth(), obj.getHeight()); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| e.printStackTrace(); | |||||
| LOGGER.error(e.getMessage(), e.fillInStackTrace()); | |||||
| } | } | ||||
| } | } | ||||
| @@ -64,7 +68,7 @@ public class ParseWord07 { | |||||
| */ | */ | ||||
| private void changeValues(XWPFParagraph paragraph, XWPFRun currentRun, String currentText, | private void changeValues(XWPFParagraph paragraph, XWPFRun currentRun, String currentText, | ||||
| List<Integer> runIndex, Map<String, Object> map) throws Exception { | List<Integer> runIndex, Map<String, Object> map) throws Exception { | ||||
| Object obj = ParseWordUtil.getRealValue(currentText, map); | |||||
| Object obj = POIPublicUtil.getRealValue(currentText, map); | |||||
| if (obj instanceof WordImageEntity) {// 如果是图片就设置为图片 | if (obj instanceof WordImageEntity) {// 如果是图片就设置为图片 | ||||
| currentRun.setText("", 0); | currentRun.setText("", 0); | ||||
| addAnImage((WordImageEntity) obj, currentRun); | addAnImage((WordImageEntity) obj, currentRun); | ||||
| @@ -91,9 +95,7 @@ public class ParseWord07 { | |||||
| String text = cell.getText().trim(); | String text = cell.getText().trim(); | ||||
| // 判断是不是迭代输出 | // 判断是不是迭代输出 | ||||
| if (text.startsWith("{{") && text.endsWith("}}") && text.indexOf("in ") != -1) { | 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 POIPublicUtil.getRealValue(text.replace("in ", "").trim(), map); | |||||
| } | } | ||||
| return null; | return null; | ||||
| } | } | ||||
| @@ -217,7 +219,7 @@ public class ParseWord07 { | |||||
| * @throws Exception | * @throws Exception | ||||
| */ | */ | ||||
| public XWPFDocument parseWord(String url, Map<String, Object> map) throws Exception { | public XWPFDocument parseWord(String url, Map<String, Object> map) throws Exception { | ||||
| JeecgXWPFDocument doc = WordCache.getXWPFDocumen(url); | |||||
| MyXWPFDocument doc = WordCache.getXWPFDocumen(url); | |||||
| parseWordSetValue(doc, map); | parseWordSetValue(doc, map); | ||||
| return doc; | return doc; | ||||
| } | } | ||||
| @@ -231,10 +233,10 @@ public class ParseWord07 { | |||||
| * @throws Exception | * @throws Exception | ||||
| */ | */ | ||||
| public void parseWord(XWPFDocument document, Map<String, Object> map) throws Exception { | public void parseWord(XWPFDocument document, Map<String, Object> map) throws Exception { | ||||
| parseWordSetValue((JeecgXWPFDocument) document, map); | |||||
| parseWordSetValue((MyXWPFDocument) document, map); | |||||
| } | } | ||||
| private void parseWordSetValue(JeecgXWPFDocument doc, Map<String, Object> map) throws Exception { | |||||
| private void parseWordSetValue(MyXWPFDocument doc, Map<String, Object> map) throws Exception { | |||||
| // 第一步解析文档 | // 第一步解析文档 | ||||
| parseAllParagraphic(doc.getParagraphs(), map); | parseAllParagraphic(doc.getParagraphs(), map); | ||||
| // 第二步解析所有表格 | // 第二步解析所有表格 | ||||
| @@ -5,7 +5,7 @@ import java.util.List; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFTable; | import org.apache.poi.xwpf.usermodel.XWPFTable; | ||||
| import org.apache.poi.xwpf.usermodel.XWPFTableCell; | import org.apache.poi.xwpf.usermodel.XWPFTableCell; | ||||
| import org.apache.poi.xwpf.usermodel.XWPFTableRow; | import org.apache.poi.xwpf.usermodel.XWPFTableRow; | ||||
| import org.jeecgframework.poi.word.util.ParseWordUtil; | |||||
| import org.jeecgframework.poi.util.POIPublicUtil; | |||||
| /** | /** | ||||
| * 处理和生成Map 类型的数据变成表格 | * 处理和生成Map 类型的数据变成表格 | ||||
| @@ -54,12 +54,12 @@ public final class ExcelMapParse { | |||||
| .getTableCells() | .getTableCells() | ||||
| .get(cellIndex) | .get(cellIndex) | ||||
| .setText( | .setText( | ||||
| ParseWordUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0) | |||||
| POIPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0) | |||||
| .toString()); | .toString()); | ||||
| } | } | ||||
| for (; cellIndex < params.length; cellIndex++) { | for (; cellIndex < params.length; cellIndex++) { | ||||
| currentRow.createCell().setText( | currentRow.createCell().setText( | ||||
| ParseWordUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0) | |||||
| POIPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0) | |||||
| .toString()); | .toString()); | ||||
| } | } | ||||
| } | } | ||||
| @@ -1,146 +0,0 @@ | |||||
| package org.jeecgframework.poi.word.util; | |||||
| import java.awt.image.BufferedImage; | |||||
| import java.io.ByteArrayOutputStream; | |||||
| import java.io.File; | |||||
| import java.lang.reflect.InvocationTargetException; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| import javax.imageio.ImageIO; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFDocument; | |||||
| import org.jeecgframework.poi.util.POIPublicUtil; | |||||
| import org.jeecgframework.poi.word.entity.WordImageEntity; | |||||
| import org.jeecgframework.poi.word.entity.params.ExcelListEntity; | |||||
| /** | |||||
| * 解析公告类 | |||||
| * Excel 实体注解,暂时不支持图片 | |||||
| * @author JueYue | |||||
| * @date 2014-7-24 | |||||
| * @version 1.1 | |||||
| */ | |||||
| public class ParseWordUtil { | |||||
| private static Integer getImageType(String type) { | |||||
| if (type.equalsIgnoreCase("JPG") || type.equalsIgnoreCase("JPEG")) { | |||||
| return XWPFDocument.PICTURE_TYPE_JPEG; | |||||
| } | |||||
| if (type.equalsIgnoreCase("GIF")) { | |||||
| return XWPFDocument.PICTURE_TYPE_GIF; | |||||
| } | |||||
| if (type.equalsIgnoreCase("BMP")) { | |||||
| return XWPFDocument.PICTURE_TYPE_GIF; | |||||
| } | |||||
| if (type.equalsIgnoreCase("PNG")) { | |||||
| return XWPFDocument.PICTURE_TYPE_PNG; | |||||
| } | |||||
| return XWPFDocument.PICTURE_TYPE_JPEG; | |||||
| } | |||||
| /** | |||||
| * 返回流和图片类型 | |||||
| *@Author JueYue | |||||
| *@date 2013-11-20 | |||||
| *@param obj | |||||
| *@return (byte[]) isAndType[0],(Integer)isAndType[1] | |||||
| * @throws Exception | |||||
| */ | |||||
| public static Object[] getIsAndType(WordImageEntity entity) throws Exception { | |||||
| Object[] result = new Object[2]; | |||||
| String type; | |||||
| if (entity.getType().equals(WordImageEntity.URL)) { | |||||
| ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); | |||||
| BufferedImage bufferImg; | |||||
| String path = Thread.currentThread().getContextClassLoader().getResource("").toURI() | |||||
| .getPath() | |||||
| + entity.getUrl(); | |||||
| path = path.replace("WEB-INF/classes/", ""); | |||||
| path = path.replace("file:/", ""); | |||||
| bufferImg = ImageIO.read(new File(path)); | |||||
| ImageIO.write( | |||||
| bufferImg, | |||||
| entity.getUrl().substring(entity.getUrl().indexOf(".") + 1, | |||||
| entity.getUrl().length()), byteArrayOut); | |||||
| result[0] = byteArrayOut.toByteArray(); | |||||
| type = entity.getUrl().split("/.")[entity.getUrl().split("/.").length - 1]; | |||||
| } else { | |||||
| result[0] = entity.getData(); | |||||
| type = POIPublicUtil.getFileExtendName(entity.getData()); | |||||
| } | |||||
| result[1] = getImageType(type); | |||||
| return result; | |||||
| } | |||||
| /** | |||||
| * 获取参数值 | |||||
| * | |||||
| * @param params | |||||
| * @param map | |||||
| * @return | |||||
| */ | |||||
| private static Object getParamsValue(String params, Map<String, Object> map) throws Exception { | |||||
| if (params.indexOf(".") != -1) { | |||||
| String[] paramsArr = params.split("\\."); | |||||
| return getValueDoWhile(map.get(paramsArr[0]), paramsArr, 1); | |||||
| } | |||||
| return map.containsKey(params) ? map.get(params) : ""; | |||||
| } | |||||
| /** | |||||
| * 解析数据 | |||||
| * | |||||
| * @Author JueYue | |||||
| * @date 2013-11-16 | |||||
| * @return | |||||
| */ | |||||
| public static Object getRealValue(String currentText, Map<String, Object> map) throws Exception { | |||||
| String params = ""; | |||||
| while (currentText.indexOf("{{") != -1) { | |||||
| params = currentText | |||||
| .substring(currentText.indexOf("{{") + 2, currentText.indexOf("}}")); | |||||
| Object obj = getParamsValue(params.trim(), map); | |||||
| //判断图片或者是集合 | |||||
| if (obj instanceof WordImageEntity || obj instanceof List | |||||
| || obj instanceof ExcelListEntity) { | |||||
| return obj; | |||||
| } else { | |||||
| currentText = currentText.replace("{{" + params + "}}", obj.toString()); | |||||
| } | |||||
| } | |||||
| return currentText; | |||||
| } | |||||
| /** | |||||
| * 通过遍历过去对象值 | |||||
| * | |||||
| * @param object | |||||
| * @param paramsArr | |||||
| * @param index | |||||
| * @return | |||||
| * @throws Exception | |||||
| * @throws InvocationTargetException | |||||
| * @throws IllegalAccessException | |||||
| * @throws IllegalArgumentException | |||||
| */ | |||||
| @SuppressWarnings("rawtypes") | |||||
| public static Object getValueDoWhile(Object object, String[] paramsArr, int index) | |||||
| throws Exception { | |||||
| if (object == null) { | |||||
| return ""; | |||||
| } | |||||
| if (object instanceof WordImageEntity) { | |||||
| return object; | |||||
| } | |||||
| if (object instanceof Map) { | |||||
| object = ((Map) object).get(paramsArr[index]); | |||||
| } else { | |||||
| object = POIPublicUtil.getMethod(paramsArr[index], object.getClass()).invoke(object, | |||||
| new Object[] {}); | |||||
| } | |||||
| return (index == paramsArr.length - 1) ? (object == null ? "" : object) : getValueDoWhile( | |||||
| object, paramsArr, ++index); | |||||
| } | |||||
| } | |||||
| @@ -33,7 +33,7 @@ public class WordExportUtilAnnExcelTest { | |||||
| /** | /** | ||||
| * 简单导出没有图片和Excel | * 简单导出没有图片和Excel | ||||
| */ | */ | ||||
| //@Test | |||||
| @Test | |||||
| public void SimpleWordExport() { | public void SimpleWordExport() { | ||||
| Map<String, Object> map = new HashMap<String, Object>(); | Map<String, Object> map = new HashMap<String, Object>(); | ||||
| map.put("department", "Jeecg"); | map.put("department", "Jeecg"); | ||||
| @@ -20,7 +20,7 @@ public class WordExportUtilBaseExcelTest { | |||||
| /** | /** | ||||
| * 简单导出包含图片 | * 简单导出包含图片 | ||||
| */ | */ | ||||
| //@Test | |||||
| @Test | |||||
| public void imageWordExport() { | public void imageWordExport() { | ||||
| Map<String, Object> map = new HashMap<String, Object>(); | Map<String, Object> map = new HashMap<String, Object>(); | ||||
| map.put("department", "Jeecg"); | map.put("department", "Jeecg"); | ||||