java生成XML格式文件,导入MS Project生成甘特图
生活随笔
收集整理的這篇文章主要介紹了
java生成XML格式文件,导入MS Project生成甘特图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
公司需求需要生成如如下圖所示的project甘特圖,開發語言是java,網上找了很久也沒有找到可以利用java生成這種甘特圖的方案。退而求其次,改變需求生成可以導入project的xml文件。
首先用project軟件制作好圖上所示的樣例,并導出為xml文件
導出后發現xml文件里有很多很多很多標簽,完全看不懂,隨便復制幾個:
如果想生成xml文件首先一定要弄明白每個標簽的含義才可以,在網上找了找到了一個解釋標簽含義的網站,對照著大概看了一遍
https://learn.microsoft.com/zh-cn/office/vba/api/project.object然后發現很多標簽都是用不到的,下面是不可省去的核心的最終xml文件和對應的描述:
<?xml version="1.0" encoding="UTF-8"?> <Projectxmlns="http://schemas.microsoft.com/project"><!--project名稱--><Name>甘特圖2.xml</Name><!--標題--><Title>計劃更新甘特圖</Title><ScheduleFromStart>3</ScheduleFromStart><!--項目總的開始時間--><StartDate>2022-07-03T09:00:00</StartDate><!--項目總的結束時間--><FinishDate>2022-08-04T09:00:00</FinishDate><!--使用的日歷,和Calendars下Calendar下的UID保持一致--><CalendarUID>3</CalendarUID><!--日歷--><Calendars><Calendar><UID>3</UID><Name>全年無休</Name><BaseCalendarUID>-1</BaseCalendarUID><WeekDays><WeekDay><!--周一為1,周二為2,以此類推...--><DayType>1</DayType><DayWorking>1</DayWorking><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></WeekDay><WeekDay><DayType>2</DayType><DayWorking>1</DayWorking><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></WeekDay><WeekDay><DayType>3</DayType><DayWorking>1</DayWorking><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></WeekDay><WeekDay><DayType>4</DayType><DayWorking>1</DayWorking><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></WeekDay><WeekDay><DayType>5</DayType><DayWorking>1</DayWorking><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></WeekDay><WeekDay><DayType>6</DayType><DayWorking>1</DayWorking><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></WeekDay><WeekDay><DayType>7</DayType><DayWorking>1</DayWorking><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></WeekDay></WeekDays></Calendar></Calendars><!--任務--><Tasks><Task><!--可以從1開始累加--><ID>1</ID><!--可以從1開始累加--><UID>1</UID><!--使用的日歷,和Calendars下Calendar下的BaseCalendarUID保持一致--><CalendarUID>-1</CalendarUID><!--相當于幾級標題,我這個案例中項目是1,單體是2,安裝單元是3--><OutlineLevel>1</OutlineLevel><!--具體層級,和word中的標題一樣,1、1.1、1.1.1....--><OutlineNumber>1</OutlineNumber><!--要顯示的名稱--><Name>項目名稱1</Name><!--開始時間--><Start>2022-07-03T09:00:00</Start><!--結束時間--><Finish>2022-08-04T09:00:00</Finish></Task><Task><ID>2</ID><UID>2</UID><CalendarUID>-1</CalendarUID><OutlineLevel>2</OutlineLevel><OutlineNumber>1.1</OutlineNumber><Name>單體1</Name><Start>2022-07-20T09:00:00</Start><Finish>2022-08-04T09:00:00</Finish></Task><Task><ID>3</ID><UID>3</UID><CalendarUID>-1</CalendarUID><OutlineLevel>3</OutlineLevel><OutlineNumber>1.1.1</OutlineNumber><Name>安裝單元1</Name><!--最后一級需要寫這個屬性--><Type>1</Type><!--最后一級需要寫這個屬性,數字為結束時間-開始時間天數*8--><RemainingDuration>PT48H0M0S</RemainingDuration><!--最后一級需要寫這個屬性,從哪天開始--><ConstraintDate>2022-07-20T09:00:00</ConstraintDate><Start>2022-07-20T09:00:00</Start><Finish>2022-07-26T09:00:00</Finish></Task><Task><ID>4</ID><UID>4</UID><CalendarUID>-1</CalendarUID><OutlineLevel>3</OutlineLevel><OutlineNumber>1.1.2</OutlineNumber><Name>安裝單元2</Name><Type>1</Type><RemainingDuration>PT32H0M0S</RemainingDuration><ConstraintDate>2022-07-26T09:00:00</ConstraintDate><!--有前置任務的需要寫這個屬性數字為結束時間-開始時間天數*8,和RemainingDuration標簽值一樣--><Duration>PT32H0M0S</Duration><!--前置任務,可以有多個--><PredecessorLink><!--前置任務的task標簽下的UID標簽的值--><PredecessorUID>3</PredecessorUID><Type>1</Type><LagFormat>7</LagFormat></PredecessorLink><Start>2022-07-26T09:00:00</Start><Finish>2022-07-30T09:00:00</Finish></Task><Task><ID>5</ID><UID>5</UID><CalendarUID>-1</CalendarUID><OutlineLevel>3</OutlineLevel><OutlineNumber>1.1.3</OutlineNumber><Name>安裝單元3</Name><Type>1</Type><RemainingDuration>PT40H0M0S</RemainingDuration><ConstraintDate>2022-07-30T09:00:00</ConstraintDate><Duration>PT40H0M0S</Duration><PredecessorLink><PredecessorUID>3</PredecessorUID><Type>1</Type><LagFormat>7</LagFormat></PredecessorLink><PredecessorLink><PredecessorUID>4</PredecessorUID><Type>1</Type><LagFormat>7</LagFormat></PredecessorLink><Start>2022-07-30T09:00:00</Start><Finish>2022-08-04T09:00:00</Finish></Task><Task><ID>6</ID><UID>6</UID><CalendarUID>-1</CalendarUID><OutlineLevel>2</OutlineLevel><OutlineNumber>1.2</OutlineNumber><Name>單體2</Name><Start>2022-07-03T09:00:00</Start><Finish>2022-07-23T09:00:00</Finish></Task><Task><ID>7</ID><UID>7</UID><CalendarUID>-1</CalendarUID><OutlineLevel>3</OutlineLevel><OutlineNumber>1.2.4</OutlineNumber><Name>安裝單元1</Name><Type>1</Type><RemainingDuration>PT80H0M0S</RemainingDuration><ConstraintDate>2022-07-03T09:00:00</ConstraintDate><Start>2022-07-03T09:00:00</Start><Finish>2022-07-13T09:00:00</Finish></Task><Task><ID>8</ID><UID>8</UID><CalendarUID>-1</CalendarUID><OutlineLevel>3</OutlineLevel><OutlineNumber>1.2.5</OutlineNumber><Name>安裝單元2</Name><Type>1</Type><RemainingDuration>PT24H0M0S</RemainingDuration><ConstraintDate>2022-07-13T09:00:00</ConstraintDate><Duration>PT24H0M0S</Duration><PredecessorLink><PredecessorUID>7</PredecessorUID><Type>1</Type><LagFormat>7</LagFormat></PredecessorLink><Start>2022-07-13T09:00:00</Start><Finish>2022-07-16T09:00:00</Finish></Task><Task><ID>9</ID><UID>9</UID><CalendarUID>-1</CalendarUID><OutlineLevel>3</OutlineLevel><OutlineNumber>1.2.6</OutlineNumber><Name>安裝單元3</Name><Type>1</Type><RemainingDuration>PT32H0M0S</RemainingDuration><ConstraintDate>2022-07-16T09:00:00</ConstraintDate><Duration>PT32H0M0S</Duration><PredecessorLink><PredecessorUID>7</PredecessorUID><Type>1</Type><LagFormat>7</LagFormat></PredecessorLink><PredecessorLink><PredecessorUID>8</PredecessorUID><Type>1</Type><LagFormat>7</LagFormat></PredecessorLink><Start>2022-07-16T09:00:00</Start><Finish>2022-07-20T09:00:00</Finish></Task><Task><ID>10</ID><UID>10</UID><CalendarUID>-1</CalendarUID><OutlineLevel>3</OutlineLevel><OutlineNumber>1.2.7</OutlineNumber><Name>安裝單元4</Name><Type>1</Type><RemainingDuration>PT24H0M0S</RemainingDuration><ConstraintDate>2022-07-20T09:00:00</ConstraintDate><Duration>PT24H0M0S</Duration><PredecessorLink><PredecessorUID>7</PredecessorUID><Type>1</Type><LagFormat>7</LagFormat></PredecessorLink><PredecessorLink><PredecessorUID>8</PredecessorUID><Type>1</Type><LagFormat>7</LagFormat></PredecessorLink><PredecessorLink><PredecessorUID>9</PredecessorUID><Type>1</Type><LagFormat>7</LagFormat></PredecessorLink><Start>2022-07-20T09:00:00</Start><Finish>2022-07-23T09:00:00</Finish></Task><Task><ID>11</ID><UID>11</UID><CalendarUID>-1</CalendarUID><OutlineLevel>2</OutlineLevel><OutlineNumber>1.3</OutlineNumber><Name>單體3</Name><Start>2022-07-10T09:00:00</Start><Finish>2022-07-23T09:00:00</Finish></Task><Task><ID>12</ID><UID>12</UID><CalendarUID>-1</CalendarUID><OutlineLevel>3</OutlineLevel><OutlineNumber>1.3.8</OutlineNumber><Name>安裝單元1</Name><Type>1</Type><RemainingDuration>PT24H0M0S</RemainingDuration><ConstraintDate>2022-07-10T09:00:00</ConstraintDate><Start>2022-07-10T09:00:00</Start><Finish>2022-07-13T09:00:00</Finish></Task><Task><ID>13</ID><UID>13</UID><CalendarUID>-1</CalendarUID><OutlineLevel>3</OutlineLevel><OutlineNumber>1.3.9</OutlineNumber><Name>安裝單元2</Name><Type>1</Type><RemainingDuration>PT80H0M0S</RemainingDuration><ConstraintDate>2022-07-13T09:00:00</ConstraintDate><Duration>PT80H0M0S</Duration><PredecessorLink><PredecessorUID>12</PredecessorUID><Type>1</Type><LagFormat>7</LagFormat></PredecessorLink><Start>2022-07-13T09:00:00</Start><Finish>2022-07-23T09:00:00</Finish></Task></Tasks><!--這個是顯示右邊的圖形的,拿我這個案例舉例,有幾個安裝單元就有幾個Assignment標簽--><Assignments><Assignment><!--應該是不重復就可以--><UID>3</UID><!--task標簽下的UID的值--><TaskUID>3</TaskUID><Start>2022-07-20T09:00:00</Start><Finish>2022-07-26T09:00:00</Finish><!--每一天--><TimephasedData><!--task標簽下的UID的值--><UID>3</UID><!--開始時間和結束時間,按天來的就這么寫--><Start>2022-07-20T09:00:00</Start><Finish>2022-07-21T09:00:00</Finish></TimephasedData><TimephasedData><UID>3</UID><Start>2022-07-21T09:00:00</Start><Finish>2022-07-22T09:00:00</Finish></TimephasedData><TimephasedData><UID>3</UID><Start>2022-07-22T09:00:00</Start><Finish>2022-07-23T09:00:00</Finish></TimephasedData><TimephasedData><UID>3</UID><Start>2022-07-23T09:00:00</Start><Finish>2022-07-24T09:00:00</Finish></TimephasedData><TimephasedData><UID>3</UID><Start>2022-07-24T09:00:00</Start><Finish>2022-07-25T09:00:00</Finish></TimephasedData><TimephasedData><UID>3</UID><Start>2022-07-25T09:00:00</Start><Finish>2022-07-26T09:00:00</Finish></TimephasedData></Assignment><Assignment><UID>4</UID><TaskUID>4</TaskUID><Start>2022-07-26T09:00:00</Start><Finish>2022-07-30T09:00:00</Finish><TimephasedData><UID>4</UID><Start>2022-07-26T09:00:00</Start><Finish>2022-07-27T09:00:00</Finish></TimephasedData><TimephasedData><UID>4</UID><Start>2022-07-27T09:00:00</Start><Finish>2022-07-28T09:00:00</Finish></TimephasedData><TimephasedData><UID>4</UID><Start>2022-07-28T09:00:00</Start><Finish>2022-07-29T09:00:00</Finish></TimephasedData><TimephasedData><UID>4</UID><Start>2022-07-29T09:00:00</Start><Finish>2022-07-30T09:00:00</Finish></TimephasedData></Assignment><Assignment><UID>5</UID><TaskUID>5</TaskUID><Start>2022-07-30T09:00:00</Start><Finish>2022-08-04T09:00:00</Finish><TimephasedData><UID>5</UID><Start>2022-07-30T09:00:00</Start><Finish>2022-07-31T09:00:00</Finish></TimephasedData><TimephasedData><UID>5</UID><Start>2022-07-31T09:00:00</Start><Finish>2022-08-01T09:00:00</Finish></TimephasedData><TimephasedData><UID>5</UID><Start>2022-08-01T09:00:00</Start><Finish>2022-08-02T09:00:00</Finish></TimephasedData><TimephasedData><UID>5</UID><Start>2022-08-02T09:00:00</Start><Finish>2022-08-03T09:00:00</Finish></TimephasedData><TimephasedData><UID>5</UID><Start>2022-08-03T09:00:00</Start><Finish>2022-08-04T09:00:00</Finish></TimephasedData></Assignment><Assignment><UID>7</UID><TaskUID>7</TaskUID><Start>2022-07-03T09:00:00</Start><Finish>2022-07-13T09:00:00</Finish><TimephasedData><UID>7</UID><Start>2022-07-03T09:00:00</Start><Finish>2022-07-04T09:00:00</Finish></TimephasedData><TimephasedData><UID>7</UID><Start>2022-07-04T09:00:00</Start><Finish>2022-07-05T09:00:00</Finish></TimephasedData><TimephasedData><UID>7</UID><Start>2022-07-05T09:00:00</Start><Finish>2022-07-06T09:00:00</Finish></TimephasedData><TimephasedData><UID>7</UID><Start>2022-07-06T09:00:00</Start><Finish>2022-07-07T09:00:00</Finish></TimephasedData><TimephasedData><UID>7</UID><Start>2022-07-07T09:00:00</Start><Finish>2022-07-08T09:00:00</Finish></TimephasedData><TimephasedData><UID>7</UID><Start>2022-07-08T09:00:00</Start><Finish>2022-07-09T09:00:00</Finish></TimephasedData><TimephasedData><UID>7</UID><Start>2022-07-09T09:00:00</Start><Finish>2022-07-10T09:00:00</Finish></TimephasedData><TimephasedData><UID>7</UID><Start>2022-07-10T09:00:00</Start><Finish>2022-07-11T09:00:00</Finish></TimephasedData><TimephasedData><UID>7</UID><Start>2022-07-11T09:00:00</Start><Finish>2022-07-12T09:00:00</Finish></TimephasedData><TimephasedData><UID>7</UID><Start>2022-07-12T09:00:00</Start><Finish>2022-07-13T09:00:00</Finish></TimephasedData></Assignment><Assignment><UID>8</UID><TaskUID>8</TaskUID><Start>2022-07-13T09:00:00</Start><Finish>2022-07-16T09:00:00</Finish><TimephasedData><UID>8</UID><Start>2022-07-13T09:00:00</Start><Finish>2022-07-14T09:00:00</Finish></TimephasedData><TimephasedData><UID>8</UID><Start>2022-07-14T09:00:00</Start><Finish>2022-07-15T09:00:00</Finish></TimephasedData><TimephasedData><UID>8</UID><Start>2022-07-15T09:00:00</Start><Finish>2022-07-16T09:00:00</Finish></TimephasedData></Assignment><Assignment><UID>9</UID><TaskUID>9</TaskUID><Start>2022-07-16T09:00:00</Start><Finish>2022-07-20T09:00:00</Finish><TimephasedData><UID>9</UID><Start>2022-07-16T09:00:00</Start><Finish>2022-07-17T09:00:00</Finish></TimephasedData><TimephasedData><UID>9</UID><Start>2022-07-17T09:00:00</Start><Finish>2022-07-18T09:00:00</Finish></TimephasedData><TimephasedData><UID>9</UID><Start>2022-07-18T09:00:00</Start><Finish>2022-07-19T09:00:00</Finish></TimephasedData><TimephasedData><UID>9</UID><Start>2022-07-19T09:00:00</Start><Finish>2022-07-20T09:00:00</Finish></TimephasedData></Assignment><Assignment><UID>10</UID><TaskUID>10</TaskUID><Start>2022-07-20T09:00:00</Start><Finish>2022-07-23T09:00:00</Finish><TimephasedData><UID>10</UID><Start>2022-07-20T09:00:00</Start><Finish>2022-07-21T09:00:00</Finish></TimephasedData><TimephasedData><UID>10</UID><Start>2022-07-21T09:00:00</Start><Finish>2022-07-22T09:00:00</Finish></TimephasedData><TimephasedData><UID>10</UID><Start>2022-07-22T09:00:00</Start><Finish>2022-07-23T09:00:00</Finish></TimephasedData></Assignment><Assignment><UID>12</UID><TaskUID>12</TaskUID><Start>2022-07-10T09:00:00</Start><Finish>2022-07-13T09:00:00</Finish><TimephasedData><UID>12</UID><Start>2022-07-10T09:00:00</Start><Finish>2022-07-11T09:00:00</Finish></TimephasedData><TimephasedData><UID>12</UID><Start>2022-07-11T09:00:00</Start><Finish>2022-07-12T09:00:00</Finish></TimephasedData><TimephasedData><UID>12</UID><Start>2022-07-12T09:00:00</Start><Finish>2022-07-13T09:00:00</Finish></TimephasedData></Assignment><Assignment><UID>13</UID><TaskUID>13</TaskUID><Start>2022-07-13T09:00:00</Start><Finish>2022-07-23T09:00:00</Finish><TimephasedData><UID>13</UID><Start>2022-07-13T09:00:00</Start><Finish>2022-07-14T09:00:00</Finish></TimephasedData><TimephasedData><UID>13</UID><Start>2022-07-14T09:00:00</Start><Finish>2022-07-15T09:00:00</Finish></TimephasedData><TimephasedData><UID>13</UID><Start>2022-07-15T09:00:00</Start><Finish>2022-07-16T09:00:00</Finish></TimephasedData><TimephasedData><UID>13</UID><Start>2022-07-16T09:00:00</Start><Finish>2022-07-17T09:00:00</Finish></TimephasedData><TimephasedData><UID>13</UID><Start>2022-07-17T09:00:00</Start><Finish>2022-07-18T09:00:00</Finish></TimephasedData><TimephasedData><UID>13</UID><Start>2022-07-18T09:00:00</Start><Finish>2022-07-19T09:00:00</Finish></TimephasedData><TimephasedData><UID>13</UID><Start>2022-07-19T09:00:00</Start><Finish>2022-07-20T09:00:00</Finish></TimephasedData><TimephasedData><UID>13</UID><Start>2022-07-20T09:00:00</Start><Finish>2022-07-21T09:00:00</Finish></TimephasedData><TimephasedData><UID>13</UID><Start>2022-07-21T09:00:00</Start><Finish>2022-07-22T09:00:00</Finish></TimephasedData><TimephasedData><UID>13</UID><Start>2022-07-22T09:00:00</Start><Finish>2022-07-23T09:00:00</Finish></TimephasedData></Assignment></Assignments> </Project>代碼:
public class GenerateGanttProjectXml {// 創建數據public static ProjectEntity generateData() {List<UnitEntity> unitEntityList = new ArrayList<>();UnitEntity unitEntity = new UnitEntity("單體1", "安裝單元1", "", LocalDate.parse("2022-07-20"), LocalDate.parse("2022-07-26"));unitEntityList.add(unitEntity);unitEntity = new UnitEntity("單體1", "安裝單元2", "安裝單元1", LocalDate.parse("2022-07-26"), LocalDate.parse("2022-07-30"));unitEntityList.add(unitEntity);unitEntity = new UnitEntity("單體1", "安裝單元3", "安裝單元1,安裝單元2", LocalDate.parse("2022-07-30"), LocalDate.parse("2022-08-04"));unitEntityList.add(unitEntity);unitEntity = new UnitEntity("單體2", "安裝單元1", "",LocalDate.parse("2022-07-03"), LocalDate.parse("2022-07-13"));unitEntityList.add(unitEntity);unitEntity = new UnitEntity("單體2", "安裝單元2", "安裝單元1",LocalDate.parse("2022-07-13"), LocalDate.parse("2022-07-16"));unitEntityList.add(unitEntity);unitEntity = new UnitEntity("單體2", "安裝單元3", "安裝單元1,安裝單元2",LocalDate.parse("2022-07-16"), LocalDate.parse("2022-07-20"));unitEntityList.add(unitEntity);unitEntity = new UnitEntity("單體2", "安裝單元4", "安裝單元1,安裝單元2,安裝單元3",LocalDate.parse("2022-07-20"), LocalDate.parse("2022-07-23"));unitEntityList.add(unitEntity);unitEntity = new UnitEntity("單體3", "安裝單元1", "",LocalDate.parse("2022-07-10"), LocalDate.parse("2022-07-13"));unitEntityList.add(unitEntity);unitEntity = new UnitEntity("單體3", "安裝單元2", "安裝單元1",LocalDate.parse("2022-07-13"), LocalDate.parse("2022-07-23"));unitEntityList.add(unitEntity);ProjectEntity projectEntity = new ProjectEntity("項目編號1", "項目名稱1", "合同編號1", "項目經理1", LocalDate.parse("2022-09-23"), unitEntityList);//給項目的開始時間和結束時間賦值LocalDate minDate = projectEntity.getEntityList().get(0).getStartTime();LocalDate maxDate = projectEntity.getEntityList().get(0).getEndTime();for (int i = 0; i < projectEntity.getEntityList().size(); i ++) {if (projectEntity.getEntityList().get(i).getStartTime().isBefore(minDate)) {minDate = projectEntity.getEntityList().get(i).getStartTime();}if (projectEntity.getEntityList().get(i).getEndTime().isAfter(maxDate)) {maxDate = projectEntity.getEntityList().get(i).getEndTime();}}projectEntity.setStartTime(minDate);projectEntity.setEndTime(maxDate);// 給單體的開始時間和結束時間賦值int insertIndex = 0;int toIndex = 0;LocalDate minDate2 = null;LocalDate maxDate2 = null;if (projectEntity.getEntityList().size() == 1) {UnitEntity unitEntity2 = new UnitEntity(projectEntity.getEntityList().get(0).getSingleBodyName(), "", "",projectEntity.getEntityList().get(0).getStartTime(), projectEntity.getEntityList().get(0).getEndTime());projectEntity.getEntityList().add(0, unitEntity2);} else {for (int i = 0; i < projectEntity.getEntityList().size() - 1; i++) {//獲取第0列和第1列每行單元格的 值 如果相等 將合并列的數量加一String name1 = projectEntity.getEntityList().get(i).getSingleBodyName();String name2 = projectEntity.getEntityList().get(i + 1).getSingleBodyName();if (name2.equals(name1)) {toIndex ++;} else {// 獲取最大日期和最小日期//循環數據 獲取最大日期和最小日期并得到天數查minDate2 = projectEntity.getEntityList().get(insertIndex).getStartTime();maxDate2 = projectEntity.getEntityList().get(toIndex).getEndTime();for (int j = insertIndex; j < toIndex; j++ ) {if (projectEntity.getEntityList().get(i).getStartTime().isBefore(minDate2)) {minDate2 = projectEntity.getEntityList().get(i).getStartTime();}if (projectEntity.getEntityList().get(i).getEndTime().isAfter(maxDate2)) {maxDate2 = projectEntity.getEntityList().get(i).getEndTime();}}UnitEntity unitEntity2 = new UnitEntity(projectEntity.getEntityList().get(i).getSingleBodyName(), "", "", minDate2, maxDate2);projectEntity.getEntityList().add(insertIndex, unitEntity2);i++;insertIndex = i + 1;toIndex = i + 1;}if (i == projectEntity.getEntityList().size() - 2) {minDate2 = projectEntity.getEntityList().get(insertIndex).getStartTime();maxDate2 = projectEntity.getEntityList().get(toIndex).getEndTime();for (int j = insertIndex; j < toIndex; j++ ) {if (projectEntity.getEntityList().get(i).getStartTime().isBefore(minDate2)) {minDate2 = projectEntity.getEntityList().get(i).getStartTime();}if (projectEntity.getEntityList().get(i).getEndTime().isAfter(maxDate2)) {maxDate2 = projectEntity.getEntityList().get(i).getEndTime();}}UnitEntity unitEntity2 = new UnitEntity(projectEntity.getEntityList().get(i).getSingleBodyName(), "", "", minDate2, maxDate2);projectEntity.getEntityList().add(insertIndex, unitEntity2);break;}}}UnitEntity unitEntity2 = new UnitEntity(projectEntity.getProjectName(), "", "", projectEntity.getStartTime(), projectEntity.getEndTime());projectEntity.getEntityList().add(0, unitEntity2);return projectEntity;}public static void generateXml(ProjectEntity projectEntity, String localXmlPath) {String xmlName = "甘特圖2.xml";String xmlTitle = "計劃更新甘特圖";String xmlCalendarUID = "3";try {// 創建解析器工廠DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder db = factory.newDocumentBuilder();Document document = db.newDocument();document.setXmlStandalone(true);Element project = document.createElement("Project");project.setAttribute("xmlns", "http://schemas.microsoft.com/project"); // <Name>Element name = document.createElement("Name");name.setTextContent(xmlName);project.appendChild(name); // <Title>Element title = document.createElement("Title");title.setTextContent(xmlTitle);project.appendChild(title); // <ScheduleFromStart>Element scheduleFromStart = document.createElement("ScheduleFromStart");scheduleFromStart.setTextContent(xmlCalendarUID);project.appendChild(scheduleFromStart); // <StartDate>Element startDate = document.createElement("StartDate");startDate.setTextContent(projectEntity.getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "T09:00:00");project.appendChild(startDate); // <FinishDate>Element finishDate = document.createElement("FinishDate");finishDate.setTextContent(projectEntity.getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "T09:00:00");project.appendChild(finishDate); // <CalendarUID>Element calendarUID = document.createElement("CalendarUID");calendarUID.setTextContent(xmlCalendarUID);project.appendChild(calendarUID);//創建日歷Element calendars = createCalendarsElement(document);project.appendChild(calendars);//創建任務Element tasks = createTasksElement(document, projectEntity);project.appendChild(tasks); // 創建AssignmentsElement assignments = createAssignmentsElement(document, projectEntity);project.appendChild(assignments);document.appendChild(project);TransformerFactory tff = TransformerFactory.newInstance();Transformer tf = tff.newTransformer();// 輸出內容是否使用換行tf.setOutputProperty(OutputKeys.INDENT, "yes");// 創建xml文件并寫入內容tf.transform(new DOMSource(document), new StreamResult(new File(localXmlPath)));System.out.println("生成xml成功");} catch (Exception e) {e.printStackTrace();System.out.println("生成xml失敗");}}public static Element createCalendarsElement(Document document) {Element calendars = document.createElement("Calendars");Element calendar = document.createElement("Calendar");calendars.appendChild(calendar); // <UID>Element UID = document.createElement("UID");UID.setTextContent("3");calendar.appendChild(UID); // <Name>Element name = document.createElement("Name");name.setTextContent("全年無休");calendar.appendChild(name); // <BaseCalendarUID>Element baseCalendarUID = document.createElement("BaseCalendarUID");baseCalendarUID.setTextContent("-1");calendar.appendChild(baseCalendarUID); // <WeekDays>Element weekDays = document.createElement("WeekDays");calendar.appendChild(weekDays); // <WeekDay>Element weekDay = null;for (int i = 1; i <= 7; i ++) {weekDay = document.createElement("WeekDay");weekDays.appendChild(weekDay); // <DayType>Element dayType = document.createElement("DayType");dayType.setTextContent(i + "");weekDay.appendChild(dayType); // <DayWorking>Element dayWorking = document.createElement("DayWorking");dayWorking.setTextContent("1");weekDay.appendChild(dayWorking); // <WorkingTimes>Element workingTimes = document.createElement("WorkingTimes");weekDay.appendChild(workingTimes);// <WorkingTime>Element workingTime = null;Element fromTime = null;Element toTime = null;for (int j = 1; j <= 2; j ++) {workingTime = document.createElement("WorkingTime");workingTimes.appendChild(workingTime);if (j == 1) { // <FromTime>fromTime = document.createElement("FromTime");fromTime.setTextContent("08:00:00");workingTime.appendChild(fromTime); // <ToTime>toTime = document.createElement("ToTime");toTime.setTextContent("12:00:00");workingTime.appendChild(toTime);} else { // <FromTime>fromTime = document.createElement("FromTime");fromTime.setTextContent("13:00:00");workingTime.appendChild(fromTime); // <ToTime>toTime = document.createElement("ToTime");toTime.setTextContent("17:00:00");workingTime.appendChild(toTime);}}}return calendars;}public static Element createTasksElement(Document document, ProjectEntity projectEntity) {Element tasks = document.createElement("Tasks");//taskElement task = null;Element id = null;Element uid = null;Element calendarUID = null;Element outlineLevel = null;Element outlineNumber = null;Element start = null;Element finish = null;Element name = null;Element type = null;Element remainingDuration = null;Element predecessorLink = null;Element predecessorUID = null;Element type2 = null;Element lagFormat = null;Element constraintDate = null;Element duration = null;List<UnitEntity> unitEntityList = projectEntity.getEntityList();String calendarUIDValue = "-1";int projectNum = 0;int monomerNum = 0;int unitNum = 0;int preBegin = 0;for (int i = 0; i < unitEntityList.size(); i ++) {task = document.createElement("Task");id = document.createElement("ID");id.setTextContent(i + 1 + "");task.appendChild(id);uid = document.createElement("UID");uid.setTextContent(i + 1 + "");task.appendChild(uid);calendarUID = document.createElement("CalendarUID");calendarUID.setTextContent(calendarUIDValue);task.appendChild(calendarUID);// OutlineLevel // 如果是項目(第一條是項目)值為1 單體(安裝單元編號為空是單體)值為2 安裝單元值為2String outlineLevelValue = null;outlineLevel = document.createElement("OutlineLevel");if (i == 0) {outlineLevelValue = "1";projectNum ++;} else if (unitEntityList.get(i).getUnitCode() == null || "".equals(unitEntityList.get(i).getUnitCode())) {outlineLevelValue = "2";monomerNum ++;preBegin = i;} else {outlineLevelValue = "3";unitNum ++;}outlineLevel.setTextContent(outlineLevelValue);task.appendChild(outlineLevel); // OutlineNumber // 級別編號 需計算outlineNumber = document.createElement("OutlineNumber");String outlineNumberValue = "";if ("1".equals(outlineLevelValue)) {outlineNumberValue = projectNum + "";} else if ("2".equals(outlineLevelValue)) {outlineNumberValue = projectNum + "." + monomerNum;} else {outlineNumberValue = projectNum + "." + monomerNum + "." + unitNum;}outlineNumber.setTextContent(outlineNumberValue);task.appendChild(outlineNumber);name = document.createElement("Name");String nameValue = "";if ("1".equals(outlineLevelValue)) {nameValue = unitEntityList.get(i).getSingleBodyName();} else if ("2".equals(outlineLevelValue)) {nameValue = unitEntityList.get(i).getSingleBodyName();} else {nameValue = unitEntityList.get(i).getUnitCode();}name.setTextContent(nameValue);task.appendChild(name);if ("3".equals(outlineLevelValue)) {type = document.createElement("Type");type.setTextContent("1");task.appendChild(type);//獲取日期差LocalDate minDate = unitEntityList.get(i).getStartTime();LocalDate maxDate = unitEntityList.get(i).getEndTime();int dayDiff = (int) (maxDate.toEpochDay()-minDate.toEpochDay());int remainingDurationValue = dayDiff * 8;String remainingDurationStrValue = "PT" + remainingDurationValue + "H0M0S";remainingDuration = document.createElement("RemainingDuration");remainingDuration.setTextContent(remainingDurationStrValue);task.appendChild(remainingDuration);constraintDate = document.createElement("ConstraintDate");constraintDate.setTextContent(unitEntityList.get(i).getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "T09:00:00");task.appendChild(constraintDate);// 如果有前置任務if (unitEntityList.get(i).getPreUnitCodes() != null && !"".equals(unitEntityList.get(i).getPreUnitCodes())) {duration = document.createElement("Duration");duration.setTextContent(remainingDurationStrValue);task.appendChild(duration);// 根據安裝單元編碼 獲取前置任務的UIDString[] preScheduleNames = unitEntityList.get(i).getPreUnitCodes().split(",");String[] preUIDS = new String[preScheduleNames.length];for (int m = 0; m < preScheduleNames.length; m ++) {for (int n = preBegin; n < i; n ++) { // 如果前面的安裝單元編碼和前置任務中的安裝編碼相同if (unitEntityList.get(n).getUnitCode().equals(preScheduleNames[m])) {preUIDS[m] = n + 1 + "";break;}}}for (int m = 0; m < preScheduleNames.length; m ++) {predecessorLink = document.createElement("PredecessorLink");task.appendChild(predecessorLink);//前置任務的UIDpredecessorUID = document.createElement("PredecessorUID");predecessorUID.setTextContent(preUIDS[m]);predecessorLink.appendChild(predecessorUID);type2 = document.createElement("Type");type2.setTextContent("1");predecessorLink.appendChild(type2);lagFormat = document.createElement("LagFormat");lagFormat.setTextContent("7");predecessorLink.appendChild(lagFormat);}}}// <Start>start = document.createElement("Start");start.setTextContent(unitEntityList.get(i).getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "T09:00:00");task.appendChild(start); // <Finish>finish = document.createElement("Finish");finish.setTextContent(unitEntityList.get(i).getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "T09:00:00");task.appendChild(finish);tasks.appendChild(task);}return tasks;}public static Element createAssignmentsElement(Document document, ProjectEntity projectEntity) {Element assignments = document.createElement("Assignments");Element assignment = null;Element UID = null;Element taskUID = null;Element start = null;Element finish = null;Element timephasedData = null;Element UID2 = null;Element start2 = null;Element finish2 = null;//獲取安裝單元所在列表的下標List<Integer> index = new ArrayList<>();List<UnitEntity> entityList = projectEntity.getEntityList();for (int i = 0; i < entityList.size(); i ++) {if (entityList.get(i).getUnitCode() != null && !"".equals(entityList.get(i).getUnitCode())) {assignment = document.createElement("Assignment");assignments.appendChild(assignment);UID = document.createElement("UID");UID.setTextContent(i + 1 + "");assignment.appendChild(UID);taskUID = document.createElement("TaskUID");taskUID.setTextContent(i + 1 + "");assignment.appendChild(taskUID);start = document.createElement("Start");start.setTextContent(entityList.get(i).getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "T09:00:00");assignment.appendChild(start);finish = document.createElement("Finish");finish.setTextContent(entityList.get(i).getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "T09:00:00");assignment.appendChild(finish);//獲取開始日期和結束日期時間差LocalDate minDate = entityList.get(i).getStartTime();LocalDate maxDate = entityList.get(i).getEndTime();int dayDiff = (int) (maxDate.toEpochDay()-minDate.toEpochDay());for(int j = 0; j < dayDiff; j ++) {timephasedData = document.createElement("TimephasedData");assignment.appendChild(timephasedData);UID2 = document.createElement("UID");UID2.setTextContent(i + 1 + "");timephasedData.appendChild(UID2);start2 = document.createElement("Start");start2.setTextContent(entityList.get(i).getStartTime().plusDays(j).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "T09:00:00");timephasedData.appendChild(start2);finish2 = document.createElement("Finish");finish2.setTextContent(entityList.get(i).getStartTime().plusDays(j+1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "T09:00:00");timephasedData.appendChild(finish2);}}}return assignments;}public static void main(String[] args) { // 獲取數據ProjectEntity projectEntity = generateData(); // 生成xml文件String localXmlPath = "D:\\文件\\公司\\需求\\甘特圖\\甘特圖project.xml";generateXml(projectEntity, localXmlPath);} }ProjectEntity類:
public class ProjectEntity {private String projectCode;private String projectName;private String contractCode;private String projectMaster;private LocalDate planCompleteTime;private List<UnitEntity> entityList;private String singleBodyName;private String unitCode;private String preUnitCodes;private LocalDate startTime;private LocalDate endTime;public ProjectEntity(String projectCode, String singleBodyName, String unitCode, LocalDate startTime, LocalDate endTime) {this.projectCode = projectCode;this.singleBodyName = singleBodyName;this.unitCode = unitCode;this.startTime = startTime;this.endTime = endTime;}public String getSingleBodyName() {return singleBodyName;}public void setSingleBodyName(String singleBodyName) {this.singleBodyName = singleBodyName;}public String getUnitCode() {return unitCode;}public void setUnitCode(String unitCode) {this.unitCode = unitCode;}public String getPreUnitCodes() {return preUnitCodes;}public void setPreUnitCodes(String preUnitCodes) {this.preUnitCodes = preUnitCodes;}public LocalDate getStartTime() {return startTime;}public void setStartTime(LocalDate startTime) {this.startTime = startTime;}public LocalDate getEndTime() {return endTime;}public void setEndTime(LocalDate endTime) {this.endTime = endTime;}public ProjectEntity(String projectCode, String projectName, String contractCode, String projectMaster, LocalDate planCompleteTime, List<UnitEntity> entityList) {this.projectCode = projectCode;this.projectName = projectName;this.contractCode = contractCode;this.projectMaster = projectMaster;this.planCompleteTime = planCompleteTime;this.entityList = entityList;}public String getProjectCode() {return projectCode;}public void setProjectCode(String projectCode) {this.projectCode = projectCode;}public String getProjectName() {return projectName;}public void setProjectName(String projectName) {this.projectName = projectName;}public String getContractCode() {return contractCode;}public void setContractCode(String contractCode) {this.contractCode = contractCode;}public String getProjectMaster() {return projectMaster;}public void setProjectMaster(String projectMaster) {this.projectMaster = projectMaster;}public LocalDate getPlanCompleteTime() {return planCompleteTime;}public void setPlanCompleteTime(LocalDate planCompleteTime) {this.planCompleteTime = planCompleteTime;}public List<UnitEntity> getEntityList() {return entityList;}public void setEntityList(List<UnitEntity> entityList) {this.entityList = entityList;}}UnitEntity類:
public class UnitEntity {private String singleBodyName;private String unitCode;private String preUnitCodes;private LocalDate startTime;private LocalDate endTime;public UnitEntity(String singleBodyName, String unitCode, String preUnitCodes, LocalDate startTime, LocalDate endTime) {this.singleBodyName = singleBodyName;this.unitCode = unitCode;this.preUnitCodes = preUnitCodes;this.startTime = startTime;this.endTime = endTime;}public String getSingleBodyName() {return singleBodyName;}public void setSingleBodyName(String singleBodyName) {this.singleBodyName = singleBodyName;}public String getUnitCode() {return unitCode;}public void setUnitCode(String unitCode) {this.unitCode = unitCode;}public String getPreUnitCodes() {return preUnitCodes;}public void setPreUnitCodes(String preUnitCodes) {this.preUnitCodes = preUnitCodes;}public LocalDate getStartTime() {return startTime;}public void setStartTime(LocalDate startTime) {this.startTime = startTime;}public LocalDate getEndTime() {return endTime;}public void setEndTime(LocalDate endTime) {this.endTime = endTime;} }總結
以上是生活随笔為你收集整理的java生成XML格式文件,导入MS Project生成甘特图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php,mysql的简历_PHP开发工程
- 下一篇: 身份证号检测工具