当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

[简单]poi读取word 2007简单文本框值

发表于: 2015-02-28   作者:53873039oycg   来源:转载   浏览:
摘要:         import java.io.FileOutputStream; import java.util.Formatter; import java.util.List; import org.apache.poi.POIXMLDocument; import org.apache.poi.xwpf.u

       

import java.io.FileOutputStream;
import java.util.Formatter;
import java.util.List;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;

public class POI_读取指定文本框值_S3_Test {
	public static void main(String[] args) throws Exception {
		POI_读取指定文本框值_S3_Test t = new POI_读取指定文本框值_S3_Test();
		String filePath="f:/saveFile/temp/文本框0003.docx";
		t.readTextBoxContentUseXPath(filePath);
		t.readTextBoxContentUseCursor(filePath);
	}

	public void readTextBoxContentUseXPath(String filePath) throws Exception {
		XWPFDocument xdoc = openDocument(filePath);
		List<XWPFParagraph> paragraphList = xdoc.getParagraphs();
		// 取第一段
		XWPFParagraph paragrap = paragraphList.get(0);
		// 文本框位于第一个<w:r></w:r>内
		XmlObject object = paragrap.getCTP().getRArray(0);
		//System.out.println(object);
		XmlCursor selectPathCursor = getXmlObjectByXPath(
				object,
				"declare namespace ve='http://schemas.openxmlformats.org/markup-compatibility/2006'; declare namespace o='urn:schemas-microsoft-com:office:office'; declare namespace r='http://schemas.openxmlformats.org/officeDocument/2006/relationships'; declare namespace m='http://schemas.openxmlformats.org/officeDocument/2006/math'; declare namespace v='urn:schemas-microsoft-com:vml'; declare namespace wp='http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'; declare namespace w10='urn:schemas-microsoft-com:office:word'; declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'; declare namespace wne='http://schemas.microsoft.com/office/word/2006/wordml';",
				"$this/w:pict/v:shape/v:textbox/w:txbxContent/w:p");
		selectPathCursor.push();//保存当前位置
		selectPathCursor
				.selectPath("declare namespace ve='http://schemas.openxmlformats.org/markup-compatibility/2006'; declare namespace o='urn:schemas-microsoft-com:office:office'; declare namespace r='http://schemas.openxmlformats.org/officeDocument/2006/relationships'; declare namespace m='http://schemas.openxmlformats.org/officeDocument/2006/math'; declare namespace v='urn:schemas-microsoft-com:vml'; declare namespace wp='http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'; declare namespace w10='urn:schemas-microsoft-com:office:word'; declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'; declare namespace wne='http://schemas.microsoft.com/office/word/2006/wordml';"
						+ "$this/w:r/w:t");
		while (selectPathCursor.toNextSelection()) {
			System.out.println("文本框原来内容=" + selectPathCursor.getTextValue());
			selectPathCursor.setTextValue("修改");
		}
		selectPathCursor.pop();//恢复上次位置
		selectPathCursor.toParent();//w:txbxContent
		selectPathCursor.toChild(1);//w:p[1]
		selectPathCursor
				.selectPath("declare namespace ve='http://schemas.openxmlformats.org/markup-compatibility/2006'; declare namespace o='urn:schemas-microsoft-com:office:office'; declare namespace r='http://schemas.openxmlformats.org/officeDocument/2006/relationships'; declare namespace m='http://schemas.openxmlformats.org/officeDocument/2006/math'; declare namespace v='urn:schemas-microsoft-com:vml'; declare namespace wp='http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'; declare namespace w10='urn:schemas-microsoft-com:office:word'; declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'; declare namespace wne='http://schemas.microsoft.com/office/word/2006/wordml';"
						+ "$this/w:r/w:t");
		while (selectPathCursor.toNextSelection()) {
			System.out.println("文本框原来内容2=" + selectPathCursor.getTextValue());
			selectPathCursor.setTextValue("修_改");
		}
		selectPathCursor.dispose();
		saveDocument(xdoc, "f:/saveFile/temp/sys_" + System.currentTimeMillis()
				+ ".docx");
	}

