JAVA代码把字符串输入到word文件中
时间: 2023-04-10 16:00:54 浏览: 76
可以使用Apache POI库来实现将字符串输入到word文件中。具体实现可以参考以下代码:
```
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
public class WriteToWord {
public static void main(String[] args) {
String content = "这是要写入到word文件中的字符串内容";
String filePath = "D:/test.docx";
try {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
paragraph.createRun().setText(content);
FileOutputStream out = new FileOutputStream(filePath);
document.write(out);
out.close();
System.out.println("写入成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```