2023年12月7日发(作者:)

javampxjmpxwriter_Mpp的一种java读写解决方案

Mpp 的导入导出

可以选用现成的 Mpxj 已有丰富的APi可用了,但是唯一不足的是MPXJ只能读mpp文件,写不支持但是支持写mpx文件

只要不能写mpp的原因在这里

Can I use MPXJ to write MPP files?

Not at present. Although it is technically feasible to generate an MPP file, the knowledge we have of the file structure is still

relatively incomplete, despite the amount of data we are able to correctly extract. It is therefore likely to take a considerable

amount of development effort to make this work, and it is conceivable that we will not be able to write the full set of

attributes that MPXJ supports back into the MPP file - simply because we don't understand the format well enough. You are

therefore probably better off using MSPDI which does support the full range of data items present in an MPP file.

ex:

File in = new File(m_basedir + "/");

ProjectFile mpx = new MPXReader().read(in);

out = TempFile("junit", ".mpx");

MPXWriter writer = new MPXWriter();

LocaleDefaults(false);

(mpx, out);

实际上这里的ProjectFile 的成功读取MPP文件接下来 通过它你就可以得心应手的获取该Mpp文件的各种信息了

既然读比较方便下面重点说说写吧,毕竟这个才是大头,好多人搞不定的地方。

基本上是一个思路 jni 处理,但是虽是用jni 也有不同的选择的

1. 网上有人用 Jawin 的不可否认处理还算方便的 详细请参考 这里不做过多说明;

2. 自己写JNI封装代码 ,这个要求的功力恐怕也不小 (仅个人拙见);

3. 还有就是 用JACOB 调用ActiveXComponent 当然实际上还是JNI调用;

Jawin 仅支持32位的好像到2005年之后没有什么改进的(是不是这个项目已经没人维护了,还未可知),资料都比较老了,经再

三斟酌我放弃了这个方案;而考虑到JNI的封装的麻烦,而且需要的DLL库的整理也比较繁琐,就优先考虑用 ActiveXComponent

处理。

既然可以写mpx,那最直接的方案就是先用mpxj写成mpx文件,剩下的考虑一件事就行了,就是把mpx转换成mpp。

下面就是关于如何利用 jacob的 ActiveXComponent 和 mpxj 把mpx文件转换成 mpp文件即可了

1 /**

2

3 工具类 把mpx转换成mpp ,这个依赖与系统已安装了project 20034

5 **/

6

78

9 public classMpx2MppUtil {10

11

12

13 private static ActiveXComponent activexComponent = null;14

15

16

17 @SuppressWarnings({ "deprecation", "unused"})18

19 /**

20

21 *@parammpxFilePath mpx文件的文件路径包括文件名和文件后缀22

23 * 转换执行成功,会在同文件夹下创建同文件名的mpp文件;24

25 * 如:入参是 C:26

27 * 成功转换后会在输出 C:28

29 *30

31 */

32

33 public static booleanconvertMpxToMpp(String mpxFilePath) {34

35 int i = 0;36

37 boolean retBooli = false;38

39 if((OrEmpty(mpxFilePath))40

41 || (() < 5)) {42

43 returnretBooli;44

45 }46

47 int j = 0;48

49 try{50

51 int k = 0;52

53 boolean retBoolk = false;54

55 try{56

57 A();58

59 j = 1;60

61 } catch(Error localError1) {62

63 k =i;64

65 retBoolk =retBooli;6667 try{6869 if (activexComponent != null) {7071 (activexComponent, "Quit",7273 new Object[] { f(0) });7475 }7677 } catch(Exception localException5) {78798081 }8283 try{8485 if (j != 0) {8687 e();8889 }9091 } catch(Error localError4) {9293 }9495 returnretBoolk;9697 }9899 activexComponent = new ActiveXComponent("ation");100101 try{102103 Variant localVariant = perty("Version");104105 if (le() >= 12.0D) {106107 k =i;108109 try{110111 if (activexComponent != null) {112113 (activexComponent, "Quit",114115 new Object[] { f(0) });116117 }118119 } catch(Exception localException6) {120121 }122123 try{124125 if (j != 0) {126127 e();128129 }130131 } catch(Error localError5) {132133 }134135 returnretBoolk;136137 //return k;138139 }140141 } catch(Exception localException1) {142143 LogFactory144145 .getLog()146147 .warn("Please install the Microsoft Project 2003 on the server");148149 }150151 perty("Visible", new Variant(false));152153 perty("DisplayAlerts", new Variant(false));154155 (activexComponent, "FileOpen",156157 newObject[] { mpxFilePath });158159 (160161 activexComponent,162163 "FileSaveAs",164165 new Object[] { ing(0,166167 () - 4)168169 + ".mpp"});170171 i = 1;172173 retBooli = true;174175 } catch(Exception localError3) {176177 tackTrace();178179 } finally{180181 try{182183 if (activexComponent != null)184185 (activexComponent, "Quit",186187 new Object[] { f(0) });188189 } catch(Exception localException7) {190191 }192193 try{194195 if (j != 0)196197 e();198199 } catch(Error localError6) {200

201

202

203 }204

205 }206

207 returnretBooli;208

209 }210

211

212

213 public static booleanisNullOrEmpty(String myStr) {214

215 return (myStr == null) || ("".equals(()));216

217 }218

219

220

221 }222

223

1 /*

2

3 测试工具类的 先读取一个mpp文件为ProjectFile 对象,在写成 一个mpx文件4

5 这后调用工具类 把mpx转换成mpp ,这个依赖与系统已安装了project 20036

7 */

8

9 public classMpx2MppUtilTest {10

11

12

13 /**

14

15 *@paramargs16

17 *@throwsException18

19 */

20

21 public static void main(String[] args) throwsException {22

23 String projPath = "D:/works/dp_springworkspace20130220/mpx2mpp";2425 String m_basedir = projPath + "/src/test/resources/test/mpp";2627 File in = new File(m_basedir + "/");2829 ProjectFile mpx = newMPPReader().read(in);3031 String pathname = projPath + "/src/test/resources/test/";3233 File testmpx = newFile(pathname);3435 MPXWriter mpxwriter = newMPXWriter();3637 Locale locale =E;3839 ale(locale );4041 (mpx, testmpx);4243 boolean res =tMpxToMpp(pathname);4445 n(res);4647 }48495051 }5253