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 替换word2007文本框的值POI,包含word表中的文本框的值,定义好word模板即可替换word中的值。
这是一个POI读取word 2003 和 word 2007的例子 是一个Eclipse工程 下载后直接导入工程 运行 src 目录下的 Test java 类即可 这个 rar 包中包含着 POI 读取word 2003 和 word 2007 所需要的 jar 包 也有需要读取的 ...
POI读取word文档的文字内容和图片内容
POI无法读取到word每一段所在的页码。我给出了代码和所有的解释,用POI读取word中的页码。也算是小方法了,基本很准确,为什么要加一个基本呢?因为,分页标志符号在第一行为空白行的时候没有分页符!!所以你的文档...
java 利用POI读取Word文件中的内容 java 利用POI读取Word文件中的内容
java.lang.NullPointerException at org.apache.poi.openxml4j.opc....在linux环境下poi读取word文件产生的NullPointerException,此问题找了两天最终解决。希望有同样问题的人能快速解决问题,所以上传此文档
POI读取word文件内容 word格式包括doc,docx两种。
apach poi 读取word 文档 jar 包。 博文链接:https://wxinpeng.iteye.com/blog/231881
POI解析word2007文本及图片(已测试).doc
利用POI读取excel写入到word 利用POI读取excel写入到word 利用POI读取excel写入到word,压缩包里包含了jar包
web小程序,通过apache poi读取项目中的word文档,并以流的形式将word内容返回给页面。
poi读取word代码,可以通过此代码实现对word中的文本、图片、表格的读取。
POI 读取 WORD EXCEL POWERPOINT 2003 2007 java 读取 WORD EXCEL POWERPOINT 2003 2007
java poi读取word文档,并获取里面的章节数、页码数和批注内容。源码带注释。
下载后,运行 可以 Test1中的main方法,注意里面的word模版地址要修改下。 test1.java主要是针对word的书签操作 MSWordTool.java主要是针对word的列表书签的操作,相对来说比较复杂,具体看个人需求。
poi读取word只读纯文本,通过poi的jar包的辅助,读取word中的纯文本。
POI读取word文件,存入数据库,代码很简单,主要用于读取word文件里面索要读取的内容,数据库那块一带而过!
安卓使用poi XWPFDocument读取及修改word文档;安卓使用poi XWPFDocument读取及修改word文档;