POI读取word转换html

本文介绍了如何使用Apache POI库读取Word文档中的图片,并获取文字的各种样式,包括判断回车符、空格符、制表符以及样式对比等。代码示例详细展示了读取过程,并提供了图片读写、样式处理的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

apache POI读取word文档的文档比较少,所以只有自己慢慢的摸索,这篇文章也属于比较基础入门的,主要是针对读取word中的图片,以及文字的各种样式,如有不好的地方,请各位多多指教!
Java代码 复制代码 收藏代码
  1. /**
  2. *
  3. */
  4. package com.util;
  5. import java.io.BufferedWriter;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.OutputStream;
  12. import java.io.OutputStreamWriter;
  13. import org.apache.poi.hwpf.HWPFDocument;
  14. import org.apache.poi.hwpf.model.PicturesTable;
  15. import org.apache.poi.hwpf.usermodel.CharacterRun;
  16. import org.apache.poi.hwpf.usermodel.Picture;
  17. import org.apache.poi.hwpf.usermodel.Range;
  18. /**
  19. *
  20. * @author 张廷 下午10:36:40
  21. *
  22. */
  23. public class WordToHtml {
  24. /**
  25. * 回车符ASCII码
  26. */
  27. private static final short ENTER_ASCII = 13;
  28. /**
  29. * 空格符ASCII码
  30. */
  31. private static final short SPACE_ASCII = 32;
  32. /**
  33. * 水平制表符ASCII码
  34. */
  35. private static final short TABULATION_ASCII = 9;
  36. private String htmlText = "";
  37. /**
  38. * 读取每个文字样式
  39. *
  40. * @param fileName
  41. * @throws Exception
  42. */
  43. public void getWordAndStyle(String fileName) throws Exception {
  44. FileInputStream in = new FileInputStream(new File(fileName));
  45. HWPFDocument doc = new HWPFDocument(in);
  46. // 取得文档中字符的总数
  47. int length = doc.characterLength();
  48. // 创建图片容器
  49. PicturesTable pTable = doc.getPicturesTable();
  50. htmlText = "<html><head><title>" + doc.getSummaryInformation().getTitle() + "</title></head><body>";
  51. // 创建临时字符串,好加以判断一串字符是否存在相同格式
  52. String tempString = "";
  53. for (int i = 0; i < length - 1; i++) {
  54. // 整篇文章的字符通过一个个字符的来判断,range为得到文档的范围
  55. Range range = new Range(i, i + 1, doc);
  56. CharacterRun cr = range.getCharacterRun(0);
  57. if (pTable.hasPicture(cr)) {
  58. // 读写图片
  59. this.readPicture(pTable, cr);
  60. } else {
  61. Range range2 = new Range(i + 1, i + 2, doc);
  62. // 第二个字符
  63. CharacterRun cr2 = range2.getCharacterRun(0);
  64. // 当前字符
  65. char currentChar = cr.text().charAt(0);
  66. // 判断是否为回车符
  67. if (currentChar == ENTER_ASCII)
  68. tempString += "<br/>";
  69. // 判断是否为空格符
  70. else if (currentChar == SPACE_ASCII)
  71. tempString += "&nbsp;";
  72. // 判断是否为水平制表符
  73. else if (currentChar == TABULATION_ASCII)
  74. tempString += " &nbsp;&nbsp;&nbsp;";
  75. // 比较前后2个字符是否具有相同的格式
  76. boolean flag = compareCharStyle(cr, cr2);
  77. String fontStyle = "<span style='font-family:" + cr.getFontName() + ";font-size:" + cr.getFontSize() / 2 + "pt;";
  78. if (cr.isBold())
  79. fontStyle += "font-weight:bold;";
  80. if (cr.isItalic())
  81. fontStyle += "font-style:italic;";
  82. if (flag && i != length - 2)
  83. tempString += currentChar;
  84. else if (!flag) {
  85. htmlText += fontStyle + "'>" + tempString + currentChar + "</span>";
  86. tempString = "";
  87. } else
  88. htmlText += fontStyle + "'>" + tempString + currentChar + "</span>";
  89. }
  90. htmlText += "</body></html>";
  91. this.writeFile(htmlText);
  92. }
  93. /**
  94. * 读写文档中的图片
  95. *
  96. * @param pTable
  97. * @param cr
  98. * @throws Exception
  99. */
  100. private void readPicture(PicturesTable pTable, CharacterRun cr) throws Exception {
  101. // 提取图片
  102. Picture pic = pTable.extractPicture(cr, false);
  103. // 返回POI建议的图片文件名
  104. String afileName = pic.suggestFullFileName();
  105. OutputStream out = new FileOutputStream(new File("g:\\test" + File.separator + afileName));
  106. pic.writeImageContent(out);
  107. htmlText += "<img src='g:\\test\\" + afileName + "'/>";
  108. }
  109. private boolean compareCharStyle(CharacterRun cr1, CharacterRun cr2) {
  110. boolean flag = false;
  111. if (cr1.isBold() == cr2.isBold() && cr1.isItalic() == cr2.isItalic() && cr1.getFontName().equals(cr2.getFontName()) && cr1.getFontSize() == cr2.getFontSize()) {
  112. flag = true;
  113. }
  114. return flag;
  115. }
  116. /**
  117. * 写文件
  118. *
  119. * @param s
  120. */
  121. private void writeFile(String s) {
  122. FileOutputStream fos = null;
  123. BufferedWriter bw = null;
  124. try {
  125. File file = new File("g:\\abc.html");
  126. fos = new FileOutputStream(file);
  127. bw = new BufferedWriter(new OutputStreamWriter(fos));
  128. bw.write(s);
  129. } catch (FileNotFoundException fnfe) {
  130. fnfe.printStackTrace();
  131. } catch (IOException ioe) {
  132. ioe.printStackTrace();
  133. } finally {
  134. try {
  135. if (bw != null)
  136. bw.close();
  137. if (fos != null)
  138. fos.close();
  139. } catch (IOException ie) {
  140. }
  141. }
  142. }
  143. 转载出处http://z276356445t.iteye.com/blog/963950  目前poi不能用于android平台 我试验了很多次 都报异常错误
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值