parent
d6afb557c4
commit
b938d72d0b
@ -0,0 +1,29 @@
|
||||
package com.dsideal.base.Test;
|
||||
|
||||
import org.apache.poi.xwpf.usermodel.*;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun;
|
||||
|
||||
public class MyXWPFSDT extends XWPFAbstractSDT implements IBodyElement, IRunBody, ISDTContents, IRunElement {
|
||||
|
||||
private final ISDTContent content;
|
||||
private CTSdtBlock block;
|
||||
|
||||
public MyXWPFSDT(CTSdtRun sdtRun, IBody part) {
|
||||
super(sdtRun.getSdtPr(), part);
|
||||
this.content = new XWPFSDTContent(sdtRun.getSdtContent(), part, this);
|
||||
}
|
||||
|
||||
public MyXWPFSDT(CTSdtBlock block, IBody part) {
|
||||
super(block.getSdtPr(), part);
|
||||
this.content = new XWPFSDTContent(block.getSdtContent(), part, this);
|
||||
this.block = block;
|
||||
}
|
||||
public ISDTContent getContent() {
|
||||
return this.content;
|
||||
}
|
||||
public CTSdtBlock getBlock(){
|
||||
return this.block;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.dsideal.base.Test;
|
||||
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
public class ReadWordDirectory {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
String filePath = "c:/双柏县人口变化及其对教育的影响.docx";
|
||||
InputStream is = new FileInputStream(filePath);
|
||||
XWPFDocument document = new XWPFDocument(is);
|
||||
|
||||
// 获取文档中的所有段落
|
||||
List<XWPFParagraph> paragraphs = document.getParagraphs();
|
||||
for (XWPFParagraph paragraph : paragraphs) {
|
||||
// 检查段落样式是否为标题样式,根据需要调整样式级别
|
||||
if (paragraph.getStyle() != null && paragraph.getStyle().startsWith("Heading")) {
|
||||
// 输出标题文本
|
||||
System.out.println(paragraph.getText());
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭文档和输入流
|
||||
document.close();
|
||||
is.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package com.dsideal.base.Test;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.SAXReader;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import static com.dsideal.base.Tools.FillData.DataEaseKit.DsKit.DocxUnzipDirectory;
|
||||
|
||||
public class ReadWordTOC {
|
||||
public static void main(String[] args) throws IOException, DocumentException {
|
||||
String wordPath = "c:/双柏县人口变化及其对教育的影响.docx";
|
||||
//解压缩
|
||||
if (new File(DocxUnzipDirectory).exists()) {
|
||||
FileUtils.deleteDirectory(new File(DocxUnzipDirectory));
|
||||
}
|
||||
File file = new File(wordPath);//取得word文件
|
||||
FileInputStream inputStream = new FileInputStream(file);
|
||||
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
|
||||
ZipEntry entry;
|
||||
byte[] ch = new byte[256];
|
||||
while ((entry = zipInputStream.getNextEntry()) != null) {
|
||||
File zFile = new File(DocxUnzipDirectory + entry.getName());
|
||||
if (entry.isDirectory()) {
|
||||
if (!zFile.exists()) {
|
||||
zFile.mkdirs();
|
||||
}
|
||||
zipInputStream.closeEntry();
|
||||
} else {
|
||||
File fpath = new File(zFile.getParent());
|
||||
if (!fpath.exists()) {
|
||||
fpath.mkdirs();
|
||||
}
|
||||
FileOutputStream outputStream = new FileOutputStream(zFile);
|
||||
int i;
|
||||
while ((i = zipInputStream.read(ch)) != -1) {
|
||||
outputStream.write(ch, 0, i);
|
||||
}
|
||||
zipInputStream.closeEntry();
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
inputStream.close();
|
||||
|
||||
//读入XML
|
||||
String xmlPath = DocxUnzipDirectory + "word\\document.xml";
|
||||
///w:document/w:body/w:p/w:r/w:t
|
||||
//System.out.println(FileUtil.readUtf8String(xmlPath).contains("加强"));
|
||||
|
||||
SAXReader reader = new SAXReader(); // 创建 SAXReader 对象,读取 XML 文件
|
||||
Document document = reader.read(new File(xmlPath));
|
||||
Element root = document.getRootElement();// 获取根元素
|
||||
List<Element> children = root.element("body").elements("p");//工作区
|
||||
|
||||
for (Element child : children) {
|
||||
if (child.getName().equals("p")) {
|
||||
List<Element> pChildren = child.elements();
|
||||
boolean isBookmark = false;
|
||||
String content = "";
|
||||
for (Element pChild : pChildren) {
|
||||
if (pChild.getName().equals("bookmarkStart")) {
|
||||
isBookmark = true;
|
||||
}
|
||||
if (isBookmark && !pChild.getName().equals("bookmarkStart") && !pChild.getName().equals("bookmarkEnd")) {
|
||||
if (pChild.getName().equals("r")) {
|
||||
for (Element t : pChild.elements("t")) {
|
||||
content = content + t.getText();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pChild.getName().equals("bookmarkEnd")) {
|
||||
isBookmark = false;
|
||||
System.out.println(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue