您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页jacob操作word excel

jacob操作word excel

来源:爱go旅游网
jacob操作word excel

项目开发过程中,需求涉及到了各种文档转换为HTML或者网页易显示格式,现在将实现方式整理如下: 一、了解Jacob

先了解一下概念,JACOB 就是 JAVA-COM Bridge的缩写,提供自动化的访问com的功能,也是通过JNI功能访问windows平台下的com组件或者win32系统库的。这是一个开始于1999年的开源项目的成果,有很多使用者对该项目进行了修改,做出了自己的贡献。

下载地址:

http://sourceforge.net/project/showfiles.php?group_id=1093&package_id=118368

二、Jacob安装

1、我们解开下载的jacob_1.9.zip,在文件夹中找到jacob.dll和jacob.jar两个文件

2、将压缩包解压后,Jacob.jar添加到Libraries中; 3、将Jacob.dll放至“WINDOWS\\SYSTEM32”下面。 需要注意的是:

【使用IDE启动Web服务器时,系统读取不到Jacob.dll,例如用MyEclipse启动Tomcat,就需要将dll文件copy到MyEclipse安装目录的“jre\\bin”下面。 一般系统没有加载到Jacob.dll文件时,报错信息为:

“java.lang.UnsatisfiedLinkError: no jacob in java.library.path”】 三、使用Jacob转换Word,Excel为HTML JAVA代码: Java代码

1. import java.io.BufferedReader; 2. import java.io.BufferedWriter; 3. import java.io.File;

4. import java.io.FileInputStream;

5. import java.io.FileNotFoundException; 6. import java.io.FileWriter; 7. import java.io.IOException;

8. import java.io.InputStreamReader; 9.

10.import com.jacob.activeX.ActiveXComponent; 11.import com.jacob.com.Dispatch; 12.import com.jacob.com.Variant;

13.

