/
WordHelloWorld.java
70 lines (56 loc) · 2.07 KB
/
WordHelloWorld.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.github.wyleedp.poi.worker;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
/**
* <pre>
* [2023.11.03]
* * Hello World!! 워드파일 생성 예제
* * 생성된 워드파일은 사용자 임시폴더의 년월일시분초_HelloWorld.docx 파일로 생성된다.
* - 경로 예) C:\Users\wyleedp\AppData\Local\Temp\20231106101051_HelloWorld.docx
* </pre>
*/
public class WordHelloWorld {
public void exec() {
System.out.println("워드파일 생성 시작");
FileOutputStream fos = null;
XWPFDocument documentWord = null;
try {
String tmpHome = System.getProperties().getProperty("java.io.tmpdir");
String wordFileName = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + "_HelloWorld.docx";
String wordFilePath = tmpHome + wordFileName;
documentWord = new XWPFDocument();
XWPFParagraph XWPFParagraph = documentWord.createParagraph();
XWPFParagraph.setAlignment(ParagraphAlignment.LEFT);
XWPFRun helloRun = XWPFParagraph.createRun();
helloRun.setFontFamily("맑은 고딕");
helloRun.setColor("2FB2F3");
helloRun.setFontSize(20);
helloRun.setText("Hello World!!!");
fos = new FileOutputStream(wordFilePath);
documentWord.write(fos);
System.out.println("워드파일 생성완료 : " + wordFilePath + " [" + FileUtils.byteCountToDisplaySize(new File(wordFilePath).length()) + "]");
}catch(Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(fos);
try {
documentWord.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
WordHelloWorld wordHelloWorld = new WordHelloWorld();
wordHelloWorld.exec();
}
}