poi doc转docx
时间: 2023-08-31 10:09:28 浏览: 53
要将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://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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)