`
53873039oycg
  • 浏览: 837087 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

[简单]POI读取word 2007内容控件

    博客分类:
  • poi
 
阅读更多

      见代码 :

     

import java.util.Iterator;
import java.util.List;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.ISDTContent;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFSDT;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLock;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;

//只处理了文本(不包括公式) 只处理了正文
public class POI_07_读取内容控件_S3_Test {
	public static void main(String[] args) throws Exception {
		POI_07_读取内容控件_S3_Test t = new POI_07_读取内容控件_S3_Test();
		t.printAllSdtContent("f:/saveFile/temp/内容控件_纯文本 - 副本.docx");
	}

	public void printAllSdtContent(String filePath) throws Exception {
		XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(filePath));
		// 打印独立于段落和表格外的内容控件
		// 关于XWPFSDT可以参考http://svn.apache.org/repos/asf/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java
		Iterator<IBodyElement> itrator = xdoc.getBodyElementsIterator();
		while (itrator.hasNext()) {
			IBodyElement element = itrator.next();
			if (element instanceof XWPFSDT) {
				XWPFSDT sdt = (XWPFSDT) element;
				printXWPFSDTContent(sdt);
			}
		}
		// 打印段落内内容控件
		List<XWPFParagraph> paraList = xdoc.getParagraphs();
		printParaListContent(paraList);
		// 打印表格内内容控件
		List<XWPFTable> tblList = xdoc.getTables();
		for (int i = 0, len = tblList.size(); i < 0; i++) {
			XWPFTable table = tblList.get(i);
			for (int j = 0, rcount = table.getNumberOfRows(); j < rcount; j++) {
				XWPFTableRow row = table.getRow(j);
				List<CTSdtCell> tblSdtList = row.getCtRow().getSdtList();
				if (tblSdtList != null && tblSdtList.size() > 0) {
					for (CTSdtCell ctSdtCell : tblSdtList) {
						CTSdtPr sdtPr = ctSdtCell.getSdtPr();
						printSdtPrContent(sdtPr);
						CTSdtContentCell sdtContent = ctSdtCell.getSdtContent();
						printSdtContentCell(sdtContent);
					}
				}
			}
		}
	}

	//无法获取更多的内容(如文本类型,是否可编辑)
	public void printXWPFSDTContent(XWPFSDT sdt) {
		StringBuffer sb = new StringBuffer();
		sb.append(" 标记:").append(sdt.getTag());
		sb.append(" 标题:").append(sdt.getTitle());
		ISDTContent content = sdt.getContent();
		sb.append(" 内容:").append(content.getText());
		System.out.println(sb.toString());
	}

	public void printParaListContent(List<XWPFParagraph> paraList) {
		if (paraList == null || paraList.size() == 0) {
			return;
		}
		for (XWPFParagraph para : paraList) {
			List<CTSdtRun> sdtList = para.getCTP().getSdtList();
			if (sdtList == null || sdtList.size() == 0) {
				continue;
			}
			for (CTSdtRun sdtRun : sdtList) {
				CTSdtPr sdtPr = sdtRun.getSdtPr();
				printSdtPrContent(sdtPr);
				CTSdtContentRun sdtContent = sdtRun.getSdtContent();
				printSdtContent(sdtContent);
			}
		}
	}

	// 解析样式,区分纯文本和格式文本
	public void printSdtPrContent(CTSdtPr sdtPr) {
		if (sdtPr == null) {
			return;
		}
		StringBuffer sb = new StringBuffer();
		List<CTSdtText> textList = sdtPr.getTextList();
		if (textList != null && textList.size() > 0) {
			sb.append(" 内容控件类型:").append("纯文本");
			CTSdtText sdtText = textList.get(0);
			if (sdtText.getMultiLine() != null) {
				int mulType = sdtText.getMultiLine().intValue();
				if (mulType == 1 || mulType == 3 || mulType == 6) {
					sb.append(" 是否允许回车:").append("是");
				}
			}
		} else {
			sb.append(" 内容控件类型:").append("格式文本");
		}
		List<CTDecimalNumber> idList = sdtPr.getIdList();
		if (idList != null && idList.size() > 0) {
			sb.append(" ID:").append(idList.get(0).getVal());
		}
		List<CTString> aliasList = sdtPr.getAliasList();
		if (aliasList != null && aliasList.size() > 0) {
			sb.append(" 标题:").append(aliasList.get(0).getVal());
		}
		List<CTString> tagList = sdtPr.getTagList();
		if (tagList != null && tagList.size() > 0) {
			sb.append(" 标记:").append(tagList.get(0).getVal());
		}
		List<CTLock> lockList = sdtPr.getLockList();
		if (lockList != null && lockList.size() > 0) {
			CTLock ctLock = lockList.get(0);
			int lockType = ctLock.getVal().intValue();
			switch (lockType) {
			case 1:
				sb.append(" 锁定方式:").append("无法删除内容控件");
				break;
			case 2:
				sb.append(" 锁定方式:").append("无法编辑内容");
				break;
			case 4:
				sb.append(" 锁定方式:").append("无法删除内容控件,无法编辑内容");
				break;
			default:
				sb.append(" 锁定方式:").append(ctLock.getVal());
				break;
			}
		}
		List<CTOnOff> tempList = sdtPr.getTemporaryList();
		if (tempList != null && tempList.size() > 0) {
			if (tempList.get(0).getVal() != null) {
				int isOn = tempList.get(0).getVal().intValue();
				if (isOn == 1 || isOn == 3 || isOn == 6) {
					sb.append(" 替换后是否删除内容控件:").append("是");
				}
			} else {
				sb.append(" 替换后是否删除内容控件:").append("是");
			}
		}
		List<CTRPr> rprList = sdtPr.getRPrList();
		if (rprList != null && rprList.size() > 0) {
			CTRPr rpr = rprList.get(0);
			CTString rprStyle = rpr.getRStyle();
			if (rprStyle != null) {
				sb.append(" 样式名称:").append(rprStyle.getVal());
			}
		}
		System.out.println(sb.toString());
	}

	// 段落内内容控件
	public void printSdtContent(CTSdtContentRun sdtContentRun) {
		if (sdtContentRun == null) {
			return;
		}
		StringBuffer sb = new StringBuffer();
		List<CTR> ctrList = sdtContentRun.getRList();
		sb.append(getCTRContent(ctrList));
		System.out.println("内容:" + sb.toString());
	}

	// 表格内内容控件
	public void printSdtContentCell(CTSdtContentCell sdtContentCell) {
		if (sdtContentCell == null) {
			return;
		}
		StringBuffer sb = new StringBuffer();
		List<CTTc> cttcList = sdtContentCell.getTcList();
		sb.append(getCTTcContent(cttcList));
		System.out.println("内容:" + sb.toString());
	}

	// 段落
	public String getCTRContent(List<CTR> ctrList) {
		StringBuffer sb = new StringBuffer();
		if (ctrList != null && ctrList.size() > 0) {
			for (CTR ctr : ctrList) {
				List<CTText> tList = ctr.getTList();
				if (tList != null && tList.size() > 0) {
					for (CTText ctText : tList) {
						sb.append(ctText.getStringValue());
					}
				}
			}
		}
		return sb.toString();
	}

	// 表格
	public String getCTTcContent(List<CTTc> cttcList) {
		StringBuffer sb = new StringBuffer();
		if (cttcList != null && cttcList.size() > 0) {
			for (CTTc cttc : cttcList) {
				List<CTP> pList = cttc.getPList();
				if (pList != null && pList.size() > 0) {
					for (CTP ctp : pList) {
						List<CTR> rList = ctp.getRList();
						sb.append(getCTRContent(rList));
					}
				}
			}
		}
		return sb.toString();
	}
}

 

     全文完。

分享到:
评论

相关推荐

    java poi操作word模版文件生成表单和修改

    1. **创建和读取Word文档** - 使用Apache POI的`XWPFDocument`类可以创建和加载.docx格式的Word文档。首先,我们需要创建一个`XWPFDocument`实例,然后通过`XWPFParagraph`和`XWPFRun`对象添加文本、样式等元素。 ...

    Vs 2005 + Com组件只读取Word文档中的纯文本内容源码

    本文将详细介绍如何在VS 2005中创建一个Windows应用程序,仅读取Word文档中的纯文本内容。 首先,确保你的系统上安装了Visual Studio 2005,并且安装了Microsoft Office,因为COM组件是基于Office的应用程序接口。...

    Android读取并显示word文件的代码例子

    在Android平台上,开发人员...通过以上步骤,你可以在Android应用中实现读取和显示Word文档的功能,覆盖了从doc到docx的格式,并能正确处理图文混排的内容。实际开发中,可以根据需求进行调整,优化性能和用户体验。

    poi读写office文件样例程序

    4. **读取Word文件** 对于Word文档,POI提供了HWPFDocument类来读取旧版本的.doc文件,而XWPFDocument则用于处理新版本的.docx文件。开发者可以读取段落、表格、图片等元素,并进行操作。 5. **写入Word文件** ...

    Java Word控件-Free Spire.Doc for Java_2.0.0

    Java Word控件-Free Spire.Doc for Java_2.0.0是一个强大的库,专为Java开发者设计,用于处理Microsoft Word文档。与Apache POI相比,它提供了更丰富的功能和更简便的API,使得在Java环境中操作Word文档变得更加高效...

    javaweb预览word与pdf文件控件为免费控件

    2. **文件读取**:Java代码需要能够读取存储的文件内容。对于PDF,可以使用如Apache PDFBox、iText等库;对于Word,可以使用Apache POI或JODConverter等库。 3. **转换为HTML**:由于浏览器无法直接显示二进制的...

    NPOI读取excel控件(.net)

    NPOI,顾名思义,就是POI的.NET...NPOI是构建在POI 3.x版本之上的,本月发布的NPOI 1.2是对应于POI 3.2 final的,所以它支持Excel文件读写,但由于人手和精力原因,还没有实现读写Word, PowerPoint, Visio的文件格式。

    android应用源码tree目录和读取word文档整合.zip源码资源下载

    2. 读取Word文档:使用POI提供的API打开和解析Word文档,例如`XWPFDocument`类用于处理.docx文件。 3. 数据提取:获取文档中的文本、段落、表格等元素,可能需要遍历文档结构并提取所需信息。 4. 显示内容:将提取的...

    《apache poi4.0》【最新版poi,snapshot版本,新增多种操作office文件的功能】.docx

    POI 提供了对这些文件格式读取和写入的支持,并且能够以纯 Java 的方式操作这些文件而无需依赖 Microsoft Office 套件。 #### 二、Apache POI 4.0 新特性概览 Apache POI 4.0 版本作为该系列的一个重要里程碑,...

    安卓Excelwordppt文档读写相关-Android读取并显示word文件的代码例子.rar

    以下是一个简单的代码片段,展示了如何使用Spacy4j库读取Word文档: ```java import com.spire.doc.*; public class WordReader { public static void main(String[] args) { Document document = new Document...

    Java调用ocx控件以及dll和word文档

    总结一下,Java调用OCX控件、DLL和处理Word文档涉及的关键技术有:Java Native Interface(JNI)用于与本地代码交互,Apache POI库则提供了处理Word文档的强大工具。在实际开发中,理解这些技术的原理和应用,能够...

    android中读取本地文件demo

    5. **文件内容预览**:对于Word文件,可能需要使用第三方库,如Apache POI,来解析内容。PDF文件则通常需要像PDFBox或iText这样的库来读取。预览内容可能需要将文本提取出来,显示在ListView的item上。 6. **搜索...

    abc.zip_Java Word_SWT word_java 取 word_word

    6. 调用Apache POI或SWT提供的API读取Word文档。如果是旧版的Word文档(.doc),可以使用`org.eclipse.swt.custom.StyledText`结合`org.eclipse.swt.dnd.TextTransfer`进行文本抽取。 7. 处理读取到的文本数据,...

    第三方NPOI控件

    【第三方NPOI控件】是一个专为.NET Framework 4.0设计的高效、稳定且无需安装Microsoft Office Excel的库,允许开发者在程序中轻松创建、读取和修改Excel文档。NPOI的名字来源于.NET(.NET Port of POI),它是...

    MVC word转成pdf格式

    在这个例子中,可能有一个`WordToPdfController`,其中包含一个动作方法(Action),比如`GeneratePdf`,该方法读取Word文件,进行内容替换,然后生成PDF并返回给前端。视图(View)则负责展示生成的PDF,可能通过...

    支持java的office控件

    在IT行业中,尤其是在Java开发领域,有时候我们需要与Microsoft Office文档进行交互,比如读取、编辑或生成Word、Excel等文件。在这种情况下,"支持Java的Office控件"扮演着关键角色。这些控件允许Java应用程序无缝...

    QT将word嵌入到widget界面中

    1. 使用开源库,如Apache POI(Java)或python-docx(Python),读取Word文档并将其转换为HTML。 2. 利用Office的自动化接口(如COM接口),通过运行Word应用程序并利用其内部功能将文档另存为HTML。 这里以Python...

    将word导入RichTextBox

    在C#中,可以使用`System.IO`命名空间下的`File`类来读取Word文档。Word文档默认的扩展名是`.doc`或`.docx`,它们分别对应的是早期的二进制格式和现代的基于XML的格式。对于`.docx`文件,我们可以直接使用`OpenText...

Global site tag (gtag.js) - Google Analytics