	public XmlCursor getXmlObjectByXPath(XmlObject xml, String nameSpace,
			String xpath) {
		// Create a temporary cursor for the XPath passed in
		XmlCursor xpathCursor = xml.newCursor();
		// Create a formatter to format the XPath
		StringBuilder builder = new StringBuilder();
		Formatter formatter = new Formatter(builder);
		formatter.format("%s %s", nameSpace, xpath);
		// Select the XPath
		xpathCursor.selectPath(formatter.toString());
		xpathCursor.toNextSelection();
		formatter.close();
		return xpathCursor;
	}

	public void readTextBoxContentUseCursor(String filePath) throws Exception {
		XWPFDocument xdoc = openDocument(filePath);
		List<XWPFParagraph> paragraphList = xdoc.getParagraphs();
		// 取第一段
		XWPFParagraph paragrap = paragraphList.get(0);
		// 文本框位于第一个<w:r></w:r>内
		XmlObject object = paragrap.getCTP().getRArray(0);
		//参考https://www.ibm.com/developerworks/cn/xml/x-beans1/  高级特性部分
		//System.out.println(object);
		XmlCursor cursor = object.newCursor();
		cursor.toChild(1);// <xml-fragment> --> <w:pict>
		cursor.toChild(1);// <w:pict> --> <v:shape>
		cursor.toChild(0);// <v:shape> --> <v:textbox>
		cursor.toChild(0);// <v:textbox> --><w:txbxContent>

		cursor.toChild(0);// <w:txbxContent> --> <w:p>
		cursor.toChild(1);// <w:p> --> <w:r>
		cursor.toChild(1);// <w:r> --> <w:t>
		System.out.println("文本框原来值:"+cursor.getTextValue());
		cursor.setTextValue("修改后");

		// 回到<w:txbxContent>位置
		cursor.toParent();// <w:t>--><w:r>
		cursor.toParent();// <w:r>--><w:p>
		cursor.toParent();// <w:p>--><w:txbxContent>
		cursor.toChild(1);// <w:txbxContent>--><w:p>
		cursor.toChild(1);// <w:p> --> <w:r>
		cursor.toChild(1);// <w:r> --> <w:t>
		System.out.println("文本框原来值:"+cursor.getTextValue());
		cursor.setTextValue("修改后");
		// 回到<w:p>位置
		cursor.toParent();// <w:t>--><w:r>
		cursor.toParent();// <w:r>--><w:p>
		cursor.toChild(2);// <w:p>--><w:r>
		cursor.toChild(1);// <w:r> --> <w:t>
		System.out.println("文本框原来值:"+cursor.getTextValue());
		cursor.setTextValue("4");
		cursor.dispose();
		saveDocument(xdoc, "f:/saveFile/temp/sys_" + System.currentTimeMillis()
				+ ".docx");
	}

	public void saveDocument(XWPFDocument document, String savePath)
			throws Exception {
		FileOutputStream fos = new FileOutputStream(savePath);
		document.write(fos);
		fos.close();
	}

	public XWPFDocument openDocument(String filePath) throws Exception {
		XWPFDocument xdoc = new XWPFDocument(
				POIXMLDocument.openPackage(filePath));
		return xdoc;
	}

}

 

    全文完.

[简单]poi读取word 2007简单文本框值

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.Reader; import
word 2007 Wingdings字符操作步骤如下图: 代码很简单: import java.io.FileOutputStream; import ja
word 2007内容控件位置: 代码如下: import java.io.File; import java.io.StringWriter; import jav
应博友要求写下这个例子,office word 2007测试通过,见代码: import java.io.FileOutputStream; im
直接上代码: import java.io.FileOutputStream; import java.math.BigInteger; import java.util.Ar
直接上代码: import java.io.FileOutputStream; import java.math.BigInteger; import java.util.R
使用已有的word 2007模版导出,常用的方法一般以下几种: (一)解压word模版后替换变量再压缩为word导
本文非原创,只是整理了下代码,原代码出自:http://blog.chiefleo.me/archives/429.原文如下: 普通
首先poi中XWPFTable.addNewRowBetween(start, end);是不能实现插入行功能的,poi3.11版本未实现该功
以前使用poi操作word 2007时候会遇到设置页面大小等问题,当时发现1cm≈567,为什么要这样一直不清楚
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号