getParagraphs().getRuns()这个方法不可用,怎么解决
时间: 2024-02-03 19:11:47 浏览: 63
`getParagraphs()`返回一个 `List`,包含文档中所有段落。每个段落可以包含多个 `Run`,所以 `getRuns()`方法是应用于段落对象的,而不是应用于文档对象的。
你需要先获取一个段落对象,然后才能使用 `getRuns()` 方法。例如,如果你想获取文档中第一段的所有 `Run`,可以这样做:
```java
XWPFDocument doc = new XWPFDocument(new FileInputStream("document.docx"));
XWPFParagraph firstParagraph = doc.getParagraphs().get(0);
List<XWPFRun> runs = firstParagraph.getRuns();
```
注意,如果第一段中有一些文本样式不同的部分,那么每个不同的样式都会作为一个单独的 `Run` 返回。
相关问题
XWPFDocument .getParagraphs 里面的方法
`XWPFDocument.getParagraphs()` 方法是用于获取Word文档中所有段落的列表。它返回一个 `List<XWPFParagraph>` 类型的对象,其中每个元素代表一个段落。
以下是一个示例代码,演示如何使用 `XWPFDocument.getParagraphs()` 方法:
```java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
public class ReadWordDocx {
public static void main(String[] args) {
try {
InputStream inputStream = new FileInputStream("example.docx");
XWPFDocument document = new XWPFDocument(inputStream);
List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
System.out.println(paragraph.getText());
}
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在上面的示例中,我们首先创建了一个 `FileInputStream` 对象,以读取名为 `example.docx` 的Word文档。然后,我们使用 `XWPFDocument` 类的构造函数将输入流传递给它,以创建一个 `XWPFDocument` 对象。接下来,我们使用 `XWPFDocument.getParagraphs()` 方法获取文档中的所有段落,并使用一个循环遍历每个段落,并调用 `XWPFParagraph.getText()` 方法获取段落的文本内容并打印出来。最后,我们调用 `XWPFDocument.close()` 方法关闭文档。
为啥getOMathList方法不存在这个包里呢
很抱歉,我的回答有误。`getOMathList()`方法不是`XWPFRun`类的方法,因此在这个类中找不到该方法。实际上,需要使用`org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR`类来获取公式列表。
以下是更新后的示例代码:
```java
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOMath;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import java.io.File;
import java.io.FileInputStream;
public class ReadWordFormula {
public static void main(String[] args) throws Exception {
File file = new File("test.docx");
FileInputStream fis = new FileInputStream(file);
XWPFDocument document = new XWPFDocument(fis);
for (XWPFParagraph paragraph : document.getParagraphs()) {
for (CTR ctr : paragraph.getCTP().getRList().get(0).getRPr().getRStyleOrRPrChangeOrRPr()) {
CTOMath ctOMath = ctr.getOMath();
if (ctOMath != null && ctOMath.getOMathParaList().size() > 0) {
STOnOff stOnOff = ctOMath.getOMathParaList().get(0).getOMathList().get(0).getE().getOMath().getF().getEqArr().getType();
if (stOnOff != null && stOnOff.intValue() == STOnOff.INT_ON)
System.out.println("公式:" + paragraph.getText());
}
}
}
document.close();
fis.close();
}
}
```
在上面的代码中,我们使用`getCTP().getRList().get(0).getRPr().getRStyleOrRPrChangeOrRPr()`方法获取段落的第一个运行元素的`CTR`对象,并检查其中是否包含公式。如果包含公式,则使用`getOMathParaList().get(0).getOMathList().get(0)`方法获取公式的信息。最后,我们使用`getType()`方法检查公式类型并将其打印出来。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)