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); } }
时间: 2024-02-14 09:05:11 浏览: 23
这段代码的作用是打开一个名为 "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”文件中。
使用Java写一个提取.docx文档首行内容并以此重命名该文档的程序
可以使用Apache POI库来读取.docx文件,以下是一个示例代码:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
public class RenameDocx {
public static void main(String[] args) {
try {
// 读取.docx文件
File file = new File("example.docx");
FileInputStream fis = new FileInputStream(file);
XWPFDocument doc = new XWPFDocument(fis);
// 获取首行内容
String firstLine = "";
for (XWPFParagraph para : doc.getParagraphs()) {
firstLine = para.getText().trim();
if (!firstLine.isEmpty()) {
break;
}
}
// 重命名文件
String newName = firstLine + ".docx";
File newFile = new File(newName);
FileOutputStream fos = new FileOutputStream(newFile);
doc.write(fos);
// 关闭流
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
这个程序会读取名为"example.docx"的文件,提取出首行内容并以此重命名该文件。如果首行内容为空,则文件名不变。注意,这个程序只适用于.docx格式的文件,如果要处理.doc格式的文件,需要使用不同的库。
相关推荐
![](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)