mpxj-0.0.25, 可以解析.mpp(ms project)文件
自己写的示例 仅供参考: public static void main(String[] args) { File file; file = new File("e:\\客户管理项目计划V1.0.mpp"); LinkedHashMap infoMap = new LinkedHashMap(); Map userSbMap = new HashMap(); List resourceList = new ArrayList(); String a = readMpp(file, infoMap, userSbMap, resourceList); } public static String readMpp(File file, LinkedHashMap infoMap, Map userSbMap, List resourceList) { MPPFile mpp; String result = ""; try { mpp = new MPPFile(file); LinkedList allList = mpp.getAllTasks(); allList.remove(0); for (int i = 0; i < allList.size(); i++) { Task t = (Task) allList.get(i); Map taskMap = new HashMap(); String flag = t.getUniqueID().toString(); taskMap.put("唯一标识号", t.getUniqueID().toString()); taskMap.put("标识号", t.getID().toString()); taskMap.put("任务名称", t.getName()); taskMap.put("大纲级别", t.getOutlineLevel().toString()); //处理开始时间 Date planStartDate = t.getStart(); String planStart = "-1"; if (planStartDate != null) { planStart = new Long(planStartDate.getTime()).toString(); } //处理完成时间 Date planStopDate = t.getFinish(); String planFinish = "-1"; if (planStopDate != null) { planFinish = new Long(planStopDate.getTime()).toString(); } //处理实际开始时间 Date actStartDate = t.getActualStart(); String actStart = "-1"; if (actStartDate != null) { actStart = new Long(actStartDate.getTime()).toString(); } //处理实际结束日期 Date actStopDate = t.getActualFinish(); String actFinish = "-1"; if (actStopDate != null) { actFinish = new Long(actStopDate.getTime()).toString(); } //处理里程碑 Boolean markedB = t.getMilestone(); String y_or_n = "否"; if (markedB != null) { boolean marked = markedB.booleanValue(); if (marked) { y_or_n = "是"; } } //处理资源 List userList = new ArrayList(); LinkedList resources = t.getResourceAssignments(); if (resources.size() > 0) { for (int j = 0; j < resources.size(); j++) { ResourceAssignment resourceA = (ResourceAssignment)resources.get(j); Resource resource = resourceA.getResource(); String resourceName = resource.getName(); String resourceUnit = String.valueOf(resourceA.getUnitsValue()); Map userMap = new HashMap(); userMap.put("quota", resourceUnit); userMap.put("userName", resourceName); if (userSbMap.get(resourceName) == null) { StringBuffer sb = new StringBuffer(); sb.append(""); userSbMap.put(resourceName, sb); userMap.put("login", sb); } else { userMap.put("login", userSbMap.get(resourceName)); } userList.add(userMap); if (!resourceList.contains(resourceName)) { resourceList.add(resourceName); } } } //处理前置任务 List pred = t.getPredecessors(); if (pred != null && pred.size() > 0) { List pred_tasksList = new ArrayList(); for (int x = 0; x < pred.size(); x++) { Relation r = (Relation) pred.get(x); String rId = String.valueOf(r.getTaskUniqueIDValue()); pred_tasksList.add(rId); } taskMap.put("前置任务", pred_tasksList); } else { taskMap.put("前置任务", new ArrayList()); } taskMap.put("资源", userList); taskMap.put("开始时间", planStart); taskMap.put("完成时间", planFinish); taskMap.put("工期", t.getDuration().toString().replaceAll("d", "")); taskMap.put("里程碑", y_or_n); taskMap.put("完成百分比", t.getPercentageComplete().toString()); taskMap.put("实际开始时间", actStart); taskMap.put("实际完成时间", actFinish); taskMap.put("任务描述", t.getText1()); infoMap.put(flag, taskMap); } } catch (MPXException e) { result = "您选择的文件格式有误或文件不是Project2003格式,请检查。"; e.printStackTrace(); } return result; } String readMpp(File file, LinkedHashMap infoMap, Map userSbMap, List resourceList) { MPPFile mpp; String result = ""; try { mpp = new MPPFile(file); LinkedList allList = mpp.getAllTasks(); allList.remove(0); for (int i = 0; i < allList.size(); i++) { Task t = (Task) allList.get(i); Map taskMap = new HashMap(); String flag = t.getUniqueID().toString(); taskMap.put("唯一标识号", t.getUniqueID().toString()); taskMap.put("标识号", t.getID().toString()); taskMap.put("任务名称", t.getName()); taskMap.put("大纲级别", t.getOutlineLevel().toString()); //处理开始时间 Date planStartDate = t.getStart(); String planStart = "-1"; if (planStartDate != null) { planStart = new Long(planStartDate.getTime()).toString(); } //处理完成时间 Date planStopDate = t.getFinish(); String planFinish = "-1"; if (planStopDate != null) { planFinish = new Long(planStopDate.getTime()).toString(); } //处理实际开始时间 Date actStartDate = t.getActualStart(); String actStart = "-1"; if (actStartDate != null) { actStart = new Long(actStartDate.getTime()).toString(); } //处理实际结束日期 Date actStopDate = t.getActualFinish(); String actFinish = "-1"; if (actStopDate != null) { actFinish = new Long(actStopDate.getTime()).toString(); } //处理里程碑 Boolean markedB = t.getMilestone(); String y_or_n = "否"; if (markedB != null) { boolean marked = markedB.booleanValue(); if (marked) { y_or_n = "是"; } } //处理资源 List userList = new ArrayList(); LinkedList resources = t.getResourceAssignments(); if (resources.size() > 0) { for (int j = 0; j < resources.size(); j++) { ResourceAssignment resourceA = (ResourceAssignment)resources.get(j); Resource resource = resourceA.getResource(); String resourceName = resource.getName(); String resourceUnit = String.valueOf(resourceA.getUnitsValue()); Map userMap = new HashMap(); userMap.put("quota", resourceUnit); userMap.put("userName", resourceName); if (userSbMap.get(resourceName) == null) { StringBuffer sb = new StringBuffer(); sb.append(""); userSbMap.put(resourceName, sb); userMap.put("login", sb); } else { userMap.put("login", userSbMap.get(resourceName)); } userList.add(userMap); if (!resourceList.contains(resourceName)) { resourceList.add(resourceName); } } } //处理前置任务 List pred = t.getPredecessors(); if (pred != null && pred.size() > 0) { List pred_tasksList = new ArrayList(); for (int x = 0; x < pred.size(); x++) { Relation r = (Relation) pred.get(x); String rId = String.valueOf(r.getTaskUniqueIDValue()); pred_tasksList.add(rId); } taskMap.put("前置任务", pred_tasksList); } else { taskMap.put("前置任务", new ArrayList()); } taskMap.put("资源", userList); taskMap.put("开始时间", planStart); taskMap.put("完成时间", planFinish); taskMap.put("工期", t.getDuration().toString().replaceAll("d", "")); taskMap.put("里程碑", y_or_n); taskMap.put("完成百分比", t.getPercentageComplete().toString()); taskMap.put("实际开始时间", actStart); taskMap.put("实际完成时间", actFinish); taskMap.put("任务描述", t.getText1()); infoMap.put(flag, taskMap); } } catch (MPXException e) { result = "您选择的文件格式有误或文件不是Project2003格式,请检查。"; e.printStackTrace(); } return result; }
用户评论
资源不错,能用
测试了可以试用,非常好
参考一下。还不错。。