ตัวอย่างโคด
package com.java.poi.word;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class HelloWord {
public static void main(String[] args) {
try {
// path ของไฟล์
String fileName = "C:\\poi\\HelloWord.docx";
// สร้างเอกสารเปล่าๆ
XWPFDocument document= new XWPFDocument();
// บันทึกเอกสารไว้ในระบบตาม path ที่ระบุ
FileOutputStream out = new FileOutputStream(fileName);
// สร้าง พารากราฟ
XWPFParagraph paragraph = document.createParagraph();
// ใส่เนื้อหา
XWPFRun run = paragraph.createRun();
run.setText("Hello Apache POI Word");
document.write(out);
out.close();
System.out.println("Word document created.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
ไฟล์ที่ถูกสร้าง
ผลลัพธ์ที่ได้


0 comments:
Post a Comment