//创建.doc后缀的word
public class WordUtils1 {
public static void main(String[] args) {
String path = "E:\\YishiFile";
String fileName = "test.doc";
//createWord(path,fileName);
//writeDataDocx
String data = "helloword";
writeDataDocx(path+"\\"+fileName,data,true,12);
}
public static void createWord(String path, String fileName) {
//判断目录是否存在
File file = new File(path);
//exists()测试此抽象路径名表示的文件或目录是否存在。
//mkdir()创建此抽象路径名指定的目录。
//mkdirs()创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
if (!file.exists()) file.mkdirs();
//因为HWPFDocument并没有提供公共的构造方法 所以没有办法构造word
//这里使用word2007及以上的XWPFDocument来进行构造word
@SuppressWarnings("resource")
XWPFDocument document = new XWPFDocument();
OutputStream stream = null;
try {
stream = new FileOutputStream(new File(file, fileName));
document.write(stream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (stream != null) ;
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//向word中写入数据
/**
* 有些方法需要传特殊类型的参数的时候,一般可以用★静态的接口.参数★来传参
*
* @param path
* @param data
*/
public static void writeDataDocx(String path, String data, boolean jiacu, int size) {
InputStream istream = null;
OutputStream ostream = null;
try {
istream = new FileInputStream(path);
ostream = new FileOutputStream(path);
@SuppressWarnings("resource")
XWPFDocument document = new XWPFDocument();
//添加一个段落
XWPFParagraph p1 = document.createParagraph();
XWPFRun r1 = p1.createRun();//p1.createRun()将一个新运行追加到这一段
r1.setText(data);
r1.setBold(jiacu);//---"加黑加粗"
r1.setFontSize(size);//---字体大小
document.write(ostream);
System.out.println("创建word成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (istream != null) {
try {
istream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ostream != null) {
try {
ostream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//向word中写入数据
// public static void writeDataDoc(String path,String data){
// OutputStream ostream=null;
// try {
// ostream = new FileOutputStream(path);
// ostream.write(data.getBytes());
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }finally{
// if(ostream != null){
// try {
// ostream.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// }
//读取数据 docx
public static String readDataDocx(String filePath) {
String content = "";
InputStream istream = null;
try {
istream = new FileInputStream(filePath);
@SuppressWarnings("resource")
XWPFDocument document = new XWPFDocument(istream);
//getLastParagraph()返回包含页眉或页脚的文本的段落
//getText()返回文档所有文本
content = document.getLastParagraph().getText();//★★★★★
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (istream != null) {
}
}
return content;
}
}
maven依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>