FileInputStream fis = new FileInputStream("test.docx"); XWPFDocument doc = new XWPFDocument(fis); // 替换文本 for (XWPFParagraph p : doc.getParagraphs()) { String text = p.getText(); if (text.contains("replace me")) { text = text.replace("replace me", "replaced"); p.set(text); } }
时间: 2024-02-14 18:05:11 浏览: 22
这段代码的作用是打开一个名为 "test.docx" 的 Word 文档,查找其中的 "replace me" 字符串并将其替换为 "replaced",然后将修改后的文本保存回原始文档中。
然而,这段代码存在一个问题:在替换文本后,应该使用 `p.setText(text)` 方法来设置段落的文本,而不是 `p.set(text)` 方法。因此,您需要将 `p.set(text)` 替换为 `p.setText(text)`,即:
```java
FileInputStream fis = new FileInputStream("test.docx");
XWPFDocument doc = new XWPFDocument(fis);
// 替换文本
for (XWPFParagraph p : doc.getParagraphs()) {
String text = p.getText();
if (text.contains("replace me")) {
text = text.replace("replace me", "replaced");
p.setText(text); // 修改此处
}
}
// 保存文档
FileOutputStream fos = new FileOutputStream("test.docx");
doc.write(fos);
fos.close();
doc.close();
```
这样修改后,代码就可以正常工作了。
相关问题
FileInputStream fis = new FileInputStream("test.docx"); XWPFDocument doc = new XWPFDocument(fis); // 替换文本 for (XWPFParagraph p : doc.getParagraphs()) { String text = p.getText(); if (text.contains("replace me")) { text = text.replace("replace me", "replaced"); p.setText(text); } }
这段代码的作用是打开一个名为 "test.docx" 的 Word 文档,查找其中的 "replace me" 字符串并将其替换为 "replaced",然后将修改后的文本保存回原始文档中。
然而,这段代码存在一个问题:在替换文本后,应该使用 `p.setText(text)` 方法来设置段落的文本,而不是 `p.set(text)` 方法。因此,您需要将 `p.set(text)` 替换为 `p.setText(text)`,即:
```java
FileInputStream fis = new FileInputStream("test.docx");
XWPFDocument doc = new XWPFDocument(fis);
// 替换文本
for (XWPFParagraph p : doc.getParagraphs()) {
String text = p.getText();
if (text.contains("replace me")) {
text = text.replace("replace me", "replaced");
p.setText(text); // 修改此处
}
}
// 保存文档
FileOutputStream fos = new FileOutputStream("test.docx");
doc.write(fos);
fos.close();
doc.close();
```
这样修改后,代码就可以正常工作了。
poi doc转docx
要将POI中的.doc文件转换为.docx文件,可以使用以下代码:
```java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
public class DocToDocxConverter {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("sample.doc");
HWPFDocument doc = new HWPFDocument(fis);
XWPFDocument docx = new XWPFDocument();
for (int i = 0; i < doc.getRange().numParagraphs(); i++) {
XWPFParagraph p = docx.createParagraph();
p.createRun().setText(doc.getRange().getParagraph(i).text());
}
FileOutputStream fos = new FileOutputStream("sample.docx");
docx.write(fos);
fos.close();
docx.close();
doc.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
```
这个程序将从“sample.doc”文件中读取内容,将其转换为XWPFDocument格式,然后将其写入到“sample.docx”文件中。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)