plus project的mpp文件导出问题和plus project的xml导出

一、前言,为了开发的软件高大尚一点,我们公司用了甘特图,使用project软件做甘特图,然后导出到项目中,最后想再导出来,然后我就去研究了这些,网上资料太少,我就自己整理了一下。其中用到的中间件有http://www.plusgantt.com/download/ ,有这个中间件。但是也有限制

二、限制:第一,mpp格式不是公开的,所以不能导出mmp格式;可以导入mpx,但是需要很低的版本才行,除去基本列,其他列会隐藏。第二,可以导出xml,但是导出的xml,需要作为新项目导入到软件中,这样会导致,隐藏列不展示,需要你添加列,才会展示出来这列数据。

三、如果不是导出xml,就不用了下面看了,功能中使用了甘特图插件。亲测project 2010和project2020都可打开

(1)业务代码

public void toExportWBSProject(HttpServletResponse response, BaseRequest breq) throws Exception {
		
		String filename = "project模板.xml";
                //对象中获取参数,使用自己的对应
		String orgid = breq.getParam("orgid");
                //根据条件查询数据,使用自己的查询方法
		List<Map<String, Object>> list = getDAO().getListWbS(orgid);
                //数据结果集,因为我返回的是CaseInsensitiveMap对象,需要转成HashMap
		List<Map<String, Object>> list1=new ArrayList<Map<String, Object>>();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			Map<String, Object> map2=new HashMap<String,Object>();
			map2.put("UID", map.get("ID"));
			map2.put("Name", map.get("NAME"));
			map2.put("ID", map.get("ID"));
			map2.put("PID", map.get("PID"));
			map2.put("UNIT", map.get("UNIT"));
			map2.put("CODE", map.get("CODE"));
			list1.add(map2);
		}
                //调用自己的util,数据转换成层级结构的数据
		List list2 = TreeUtil.ToTree(list1, "children", "UID", "PID");
                //调用plusproject插件的方法,会添加一些其他值信息
		List tasks=Tree.ToTable(list2, "-1", "children", "UID", "PID");
                
		Map<String, Object> hs = new HashMap<String, Object>();
		hs.put("Tasks", tasks);
		hs.put("Name", "总进度计划");
                //调用自己写的方法,创建自定义列,可自定义自己的。util方法,看下面
		List<Map<String, Object>> extendedAttributes=ProjetExportUtil.getExtendedAttributes();
		hs.put("ExtendedAttributes", extendedAttributes);
		for (Object task_o : tasks) {
			@SuppressWarnings("unchecked")
			Map<String,Object> task = (Map<String,Object>) task_o;
                        //调用util的方法,放入自定义列的值,可调整
			task=ProjetExportUtil.getExtendedAttribute(task);
		}
                //调用插件的写入方法
		//String xmlStr = Export.ToXML(hs);
                //调用本人重写并优化了一点的写入数据的方法
		StringBuilder xmlStr = ProjetExportUtil.ToXML(hs);
                
                //设置响应下载的编码,请求头等
		String headfilename = new String(filename.getBytes("UTF-8"), "iso-8859-1");

		response.addHeader("Content-Disposition",
				"attachment;filename=" + headfilename);
		BufferedOutputStream bouts = null;
		try {
			OutputStream output = response.getOutputStream();
			bouts = new BufferedOutputStream(output);
			xmlStr.getBytes().length);
			int y=0;
                        //一次性写入太多会出错;所以需要截取循环写入xml;
			for (int i = 0; i < xmlStr.length(); i = y) {
				String str = "";
                                //防止写入数据换行,导致xml导入不了,需要写入的时候已节点写入
				int y2=xmlStr.indexOf(">", i + 200);
				if (y2<0||y2>= xmlStr.length()) {
					str = xmlStr.substring(i, xmlStr.length());
				} else {
					try {
						str = xmlStr.substring(i, y2);
					} catch (Exception e) {
						// TODO: handle exception
						//实际长度不够
						str= xmlStr.substring(i);
					}
					
				}
				if(y2<0) {
					y=xmlStr.length();
				}else {
					y=y2;
				}
                                //写部分数据
				bouts.write(str.getBytes(), 0, str.getBytes().length);
			}
			bouts.flush();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new WafException(e);
		} finally {
                        //写入异常或者写入完毕后,关闭流
			if (bouts != null) {
				try {
					bouts.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

(2)util类

package cn.ccccltd.pms.process.plan.agile.util;

import java.text.ParseException;
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 PluSoft.Utils.Convert;
import PluSoft.Utils.DateUtil;
import PluSoft.Utils.JSON;
import PluSoft.Utils.StringUtil;

/**
 * 需要下载plusproject官网的jar, 重写甘特图写入方法,因为其他原因导致只优化了部分代码,希望谅解
 * 
 * @author Administrator
 *
 */
public class ProjetExportUtil {

	/**
	 * 重新甘特图插件方法
	 * 
	 * @return
	 */
	protected static ArrayList GetDefaultCalendars() {
		return (ArrayList) JSON.Decode(
				"[{UID: 1,IsBaseCalendar: 1,BaseCalendarUID:-1,Name: '',WeekDays: [{DayType: 1,DayWorking: 0},{DayType: 2,DayWorking: 1},{DayType: 3,DayWorking: 1},{DayType: 4,DayWorking: 1},{DayType: 5,DayWorking: 1},{DayType: 6,DayWorking: 1},{DayType: 7,DayWorking: 0}],Exceptions:[]}]");
	}

	/**
	 * 重写平台(Export.ToXML(hs))的方法,防止查询数据库返回的是CaseInsensitiveMap对象的map,
	 * 平台内部和HashMap不对应
	 * 
	 * @param hs
	 * @return
	 * @throws ParseException
	 */
	@SuppressWarnings("unchecked")
	public static StringBuilder ToXML(Map<String, Object> hs) throws ParseException {
		List<Map<String, Object>> eaList = (List<Map<String, Object>>) hs.get("ExtendedAttributes");
		if (eaList == null) {
			eaList = new ArrayList<Map<String, Object>>();
			hs.put("ExtendedAttributes", eaList);
		}
		Map<String, Object> eaText = new HashMap<String, Object>();
		eaText.put("FieldID", Integer.valueOf(188743731));
		eaText.put("FieldName", "GUID");
		eaList.add(eaText);
		List<Map<String, Object>> tasks = (List<Map<String, Object>>) hs.get("Tasks");
		List<Map<String, Object>> eas;
		for (Map<String, Object> task_o : tasks) {
			Map<String, Object> task = task_o;
			eas = (List<Map<String, Object>>) task.get("ExtendedAttribute");
			if (eas == null) {
				eas = new ArrayList<Map<String, Object>>();
				task.put("ExtendedAttribute", eas);
			}
			Map<String, Object> ea = new HashMap<String, Object>();
			ea.put("FieldID", Integer.valueOf(188743731));
			ea.put("Value", task.get("UID"));
			eas.add(ea);
		}

		if (hs.get("CreationDate") == null) {
			hs.put("CreationDate", new Date());
		}
		if (hs.get("WeekStartDay") == null) {
			hs.put("WeekStartDay", Integer.valueOf(0));
		}
		if (hs.get("DefaultStartTime") == null) {
			hs.put("DefaultStartTime", "08:00:00");
		}
		if (hs.get("DefaultFinishTime") == null) {
			hs.put("DefaultFinishTime", "17:00:00");
		}
		if (hs.get("MinutesPerDay") == null) {
			hs.put("MinutesPerDay", Integer.valueOf(480));
		}
		if (hs.get("MinutesPerWeek") == null) {
			hs.put("MinutesPerWeek", Integer.valueOf(2400));
		}
		if (hs.get("DaysPerMonth") == null) {
			hs.put("DaysPerMonth", Integer.valueOf(20));
		}
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" standalone=\"yes\"?><Project xmlns=\"http://schemas.microsoft.com/project\">");
		sb.append("<SaveVersion>1000</SaveVersion>");
		sb.append("<UID>" + hs.get("UID") + "</UID>");
		sb.append("<Name>" + ToCDATAString(hs.get("Name")) + "</Name>");
		sb.append("<StartDate>" + ToDateTimeString(hs.get("StartDate"), true) + "</StartDate>");
		sb.append("<FinishDate>" + ToDateTimeString(hs.get("FinishDate"), false) + "</FinishDate>");
		sb.append("<CreationDate>" + ToDateTimeString(hs.get("CreationDate")) + "</CreationDate>");
		sb.append("<CalendarUID>" + hs.get("CalendarUID") + "</CalendarUID>");
		sb.append("<WeekStartDay>" + hs.get("WeekStartDay") + "</WeekStartDay>");
		sb.append("<DefaultStartTime>" + hs.get("DefaultStartTime") + "</DefaultStartTime>");
		sb.append("<DefaultFinishTime>" + hs.get("DefaultFinishTime") + "</DefaultFinishTime>");
		sb.append("<MinutesPerDay>" + hs.get("MinutesPerDay") + "</MinutesPerDay>");
		sb.append("<MinutesPerWeek>" + hs.get("MinutesPerWeek") + "</MinutesPerWeek>");
		sb.append("<DaysPerMonth>" + hs.get("DaysPerMonth") + "</DaysPerMonth>");
		sb.append("<ProjectExternallyEdited>0</ProjectExternallyEdited>");

		sb.append("<Calendars>");
		List<Map<String, Object>> calendarList = (List<Map<String, Object>>) hs.get("Calendars");
		if ((calendarList == null) || (calendarList.size() == 0)) {
			calendarList = GetDefaultCalendars();
			hs.put("Calendars", calendarList);
			hs.put("CalendarUID", "1");
		}
		Map<String, Object> ca;
		Map<String, Object> wktm;
		for (Object ca_o : calendarList) {
			ca = (Map<String, Object>) ca_o;
			String caName = ToCDATAString(ca.get("Name"));
			if (StringUtil.isNullOrEmpty(caName)) {
				caName = "Calendar" + ca.get("UID");
			}
			sb.append("<Calendar>");
			sb.append("<UID>" + ca.get("UID") + "</UID>");
			sb.append("<Name>" + caName + "</Name>");
			sb.append("<IsBaseCalendar>" + ca.get("IsBaseCalendar") + "</IsBaseCalendar>");
			sb.append("<BaseCalendarUID>" + ca.get("BaseCalendarUID") + "</BaseCalendarUID>");
			List<Map<String, Object>> weekdayList = (List<Map<String, Object>>) ca.get("WeekDays");
			List<Map<String, Object>> exceptionsList = (List<Map<String, Object>>) ca.get("Exceptions");
			if (exceptionsList == null) {
				exceptionsList = new ArrayList<Map<String, Object>>();
			}
			Map<String, Object> wktm1;
			if (weekdayList != null) {
				sb.append("<WeekDays>");
				for (Object weekday_o : weekdayList) {
					Map<String, Object> weekday = (Map<String, Object>) weekday_o;
					if (weekday.get("DayType").toString().equals("0")) {
						exceptionsList.add(weekday);
					} else {
						sb.append("<WeekDay>");
						sb.append("<DayType>" + weekday.get("DayType") + "</DayType>");
						sb.append("<DayWorking>" + weekday.get("DayWorking") + "</DayWorking>");

						List<Map<String, Object>> wktmList = (List<Map<String, Object>>) weekday.get("WorkingTimes");
						if (wktmList != null) {
							sb.append("<WorkingTimes>");
							for (Object wktm_o : wktmList) {
								wktm1 = (Map<String, Object>) wktm_o;
								sb.append("<WorkingTime>");
								sb.append("<FromTime>" + wktm1.get("FromTime") + "</FromTime>");
								sb.append("<ToTime>" + wktm1.get("ToTime") + "</ToTime>");
								sb.append("</WorkingTime>");
							}
							sb.append("</WorkingTimes>");
						}
						if ((weekday.get("DayWorking").toString().equals("1")) && (wktmList == null)) {
							sb.append(
									"<WorkingTimes><WorkingTime><FromTime>08:00:00</FromTime><ToTime>12:00:00</ToTime></WorkingTime><WorkingTime><FromTime>13:00:00</FromTime><ToTime>17:00:00</ToTime></WorkingTime></WorkingTimes>");
						}

						sb.append("</WeekDay>");
					}
				}
				sb.append("</WeekDays>");
			}
			if (exceptionsList != null) {
				sb.append("<Exceptions>");
				for (Object ex : exceptionsList) {
					Map<String, Object> excep = (Map<String, Object>) ex;
					sb.append("<Exception>");
					sb.append("<EnteredByOccurrences>0</EnteredByOccurrences>");
					sb.append("<Occurrences>1</Occurrences>");
					sb.append("<Type>1</Type>");
					sb.append("<DayWorking>" + excep.get("DayWorking") + "</DayWorking>");
					sb.append("<Name>" + excep.get("Name") + "</Name>");
					sb.append("<TimePeriod>");
					Map<String, Object> tp = (Map<String, Object>) excep.get("TimePeriod");
					sb.append("<FromDate>" + ToDateTimeString(tp.get("FromDate")) + "</FromDate>");
					sb.append("<ToDate>" + ToDateTimeString(tp.get("ToDate")) + "</ToDate>");
					sb.append("</TimePeriod>");

					List<Map<String, Object>> wktmList = (List<Map<String, Object>>) excep.get("WorkingTimes");
					if (wktmList != null) {
						sb.append("<WorkingTimes>");
						for (Object wktm_o : wktmList) {
							wktm1 = (Map<String, Object>) wktm_o;
							sb.append("<WorkingTime>");
							sb.append("<FromTime>" + wktm1.get("FromTime") + "</FromTime>");
							sb.append("<ToTime>" + wktm1.get("ToTime") + "</ToTime>");
							sb.append("</WorkingTime>");
						}
						sb.append("</WorkingTimes>");
					} else if ("1".equals(excep.get("DayWorking").toString())) {
						sb.append(
								"<WorkingTimes><WorkingTime><FromTime>08:00:00</FromTime><ToTime>12:00:00</ToTime></WorkingTime><WorkingTime><FromTime>13:00:00</FromTime><ToTime>17:00:00</ToTime></WorkingTime></WorkingTimes>");
					}
					sb.append("</Exception>");
				}
				sb.append("</Exceptions>");
			}
			sb.append("</Calendar>");
		}
		sb.append("</Calendars>");
		List<Map<String, Object>> ExtendedAttributes = (List<Map<String, Object>>) hs.get("ExtendedAttributes");
		Map<String, Object> ea;
		if (ExtendedAttributes != null) {
			sb.append("<ExtendedAttributes>");
			for (Object ea_o : ExtendedAttributes) {
				ea = (Map<String, Object>) ea_o;
				sb.append("<ExtendedAttribute>");
				sb.append("<FieldID>" + ea.get("FieldID") + "</FieldID>");
				sb.append("<FieldName>" + ea.get("FieldName") + "</FieldName>");
				if (ea.get("Ltuid") != null) {
					sb.append("<Ltuid>" + ea.get("Ltuid") + "</Ltuid>");
				}
				if (ea.get("Alias") != null) {
					sb.append("<Alias>" + ea.get("Alias") + "</Alias>");
				}
				if (ea.get("Guid") != null)
					sb.append("<Guid>" + ea.get("Guid") + "</Guid>");
				sb.append("</ExtendedAttribute>");
			}
			sb.append("</ExtendedAttributes>");
		}

		List<Map<String, Object>> OutlineCodes = (List<Map<String, Object>>) hs.get("OutlineCodes");
		Map<String, Object> mark;
		if (OutlineCodes != null) {
			sb.append("<OutlineCodes>");
			for (Object oc_o : OutlineCodes) {
				Map<String, Object> oc = (Map<String, Object>) oc_o;
				sb.append("<OutlineCode>");
				if (oc.get("Guid") != null)
					sb.append("<Guid>" + oc.get("Guid") + "</Guid>");
				List<Map<String, Object>> marks = (List<Map<String, Object>>) oc.get("Masks");
				if (marks != null) {
					sb.append("<Masks>");
					for (Object mark_o : marks) {
						mark = (Map<String, Object>) mark_o;
						sb.append("<Mask>");
						sb.append("<Level>" + mark.get("Level") + "</Level>");
						sb.append("<Type>" + mark.get("Type") + "</Type>");
						sb.append("<Length>" + mark.get("Length") + "</Length>");
						sb.append("<Separator>" + mark.get("Separator") + "</Separator>");
						sb.append("</Mask>");
					}
					sb.append("</Masks>");
				}

				List<Map<String, Object>> Values = (List<Map<String, Object>>) oc.get("Values");
				if (Values != null) {
					sb.append("<Values>");
					for (Object v_o : Values) {
						Map<String, Object> value = (Map<String, Object>) v_o;
						sb.append("<Value>");
						sb.append("<ValueID>" + value.get("ValueID") + "</ValueID>");
						sb.append("<FieldGUID>" + value.get("FieldGUID") + "</FieldGUID>");
						sb.append("<ParentValueID>" + value.get("ParentValueID") + "</ParentValueID>");
						sb.append("<Type>" + value.get("Type") + "</Type>");
						sb.append("<Value>" + value.get("Value") + "</Value>");
						sb.append("</Value>");
					}
					sb.append("</Values>");
				}
				sb.append("</OutlineCode>");
			}
			sb.append("</OutlineCodes>");
		}
		List<Map<String, Object>> Assignments = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> taskList = (List<Map<String, Object>>) hs.get("Tasks");
		Map<String, Object> UIDTasks = new HashMap<String, Object>();
		Map<String, Object> NewUIDTasks = new HashMap<String, Object>();
		int maxTaskUID = 1;
		Map<String, Object> task;
		for (Object task_o : taskList) {
			task = (Map<String, Object>) task_o;
			if (task.get("UID") != null) {
				UIDTasks.put(task.get("UID").toString(), task);
				task.put("UID", Integer.valueOf(maxTaskUID++));
				NewUIDTasks.put(String.valueOf(task.get("UID")), task);
			}
		}

		int TaskID = 1;
		sb.append("<Tasks>");
		List<Map<String, Object>> ass;
		for (Object task_o : taskList) {
			Map<String, Object> task1 = (Map<String, Object>) task_o;
			sb.append("<Task>");
			sb.append("<IsNull>0</IsNull>");
			sb.append("<UID>" + task1.get("UID") + "</UID>");
			sb.append("<ID>" + TaskID++ + "</ID>");
			sb.append("<Name>" + ToCDATAString(task1.get("Name")) + "</Name>");
			sb.append("<Type>" + (task1.get("Type") == null ? Integer.valueOf(1) : task1.get("Type")) + "</Type>");
			sb.append("<OutlineNumber>" + task1.get("OutlineNumber") + "</OutlineNumber>");
			sb.append("<OutlineLevel>" + task1.get("OutlineLevel") + "</OutlineLevel>");
			if (task1.get("Priority") != null)
				sb.append("<Priority>" + task1.get("Priority") + "</Priority>");
			sb.append("<Start>" + ToDateTimeString(task1.get("Start")) + "</Start>");
			sb.append("<Finish>" + ToDateTimeString(task1.get("Finish")) + "</Finish>");
			sb.append("<Duration>PT" + task1.get("Duration") + "H0M0S</Duration>");
			if (task1.get("Work") != null)
				sb.append("<Work>PT" + task1.get("Work") + "H0M0S</Work>");
			sb.append("<DurationFormat>"
					+ (task1.get("DurationFormat") == null ? Integer.valueOf(7) : task1.get("DurationFormat"))
					+ "</DurationFormat>");
			sb.append("<Estimated>" + (task1.get("Estimated") == null ? Integer.valueOf(0) : task1.get("Estimated"))
					+ "</Estimated>");
			sb.append("<PercentComplete>" + task1.get("PercentComplete") + "</PercentComplete>");
			sb.append("<Milestone>" + (task1.get("Milestone") == null ? Integer.valueOf(0) : task1.get("Milestone"))
					+ "</Milestone>");
			if (task1.get("Summary") != null)
				sb.append("<Summary>" + task1.get("Summary") + "</Summary>");
			if (task1.get("Critical") != null) {
				sb.append("<Critical>" + task1.get("Critical") + "</Critical>");
			}
			if (task1.get("HyperLink") != null) {
				sb.append("<HyperLink>" + ToCDATAString(task1.get("HyperLink")) + "</HyperLink>");
			}
			if (task1.get("HyperLinkAddress") != null) {
				sb.append("<HyperLinkAddress>" + ToCDATAString(task1.get("HyperLinkAddress")) + "</HyperLinkAddress>");
			}
			if (task1.get("Notes") != null) {
				sb.append("<Notes>" + task1.get("Notes") + "</Notes>");
			}
			if (task1.get("ConstraintType") != null)
				sb.append("<ConstraintType>" + task1.get("ConstraintType") + "</ConstraintType>");
			if (task1.get("ConstraintDate") != null) {
				sb.append("<ConstraintDate>" + ToDateTimeString(task1.get("ConstraintDate")) + "</ConstraintDate>");
			}
			if (task1.get("Manual") != null) {
				sb.append("<Manual>" + task1.get("Manual") + "</Manual>");
				if (task1.get("Manual").toString().equals("1")) {
					if (task1.get("ManualStart") == null)
						task1.put("ManualStart", task1.get("Start"));
					if (task1.get("ManualFinish") == null)
						task1.put("ManualFinish", task1.get("Finish"));
					if (task1.get("ManualDuration") == null)
						task1.put("ManualDuration", task1.get("Duration"));
				}
			}
			if (task1.get("ManualStart") != null)
				sb.append("<ManualStart>" + ToDateTimeString(task1.get("ManualStart"), "08:00:00") + "</ManualStart>");
			if (task1.get("ManualFinish") != null)
				sb.append(
						"<ManualFinish>" + ToDateTimeString(task1.get("ManualFinish"), "17:00:00") + "</ManualFinish>");
			if (task1.get("ManualDuration") != null) {
				sb.append("<ManualDuration>PT" + Convert.toInt(task1.get("ManualDuration")) + "H0M0S</ManualDuration>");
			}
			sb.append("<FixedCostAccrual>3</FixedCostAccrual>");
			task1.get("BaseLine");
			if (task1.get("PredecessorLink") != null) {
				List plList = new ArrayList();
				if (task1.get("PredecessorLink").getClass().getName() != "java.util.ArrayList") {
					plList.add(task1.get("PredecessorLink"));
				} else {
					plList = (ArrayList) task1.get("PredecessorLink");
				}
				for (Object pl_o : plList) {
					Map<String, Object> pl = (Map<String, Object>) pl_o;

					if (pl.get("PredecessorUID") != null) {
						Map<String, Object> pTask = (Map<String, Object>) UIDTasks
								.get(pl.get("PredecessorUID").toString());
						if (pTask != null) {
							pl.put("PredecessorUID", pTask.get("UID"));
							sb.append("<PredecessorLink>");
							sb.append("<CrossProject>0</CrossProject>");
							sb.append("<PredecessorUID>" + pl.get("PredecessorUID") + "</PredecessorUID>");
							sb.append("<Type>" + pl.get("Type") + "</Type>");
							sb.append("<LinkLag>" + pl.get("LinkLag") + "</LinkLag>");
							sb.append("<LagFormat>" + pl.get("LagFormat") + "</LagFormat>");
							sb.append("</PredecessorLink>");
						}
					}
				}
			}
			List<Map<String, Object>> ExtendedAttribute = (List<Map<String, Object>>) task1.get("ExtendedAttribute");
			Map<String, Object> ea1;
			if (ExtendedAttribute != null) {
				for (Object ea_o : ExtendedAttribute) {
					ea1 = (Map<String, Object>) ea_o;
					sb.append("<ExtendedAttribute>");
					sb.append("<FieldID>" + ea1.get("FieldID") + "</FieldID>");
					sb.append("<Value>" + ea1.get("Value") + "</Value>");
					if (ea1.get("ValueGUID") != null) {
						sb.append("<ValueGUID>" + ea1.get("ValueGUID") + "</ValueGUID>");
					}
					sb.append("</ExtendedAttribute>");
				}
			}
			sb.append("</Task>");
			ass = (List) task1.get("Assignments");
			if ((ass != null) && (ass.size() > 0)) {
				for (Object ass_o : ass) {
					Map<String, Object> ass_m = (Map<String, Object>) ass_o;
					ass_m.put("TaskUID", task1.get("UID"));
				}
				Assignments.addAll(ass);
			} else {
				ass = new ArrayList<Map<String, Object>>();
				Map<String, Object> a = new HashMap<String, Object>();
				a.put("ResourceUID", Integer.valueOf(-65535));
				a.put("TaskUID", task1.get("UID"));
				a.put("Units", Integer.valueOf(100));
				ass.add(a);
				Assignments.addAll(ass);
			}
		}
		sb.append("</Tasks>");
		Map resourceMaps = new HashMap();
		int resourceUID = 1;

		ArrayList resourcesList = (ArrayList) hs.get("Resources");
		if (resourcesList == null) {
			resourcesList = new ArrayList();
		}
		sb.append("<Resources>");
		HashMap re;
		for (Object re_o : resourcesList) {
			re = (HashMap) re_o;
			int reUID = resourceUID++;
			resourceMaps.put(re.get("UID"), Integer.valueOf(reUID));

			sb.append("<Resource>");
			sb.append("<UID>" + reUID + "</UID>");
			sb.append("<Type>" + re.get("Type") + "</Type>");
			sb.append("<Name>" + ToCDATAString(re.get("Name")) + "</Name>");
			sb.append("</Resource>");
		}
		sb.append("</Resources>");
		ArrayList assignmentList = (ArrayList) hs.get("Assignments");
		HashMap assign;
		if (assignmentList != null) {
			for (Object assign_o : assignmentList) {
				assign = (HashMap) assign_o;
				Map task1 = (Map) UIDTasks.get(assign.get("TaskUID"));
				assign.put("TaskUID", task1.get("UID"));
			}
			Assignments.addAll(assignmentList);
		}
		sb.append("<Assignments>");
		int assUID = 1;

		for (Object assign_o : Assignments) {
			HashMap assign1 = (HashMap) assign_o;

			Map task1 = (Map) NewUIDTasks.get(assign1.get("TaskUID"));
			if (task1 != null) {
				int Summary = Convert.toInt(task1.get("Summary")).intValue();
				if (Summary != 1) {

					String reUID = assign1.get("ResourceUID").toString();
					String reUID2 = resourceMaps.get(reUID) != null ? resourceMaps.get(reUID).toString() : reUID;
					sb.append("<Assignment>");
					sb.append("<UID>" + assUID + "</UID>");
					sb.append("<TaskUID>" + assign1.get("TaskUID") + "</TaskUID>");
					sb.append("<ResourceUID>" + reUID2 + "</ResourceUID>");

					double percent = Convert.toInt(task1.get("PercentComplete")).intValue();
					double work = Convert.toInt(task1.get("Duration")).intValue();
					double actualWork = percent / 100.0D * work;
					sb.append("<Work>PT" + work + "H0M0S</Work>");
					int hours = (int) actualWork;
					int minutes = (int) ((actualWork - hours) * 60.0D);
					sb.append("<ActualWork>PT");
					sb.append(hours + "H");
					sb.append(minutes + "M");
					sb.append("0S</ActualWork>");

					double remainintWrok = work - actualWork;
					hours = (int) remainintWrok;
					minutes = (int) ((remainintWrok - hours) * 60.0D);
					sb.append("<RemainingWork>PT");
					sb.append(hours + "H");
					sb.append(minutes + "M");
					sb.append("0S</RemainingWork>");

					sb.append("<Units>1</Units>");
					if (percent > 0.0D) {
						sb.append("<ActualStart>" + ToDateTimeString(task1.get("Start")) + "</ActualStart>");
					}

					sb.append("<Start>" + ToDateTimeString(task1.get("Start")) + "</Start>");

					sb.append("<TimephasedData>");
					sb.append("<Type>1</Type>");
					sb.append("<UID>" + assUID + "</UID>");
					sb.append("<Start>" + ToDateTimeString(task1.get("Start")) + "</Start>");
					sb.append("<Finish>" + ToDateTimeString(task1.get("Finish")) + "</Finish>");
					sb.append("<Unit>1</Unit>");
					sb.append("<Value>PT" + task1.get("Duration") + "H0M0S</Value>");
					sb.append("</TimephasedData>");
					sb.append("</Assignment>");

					assUID++;
				}
			}
		}
		sb.append("</Assignments>");
		sb.append("</Project>");

		return sb;
	}

	/**
	 * 重新甘特图插件的方法
	 * 
	 * @param o
	 * @return
	 * @throws ParseException
	 */
	private static String ToDateTimeString(Object o) throws ParseException {
		if (o == null)
			return "";
		if (o.getClass().getName() == "java.lang.String") {
			return o.toString();
		}
		Date date = (Date) o;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd啊HH:mm:ss");
		String str = df.format(date);
		str = str.replace("啊", "T");
		return str;
	}

	private static String ToDateTimeString(Object o, boolean clearTime) throws ParseException {
		if (o == null)
			return "";
		if (o.getClass().getName() == "java.lang.String") {
			return o.toString();
		}
		Date date = (Date) o;
		if (clearTime)
			date = DateUtil.clearTime(date);
		else {
			date = DateUtil.createDate(DateUtil.getYear(date), DateUtil.getMonth(date), DateUtil.getDate(date), 23, 59,
					59);
		}
		return ToDateTimeString(date);
	}

	private static String ToCDATAString(Object o) {
		String s = Convert.toString(o);
		if ((s.indexOf("<") != -1) || (s.indexOf(">") != -1) || (s.indexOf("&") != -1)) {
			s = "<![CDATA[" + s + "]]>";
		}
		return s;
	}

	/**
	 * 设置自定义列对应的名称 FieldID 值固定,可通过project软件导出xml,打开xml查看代码中文本或数字对应的对应的FieldID
	 * 
	 * @param @return
	 * 			@return List<Map>
	 * @author
	 */
	public static List<Map<String, Object>> getExtendedAttributes() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> extendedAttributes = new ArrayList<Map<String, Object>>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		// 文本对应的id,固定
		map1.put("FieldID", "188743740");
		// id对应的project的那个列
		map1.put("FieldName", "文本4");
		// 列对应的别名,用于实际展示
		map1.put("Alias", "编码");
		// project导出xml的文本对应的guid
		map1.put("Guid", "000039B7-8BBE-4CEB-82C4-FA8C0B400033");
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("FieldID", "188743734");
		map2.put("FieldName", "文本2");
		map2.put("Alias", "单位");
		map2.put("Guid", "000039B7-8BBE-4CEB-82C4-FA8C0B400036");
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("FieldID", "188743737");
		map3.put("FieldName", "文本3");
		map3.put("Alias", "总数量");
		Map<String, Object> map4 = new HashMap<String, Object>();
		map4.put("FieldID", "188743746");
		map4.put("FieldName", "文本6");
		map4.put("Alias", "形象进度项编码");
		map4.put("Guid", "000039B7-8BBE-4CEB-82C4-FA8C0B400042");
		Map<String, Object> map5 = new HashMap<String, Object>();
		map5.put("FieldID", "188743767");
		map5.put("FieldName", "数字1");
		map5.put("Alias", "总产值");
		map5.put("Guid", "000039B7-8BBE-4CEB-82C4-FA8C0B400057");

		extendedAttributes.add(map1);
		extendedAttributes.add(map2);
		extendedAttributes.add(map3);
		extendedAttributes.add(map4);
		extendedAttributes.add(map5);
		return extendedAttributes;
	}

	/**
	 * 重新甘特图插件方法,写入的时候需要
	 * 
	 * @param o
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	private static String ToDateTimeString(Object o, String time) throws ParseException {
		if (o == null)
			return "";
		if (o.getClass().getName() == "java.lang.String") {
			return o.toString();
		}
		Date date = (Date) o;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd啊");
		String str = df.format(date);
		str = str.replace("啊", "T");
		str = str + time;
		return str;
	}

	/**
	 * 设置自定义列的值信息
	 * 
	 */
	public static Map<String, Object> getExtendedAttribute(Map<String, Object> task) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> extendedAttributes = new ArrayList<Map<String, Object>>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("FieldID", "188743740");
		map1.put("Value", task.get("CODE") == null ? "" : task.get("CODE"));
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("FieldID", "188743734");
		map2.put("Value", task.get("UNIT") == null ? "" : task.get("UNIT"));
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("FieldID", "188743737");
		map3.put("Value", task.get("WORKQTY") == null ? "" : task.get("WORKQTY"));
		Map<String, Object> map4 = new HashMap<String, Object>();
		map4.put("FieldID", "188743746");
		map4.put("Value", task.get("TYPEITEM") == null ? "" : task.get("TYPEITEM"));
		Map<String, Object> map5 = new HashMap<String, Object>();
		map5.put("FieldID", "188743767");
		map5.put("Value", task.get("TOTALAMT") == null ? "" : task.get("TOTALAMT"));

		extendedAttributes.add(map1);
		extendedAttributes.add(map2);
		extendedAttributes.add(map3);
		extendedAttributes.add(map4);
		extendedAttributes.add(map5);
		task.put("ExtendedAttribute", extendedAttributes);
		return task;
	}
}

四、总结,因为网上资料太少,加上我公司必须要导出,自己就深入研究了一下。发布这篇博客,丰富一下这块的资料吧;如果有用,你也有csdn积分的话可以打赏一点,感谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值