07版的出错。
如果是导入07版本的Word文件,导入过程中,会直接进入exception 而不是OfficeXmlFileException异常,怎么办啊。
下面是我的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | package com.huanyu.service; import com.huanyu.dto.ImportWordParamDto; import com.huanyu.dto.ImportWordResultDto; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import java.io.IOException; import java.util.List; /** * 实现导入Word文档的业务层 * * @author Huanyu * @date 2018/4/10 */ public class WordService { public ImportWordResultDto imp(ImportWordParamDto paramDto) { ImportWordResultDto result = new ImportWordResultDto(); result.setTitle(paramDto.getTitle()); HWPFDocument document = null ; try { document = new HWPFDocument(paramDto.getWord().getInputStream()); // System.out.println(document.getDocumentText()); // 将Word文档中的换行,转换为HTML中的换行, result.setContent(document.getDocumentText().replace( "\r" , "<br/>" )); } catch (OfficeXmlFileException oe) { System.out.println( "这可能是一个07版的Word" ); // 07版本 } catch (Exception e) { result.setMsg( "这可能不是一个Word文档" ); // result = impForHigh(paramDto); // return result; } finally { if (document != null ) { try { document.close(); return result; } catch (IOException e) { e.printStackTrace(); } } } XWPFDocument documentx = null ; try { documentx = new XWPFDocument(paramDto.getWord().getInputStream()); List<XWPFParagraph> paragraphs = documentx.getParagraphs(); StringBuilder content = new StringBuilder(); for ( int i = 0 ; i < paragraphs.size(); i++) { if (i != 0 ) { // 添加段落换行符 content.append( "<br/>" ); } content.append(paragraphs.get(i).getText()); } result.setContent(content.toString()); } catch (IOException e) { e.printStackTrace(); } finally { if (documentx != null ) { try { documentx.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } private ImportWordResultDto impForHigh(ImportWordParamDto paramDto) { ImportWordResultDto result = new ImportWordResultDto(); result.setTitle(paramDto.getTitle()); XWPFDocument documentx = null ; try { documentx = new XWPFDocument(paramDto.getWord().getInputStream()); List<XWPFParagraph> paragraphs = documentx.getParagraphs(); StringBuilder content = new StringBuilder(); for ( int i = 0 ; i < paragraphs.size(); i++) { if (i != 0 ) { // 添加段落换行符 content.append( "<br/>" ); } content.append(paragraphs.get(i).getText()); } result.setContent(content.toString()); } catch (IOException e) { e.printStackTrace(); } finally { if (documentx != null ) { try { documentx.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } } |
22
收起
正在回答 回答被采纳积分+1
2回答
Special994
2018-04-11 13:00:24
我替换了你提供的这段代码,测试功能都能正常实现,可以上传07版的文档。可以找一下错误提示,异常信息。或者是不是在写完代码后没有Redeploy服务器而导致更新的代码功能没有实现。
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