POI读取word转换html

上传人:飞*** 文档编号:28652965 上传时间:2018-01-18 格式:DOC 页数:5 大小:51.50KB
返回 下载 相关 举报
POI读取word转换html_第1页
第1页 / 共5页
POI读取word转换html_第2页
第2页 / 共5页
POI读取word转换html_第3页
第3页 / 共5页
POI读取word转换html_第4页
第4页 / 共5页
POI读取word转换html_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《POI读取word转换html》由会员分享,可在线阅读,更多相关《POI读取word转换html(5页珍藏版)》请在金锄头文库上搜索。

1、POI 读取 word 转换 html文章分类: Java 编程apache POI 读取 word 文档的文档比较少 ,所以只有自己慢慢的摸索,这篇文章也属于比较基础入门的,主要是针对读取 word 中的图片,以及文字的各种样式, 如有不好的地方,请各位多多指教! Java 代码 1. /* 2. * 3. */ 4. package com.util; 5. 6. import java.io.BufferedWriter; 7. import java.io.File; 8. import java.io.FileInputStream; 9. import java.io.FileNo

2、tFoundException; 10. import java.io.FileOutputStream; 11. import java.io.IOException; 12. import java.io.OutputStream; 13. import java.io.OutputStreamWriter; 14. 15. import org.apache.poi.hwpf.HWPFDocument; 16. import org.apache.poi.hwpf.model.PicturesTable; 17. import org.apache.poi.hwpf.usermodel.

3、CharacterRun; 18. import org.apache.poi.hwpf.usermodel.Picture; 19. import org.apache.poi.hwpf.usermodel.Range; 20. 21. /* 22. * 23. * author 张廷 下午 10:36:40 24. * 25. */ 26. public class WordToHtml 27. 28. /* 29. * 回车符 ASCII 码 30. */ 31. private static final short ENTER_ASCII = 13; 32. 33. /* 34. *

4、空格符 ASCII 码 35. */ 36. private static final short SPACE_ASCII = 32; 37. 38. /* 39. * 水平制表符 ASCII 码 40. */ 41. private static final short TABULATION_ASCII = 9; 42. 43. private String htmlText = ; 44. 45. /* 46. * 读取每个文字样式 47. * 48. * param fileName 49. * throws Exception 50. */ 51. public void getWor

5、dAndStyle(String fileName) throws Exception 52. 53. FileInputStream in = new FileInputStream(new File(fileName); 54. 55. HWPFDocument doc = new HWPFDocument(in); 56. 57. / 取得文档中字符的总数 58. int length = doc.characterLength(); 59. 60. / 创建图片容器 61. PicturesTable pTable = doc.getPicturesTable(); 62. 63. h

6、tmlText = + doc.getSummaryInformation().getTitle() + ; 64. 65. / 创建临时字符串,好加以判断一串字符是否存在相同格式 66. 67. String tempString = ; 68. 69. for (int i = 0; i ; 93. / 判断是否为空格符 94. else if (currentChar = SPACE_ASCII) 95. tempString += 96. / 判断是否为水平制表符 97. else if (currentChar = TABULATION_ASCII) 98. tempString +

7、= 99. / 比较前后 2 个字符是否具有相同的格式 100. boolean flag = compareCharStyle(cr, cr2); 101. 102. String fontStyle = + tempString + currentChar+ ; 113. tempString = ; 114. else 115. htmlText += fontStyle + + tempString + currentChar+ ; 116. 117. htmlText += ; 118. 119. this.writeFile(htmlText); 120. 121. 122. /*

8、 123. * 读写文档中的图片 124. * 125. * param pTable 126. * param cr 127. * throws Exception 128. */ 129. private void readPicture(PicturesTable pTable, CharacterRun cr) throws Exception 130. / 提取图片 131. Picture pic = pTable.extractPicture(cr, false); 132. 133. / 返回 POI 建议的图片文件名 134. String afileName = pic.s

9、uggestFullFileName(); 135. 136. OutputStream out = new FileOutputStream(new File(g:test + File.separator + afileName); 137. 138. pic.writeImageContent(out); 139. 140. htmlText += ; 141. 142. 143. private boolean compareCharStyle(CharacterRun cr1, CharacterRun cr2) 144. boolean flag = false; 145. if

10、(cr1.isBold() = cr2.isBold() & cr1.isItalic() = cr2.isItalic() & cr1.getFontName().equals(cr2.getFontName() & cr1.getFontSize() = cr2.getFontSize() 146. flag = true; 147. 148. return flag; 149. 150. 151. /* 152. * 写文件 153. * 154. * param s 155. */ 156. private void writeFile(String s) 157. FileOutpu

11、tStream fos = null; 158. BufferedWriter bw = null; 159. try 160. File file = new File(g:abc.html); 161. fos = new FileOutputStream(file); 162. bw = new BufferedWriter(new OutputStreamWriter(fos); 163. bw.write(s); 164. catch (FileNotFoundException fnfe) 165. fnfe.printStackTrace(); 166. catch (IOException ioe) 167. ioe.printStackTrace(); 168. finally 169. try 170. if (bw != null) 171. bw.close(); 172. if (fos != null) 173. fos.close(); 174. catch (IOException ie) 175. 176. 177. 178.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号