一、在https://poi.apache.org/download.html下载apache.poi 的jar包,并导入工程
二、代码:
public class Test{
public static void main(String[] args){
Test.writeWordFile();
}
public static boolean writeWordFile(){
boolean w = false;
//设置路径
String path = "d:/";
try{
if(!"".equals(path)){
// 检查目录是否存在
File fileDir = new File(path);
if(fileDir.exists()){
// 生成临时文件名称
String fileName = "a.doc";
String content = ""
+ "百度首页cdvdv"
+ "消息"
+ "";
byte b[] = content.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
FileOutputStream ostream = new FileOutputStream(path + fileName);
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
}
}
}catch(IOException e){
e.printStackTrace();
}
return w;
}
}