Java 读取word文件内容

public static String getDocx(String uri){
        //解析docx模板并获取document对象
        XWPFDocument document;
        //获取XWPFRun对象输出整个文本内容
        StringBuffer tempText = new StringBuffer();
        try {
            document = new XWPFDocument(POIXMLDocument.openPackage(uri));
        //获取整个文本对象
        List<XWPFParagraph> allParagraph = document.getParagraphs();
        for (XWPFParagraph xwpfParagraph : allParagraph) {
                List<XWPFRun> runList = xwpfParagraph.getRuns();
                for (XWPFRun xwpfRun : runList) {
                    tempText.append(xwpfRun.toString());
                }
        }
        Document doc = Jsoup.parse(tempText.toString());
		if (doc == null) {
			return null;
		}
		// 获取
		Elements matchElements = doc.select("evaluation");
		Iterator<Element> i = matchElements.iterator();
		//测评标题
		String title = "";
		//测评题目
		List<String> question_arry=new ArrayList<String>();
		//问题对应的选项
		List<String> optionContent_arry=new ArrayList<String>();
		//选项对应的权值
		List<String> option_scoreArry=new ArrayList<String>();

		while (i.hasNext()) {
			Element el = i.next();
			title = el.select("title").text();
			
			Elements tm = doc.select("tm");
			Iterator<Element> j = tm.iterator();
			while (j.hasNext()) {
				Element el_tm = j.next();
				question_arry.add(el_tm.select("tm1").text());
				//获取选项
				Elements tm_xx = el_tm.select("option");
				Iterator<Element> k = tm_xx.iterator();
				String option = "";
				String score = "";
				while (k.hasNext()) {
					Element el_xx = k.next();
					option = option+el_xx.text()+"#";
					score = score+el_xx.attr("score")+"#";
				}
				optionContent_arry.add(option.substring(0, option.length()-1));
				option_scoreArry.add(score.substring(0, score.length()-1));
			}
		}
		System.out.println("测评标题:"+title);
		System.out.println("测评标题:"+question_arry);
		System.out.println("测评选项:"+optionContent_arry);
		System.out.println("选项权值:"+option_scoreArry);
       /* //存放文档新地址
        String newPath="";
        //读取源文档内容到新文档
        File file = new File(newPath);
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdir();
            file.getParentFile().createNewFile();
        }
        FileOutputStream stream = new FileOutputStream(newPath);
        document.write(stream);//写入新文档
        stream.close();*/
        
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //文档内容
        return tempText.toString();
    }

下面附上我本地的word文件内容

<evaluation>
<title class="one">关于个人喜好的测试</title>
<tm>
<tm1>你多少岁啦</tm1>
<option score="15">10</option>
<option score="25">12</option>
<option score="35">14</option>
<option score="45">16</option>
</tm>
<tm>
<tm1>你喜欢什么颜色</tm1>
<option score="15">红色</option>
<option score="25">黑色</option>
<option score="35">蓝色</option>
<option score="45">白色</option>
</tm>
</evaluation>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码奴生来只知道前进~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值