14.public class TransformFiletoHtml 15.{

16. int WORD_HTML = 8; 17. int WORD_TXT = 7; 18. int EXCEL_HTML = 44; 19. 20. /**

21. * WORD转HTML

22. * @param docfile WORD文件全路径

23. * @param htmlfile 转换后HTML存放路径 24. */

25. public void wordToHtml(String docfile, String htmlfile) 26. {

27. ActiveXComponent app = new ActiveXComponent(\"Word.Application\"); // 启动word 28. try 29. {

30. app.setProperty(\"Visible\31. Dispatch docs = app.getProperty(\"Documents\").toDispatch();

32. Dispatch doc = Dispatch.invoke(docs,\"Open\.Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();

33. Dispatch.invoke(doc, \"SaveAs\ Object[] {htmlfile, new Variant(WORD_HTML) }, new int[1]); 34. Variant f = new Variant(false); 35. Dispatch.call(doc, \"Close\36. }

37. catch (Exception e) 38. {

39. e.printStackTrace(); 40. }

41. finally 42. {

43. app.invoke(\"Quit\44. } 45. } 46. 47. /**

48. * EXCEL转HTML

49. * @param xlsfile EXCEL文件全路径

50. * @param htmlfile 转换后HTML存放路径 51. */

52. public void excelToHtml(String xlsfile, String htmlfile) 53. {

. ActiveXComponent app = new ActiveXComponent(\"Excel.Application\"); // 启动excel 55. try 56. {

57. app.setProperty(\"Visible\58. Dispatch excels = app.getProperty(\"Workbooks\").toDispatch();

59. Dispatch excel = Dispatch.invoke(excels,\"Open\atch.Method,new Object[] { xlsfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();

60. Dispatch.invoke(excel, \"SaveAs\ew Object[] {htmlfile, new Variant(EXCEL_HTML) }, new int[1]);

61. Variant f = new Variant(false); 62. Dispatch.call(excel, \"Close\63. }

. catch (Exception e) 65. {

66. e.printStackTrace(); 67. }

68. finally 69. {

70. app.invoke(\"Quit\71. } 72. } 73. 74. /**

75. * /删除指定文件夹

76. * @param folderPath 文件夹全路径

77. * @param htmlfile 转换后HTML存放路径 78. */

79. public void delFolder(String folderPath) 80. { 81. try 82. {

83. delAllFile(folderPath); //删除完里面所有内容 84. String filePath = folderPath; 85. filePath = filePath.toString();

86. java.io.File myFilePath = new java.io.File(filePath);

87. myFilePath.delete(); //删除空文件夹

88. } catch (Exception e) {e.printStackTrace();}

. } 90. 91. /**

92. * /删除指定文件夹下所有文件 93. * @param path 文件全路径 94. */

95. public boolean delAllFile(String path) 96. {

97. boolean flag = false;

98. File file = new File(path); 99. if (!file.exists()) 100. {

101. return flag; 102. }

103. if (!file.isDirectory()) 104. {

105. return flag; 106. }

107. String[] tempList = file.list(); 108. File temp = null;

109. for (int i = 0; i < tempList.length; i++) 110. {

111. if (path.endsWith(File.separator)) 112. {

113. temp = new File(path + tempList[i]); 114. } 115. else 116. {

117. temp = new File(path + File.separator + tempList[i]);

118. }

119. if (temp.isFile()) 120. {

121. temp.delete(); 122. }

123. if (temp.isDirectory()) 124. {

125. delAllFile(path + \"/\" + tempList[i]);//先删除文件夹里面的文件

126. delFolder(path + \"/\" + tempList[i]);//再删除空文件夹

127. flag = true; 128. } 129. }

130. 131. 132. return flag; } }

调用JAVA代码: Java代码

1. public class Test1 {

2. public static void main(String[] args) { 3. // TODO Auto-generated method stub

4. TransformFiletoHtml trans = new TransformFiletoHtml();

5. trans.wordToHtml(\"D:\\\\sinye.doc\6. } 7. 8. }

只写了一个测试word转html的,excel转html的同理,在TransformFiletoHtml类中,写了两个方法,一个是删除文件夹的方法(delFolder()),一个是删除文件夹下所有文件的方法(delAllFile())。写这个的目的是出于:在word或者excel转html的过程中,除了生成制定的html页面外,jacob组件会生成一些转换html页面时相关的其它页面,但是这些相关的其它页面不是我们所需要的,因此想把它删除,其实不删除也可以,只是看着不爽,在测试中,发现,word有时不会生成这样的文件,而且即使生成了,也能删除掉,但是excel生成的文件却不能删除,还望高手们给予解答。

另外,在你将excel转换html时,如果你的代码没问题,转换时,老提示什么存在用户区域的安全设置这什么的,会让你选择继续转换,还是取消。这是因为你转换的那个excel中写了保护,解决办法是在excel的工具->保护->允许用户编辑区域,删除里面的所有保护。

很好啊。学习了一下,对了,楼主可以问你一下如果我要查找word的内容,如

范、刹、镶、裹、泻、镀、润、杰、这些文字在word中找出来,再标注或是加一个下划线,应该怎么做了

只是给你提供一种思路,我以前的项目中也有类似的要求。在你将word转成html时,刚转成的html作为临时文件,然后通过java的文件读写流,将其中的内容置换成你要的内容,然后再保存为新的文件,删除那个旧的临时文件,比如转换出来的html里想将\"我爱JAVA\"换成\"我爱中国\",或者将里的内容换成不换行。你的那个要求以此类推就可以了。详见如下的JAVA代码:注意红色部分,就是你要改动的地方 Java代码

1. /**

2. *

3. * @Function //CreateFormallyHtml

4. * @Description //读取临时生成的网页文件,修改其中的内容后生成正式的网页文件,并删除临时网页文件

5. * @Input //临时网页文件全路径,新网页文件全路径 6. * @Return //布尔型,成功则返回true,否则返回false 7. */

8. public boolean CreateWordHtml(String strFile,String strNewHtml) 9. {

10. Boolean blStatus=false; 11.

12. String strInfo=null; 13. //读取网页文件 14. try 15. {

16. FileInputStream fis=new FileInputStream(strFile);

17. InputStreamReader isr=new InputStreamReader(fis);

18. BufferedReader buf=new BufferedReader(isr); 19. int i=0; 20. String c; 21. try 22. {

23. while((c=buf.readLine())!=null) 24. {

25. if(i==0){strInfo=c;}

26. else{strInfo=strInfo+\"\\n\"+c;} 27. i++;

28. }

29. buf.close(); 30. isr.close(); 31. fis.close(); 32. }

33. catch (IOException e){e.printStackTrace();} 34. }

35. catch (FileNotFoundException e) {e.printStackTrace();}

36.

37. //处理网页内容,并重新生成新的网页文件 38. try 39. {

40. File write = new File(strNewHtml);

41. BufferedWriter bw = new BufferedWriter(new FileWriter(write));

42. [color=red]strInfo=strInfo.replace(\"我爱JAVA\我爱中国\");

43. strInfo=strInfo.replace(\"44.

45. bw.write(strInfo); 46. bw.close(); 47.

48. blStatus=true; 49. }

50. catch (IOException ee) {System.out.println(ee.getMessage());} 51.

52. //删除临时生成的网页文件及其附属文件夹 53. try

. {

55. File fl = new File(strFile);

56. if(fl.exists() && fl.isFile()){fl.delete();}//删除临时网页

57. int intLen=strFile.lastIndexOf(\".\");

58. String strFileFolder=strFile.substring(0,intLen+1)+\"files\";

59. delFolder(strFileFolder);//删除临时网页附属的文件夹

60. }

61. catch (Exception ee) {System.out.println(ee.getMessage());}

62. 63.

. return blStatus; 65. }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务