PageRenderTime 179ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/jeecg-framework/src/main/java/org/jeecgframework/poi/excel/ExcelPublicUtil.java

https://github.com/nic9805/jeecg
Java | 277 lines | 181 code | 16 blank | 80 comment | 62 complexity | 87019a72d8e02af505e4e1e5b6ac2ed2 MD5 | raw file
Possible License(s): GPL-3.0, MPL-2.0-no-copyleft-exception, MIT
  1. package org.jeecgframework.poi.excel;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import org.apache.poi.POIXMLDocumentPart;
  10. import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
  11. import org.apache.poi.hssf.usermodel.HSSFPicture;
  12. import org.apache.poi.hssf.usermodel.HSSFPictureData;
  13. import org.apache.poi.hssf.usermodel.HSSFShape;
  14. import org.apache.poi.hssf.usermodel.HSSFSheet;
  15. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  16. import org.apache.poi.ss.usermodel.PictureData;
  17. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  18. import org.apache.poi.xssf.usermodel.XSSFDrawing;
  19. import org.apache.poi.xssf.usermodel.XSSFPicture;
  20. import org.apache.poi.xssf.usermodel.XSSFShape;
  21. import org.apache.poi.xssf.usermodel.XSSFSheet;
  22. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  23. import org.hibernate.mapping.Collection;
  24. import org.jeecgframework.poi.excel.annotation.Excel;
  25. import org.jeecgframework.poi.excel.annotation.ExcelCollection;
  26. import org.jeecgframework.poi.excel.annotation.ExcelEntity;
  27. import org.jeecgframework.poi.excel.annotation.ExcelIgnore;
  28. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
  29. public class ExcelPublicUtil {
  30. public static String GET = "get";
  31. public static String SET = "set";
  32. /**
  33. * 获取class的 包括父类的
  34. * @param type
  35. * @return
  36. */
  37. public static Field[] getClassFields(Class<?> clazz) {
  38. List<Field> list = new ArrayList<Field>();
  39. Field[] fields;
  40. do{
  41. fields = clazz.getDeclaredFields();
  42. for(int i = 0;i<fields.length;i++){
  43. list.add(fields[i]);
  44. }
  45. clazz = clazz.getSuperclass();
  46. }while(clazz!= Object.class&&clazz!=null);
  47. return list.toArray(fields);
  48. }
  49. /**
  50. * 判断是不是集合的实现类
  51. * @param clazz
  52. * @return
  53. */
  54. public static boolean isCollection(Class<?> clazz) {
  55. return clazz.isAssignableFrom(List.class)||
  56. clazz.isAssignableFrom(Set.class)||
  57. clazz.isAssignableFrom(Collection.class);
  58. // String colleciton = "java.util.Collection";
  59. // Class<?>[] faces = clazz.getInterfaces();
  60. // for (Class<?> face : faces) {
  61. // if(face.getName().equals(colleciton)){
  62. // return true;
  63. // }else{
  64. // if(face.getSuperclass()!= Object.class&&face.getSuperclass()!=null){
  65. // return isCollection(face.getSuperclass());
  66. // }
  67. // }
  68. // }
  69. // if(clazz.getSuperclass()!= Object.class&&clazz.getSuperclass()!=null){
  70. // return isCollection(clazz.getSuperclass());
  71. // }
  72. // return false;
  73. }
  74. /**
  75. * 判断是否不要在这个excel操作中
  76. * @param field
  77. * @param targetId
  78. * @return
  79. */
  80. public static boolean isNotUserExcelUserThis(Field field, String targetId) {
  81. boolean boo = true;
  82. if(field.getAnnotation(ExcelIgnore.class)!=null){
  83. boo = true;
  84. }else if(boo&&field.getAnnotation(ExcelCollection.class)!=null
  85. &&isUseInThis(field.getAnnotation(ExcelCollection.class).exportName(),targetId)){
  86. boo = false;
  87. }else if(boo&&field.getAnnotation(Excel.class)!=null
  88. &&isUseInThis(field.getAnnotation(Excel.class).exportName(),targetId)){
  89. boo = false;
  90. }else if(boo&&field.getAnnotation(ExcelEntity.class)!=null
  91. &&isUseInThis(field.getAnnotation(ExcelEntity.class).exportName(),targetId)){
  92. boo = false;
  93. }
  94. return boo;
  95. }
  96. /**
  97. * 判断是不是使用
  98. * @param exportName
  99. * @param targetId
  100. * @return
  101. */
  102. private static boolean isUseInThis(String exportName, String targetId) {
  103. return targetId == null || exportName.equals("")
  104. || exportName.indexOf("_") < 0
  105. || exportName.indexOf(targetId) != -1;
  106. }
  107. /**
  108. * 是不是java基础类
  109. * @param field
  110. * @return
  111. */
  112. public static boolean isJavaClass(Field field) {
  113. Class<?> fieldType = field.getType();
  114. boolean isBaseClass = false;
  115. if(fieldType.isArray()){
  116. isBaseClass = false;
  117. }else if (fieldType.isPrimitive()||fieldType.getPackage()==null
  118. || fieldType.getPackage().getName().equals("java.lang")
  119. || fieldType.getPackage().getName().equals("java.math")
  120. || fieldType.getPackage().getName().equals("java.util")) {
  121. isBaseClass = true;
  122. }
  123. return isBaseClass;
  124. }
  125. /**
  126. * 彻底创建一个对象
  127. * @param clazz
  128. * @return
  129. */
  130. public static Object createObject(Class<?> clazz,String targetId) {
  131. Object obj = null;
  132. String fieldname;
  133. Method setMethod;
  134. try {
  135. obj = clazz.newInstance();
  136. Field[] fields = getClassFields(clazz);
  137. for(Field field:fields){
  138. if(isNotUserExcelUserThis(field, targetId)){continue;}
  139. if(isCollection(field.getType())){
  140. ExcelCollection collection = field
  141. .getAnnotation(ExcelCollection.class);
  142. fieldname = field.getName();
  143. setMethod = getMethod(fieldname,clazz,field.getType());
  144. setMethod.invoke(obj,ExcelPublicUtil.class.getClassLoader().loadClass(collection.type()).newInstance());
  145. }else if(!isJavaClass(field)){
  146. fieldname = field.getName();
  147. setMethod = getMethod(fieldname,clazz,field.getType() );
  148. setMethod.invoke(obj, createObject(field.getType(),targetId));
  149. }
  150. }
  151. } catch (Exception e) {
  152. e.printStackTrace();
  153. }
  154. return obj;
  155. }
  156. /**
  157. * 获取方法
  158. * @param name
  159. * @param pojoClass
  160. * @return
  161. * @throws Exception
  162. */
  163. public static Method getMethod(String name, Class<?> pojoClass)
  164. throws Exception {
  165. StringBuffer getMethodName = new StringBuffer(GET);
  166. getMethodName.append(name.substring(0, 1).toUpperCase());
  167. getMethodName.append(name.substring(1));
  168. return pojoClass.getMethod(getMethodName.toString(),new Class[]{});
  169. }
  170. /**
  171. * 获取方法
  172. * @param name
  173. * @param pojoClass
  174. * @param type
  175. * @return
  176. * @throws Exception
  177. */
  178. public static Method getMethod(String name, Class<?> pojoClass,Class<?> type)
  179. throws Exception {
  180. StringBuffer getMethodName = new StringBuffer(SET);
  181. getMethodName.append(name.substring(0, 1).toUpperCase());
  182. getMethodName.append(name.substring(1));
  183. return pojoClass.getMethod(getMethodName.toString(),new Class[]{type});
  184. }
  185. /**
  186. *
  187. * @param photoByte
  188. * @return
  189. */
  190. public static String getFileExtendName(byte[] photoByte) {
  191. String strFileExtendName = "JPG";
  192. if ((photoByte[0] == 71) && (photoByte[1] == 73)
  193. && (photoByte[2] == 70) && (photoByte[3] == 56)
  194. && ((photoByte[4] == 55) || (photoByte[4] == 57))
  195. && (photoByte[5] == 97)) {
  196. strFileExtendName = "GIF";
  197. } else if ((photoByte[6] == 74) && (photoByte[7] == 70)
  198. && (photoByte[8] == 73) && (photoByte[9] == 70)) {
  199. strFileExtendName = "JPG";
  200. } else if ((photoByte[0] == 66) && (photoByte[1] == 77)) {
  201. strFileExtendName = "BMP";
  202. } else if ((photoByte[1] == 80) && (photoByte[2] == 78)
  203. && (photoByte[3] == 71)) {
  204. strFileExtendName = "PNG";
  205. }
  206. return strFileExtendName;
  207. }
  208. /**
  209. * 获取Excel2003图片
  210. * @param sheet 当前sheet对象
  211. * @param workbook 工作簿对象
  212. * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
  213. */
  214. @SuppressWarnings("unchecked")
  215. public static Map<String, PictureData> getSheetPictrues03(HSSFSheet sheet,
  216. HSSFWorkbook workbook) {
  217. Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
  218. List<HSSFPictureData> pictures = workbook.getAllPictures();
  219. if (pictures.size() != 0) {
  220. for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
  221. HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
  222. if (shape instanceof HSSFPicture) {
  223. HSSFPicture pic = (HSSFPicture) shape;
  224. int pictureIndex = pic.getPictureIndex() - 1;
  225. HSSFPictureData picData = pictures.get(pictureIndex);
  226. String picIndex = String.valueOf(anchor.getRow1()) + "_"
  227. + String.valueOf(anchor.getCol1());
  228. sheetIndexPicMap.put(picIndex, picData);
  229. }
  230. }
  231. return sheetIndexPicMap;
  232. } else {
  233. return (Map<String, PictureData>) sheetIndexPicMap.put(null, null);
  234. }
  235. }
  236. /**
  237. * 获取Excel2007图片
  238. * @param sheetNum 当前sheet编号
  239. * @param sheet 当前sheet对象
  240. * @param workbook 工作簿对象
  241. * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
  242. */
  243. public static Map<String, PictureData> getSheetPictrues07(
  244. XSSFSheet sheet, XSSFWorkbook workbook) {
  245. Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
  246. for (POIXMLDocumentPart dr : sheet.getRelations()) {
  247. if (dr instanceof XSSFDrawing) {
  248. XSSFDrawing drawing = (XSSFDrawing) dr;
  249. List<XSSFShape> shapes = drawing.getShapes();
  250. for (XSSFShape shape : shapes) {
  251. XSSFPicture pic = (XSSFPicture) shape;
  252. XSSFClientAnchor anchor = pic.getPreferredSize();
  253. CTMarker ctMarker = anchor.getFrom();
  254. String picIndex = ctMarker.getRow() + "_" + ctMarker.getCol();
  255. sheetIndexPicMap.put(picIndex, pic.getPictureData());
  256. }
  257. }
  258. }
  259. return sheetIndexPicMap;
  260. }
  261. }