POI解析word2007为html,编码异常,图片错误

qq_20533371 2016-09-18 07:02:55
今天做POI解析word2007为html的时候,虽然已经转换到了html了,但是存在一个问题,那就是转换之后,表格中的内容从ASCII编码变为了unicode编码,具体代码如下:
实现转换的代码:

public static String docx2html(File file, File outPutPath,String htmlFileName) {
InputStream in=null;
OutputStream out=null;
XWPFDocument document=null;
String outPutFile=outPutPath+File.separator+htmlFileName;
try{
// 1) 加载word文档生成 XWPFDocument对象
in = new FileInputStream(file);
document = new XWPFDocument(in);
// 2) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)
final File imagePath=new File(outPutPath+File.separator+"image");
String img=SysConfig.getStrValue("upload_file_pre")+File.separator+imagePath;
final File url=new File(img);
if (!imagePath.exists() || !imagePath.isDirectory()) {
imagePath.mkdirs();
}
XHTMLOptions options = XHTMLOptions.create().indent(4);
options.URIResolver(new FileURIResolver(url));
options.URIResolver(new BasicURIResolver(img));
options.setExtractor(new FileImageExtractor(imagePath));
options.setIgnoreStylesIfUnused(false);
options.setFragment(true);

// 3) 将 XWPFDocument转换成XHTML
out = new FileOutputStream(new File(outPutFile));
XHTMLConverter.getInstance().convert(document, out, options);
}catch(Exception e){
logger.error(e.getMessage(),e);
}finally{
close(out,document,in);
}
return outPutFile;
}


转换后的部分html代码:
<div style="width:595.0pt;margin-bottom:72.0pt;margin-top:72.0pt;margin-left:90.0pt;margin-right:90.0pt;">
<p style="text-align:left;">
<span style="font-family:'Times New Roman';color:#ff0000;">【题干】
</span>
<span style="font-family:'宋体';font-size:10.0pt;color:#000000;">ewqeqwe
</span>
<span style="font-family:'宋体';font-size:10.0pt;color:#000000;">导入的习题_你好
</span>
</p>
<p style="text-align:left;">
<span style="font-family:'Times New Roman';color:#ff0000;">【选项】
</span>
<span style="font-family:'宋体';font-size:10.0pt;color:#000000;">【无】
</span>
</p>
<p style="text-align:left;">
<span style="font-family:'Times New Roman';color:#ff0000;">【答案】
</span>
<span style="font-family:'宋体';font-size:10.0pt;color:#000000;">1111
</span>
</p>
<p style="text-align:left;">
<span style="font-family:'Times New Roman';color:#ff0000;">【解析】
</span>
<span style="font-family:'宋体';font-size:10.0pt;color:#000000;">解析法大法师法
</span>
</p>
<p style="text-align:left;">
<span style="font-family:'Times New Roman';color:#ff0000;">【结束】
</span>
</p>
</div>




其中的 【 就是我需要的内容,但是不知道怎么的就被转换为了这种编码的内容了,浏览器能够正确的解析这种编码的内容。
跪求大神告知如何修改!
...全文
219 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