Apache POI XWPF

 
 

Java API สำหรับ Word OOXML เอกสาร

โซลูชันโอเพนซอร์สเพื่อสร้าง อ่าน แก้ไข และแปลงไฟล์ Microsoft Word DOCX ในแอปพลิเคชัน Java

Apache POI XWPF มีฟังก์ชันสำหรับอ่านและเขียนรูปแบบไฟล์ Microsoft Word 2007 DOCX XWPF มี API หลักที่ค่อนข้างเสถียร ทำให้สามารถเข้าถึงส่วนหลักของไฟล์ Word DOCX สามารถใช้สำหรับการแยกข้อความพื้นฐานและเฉพาะ การจัดการส่วนหัวและส่วนท้าย การจัดการข้อความ และคุณลักษณะการจัดรูปแบบ 

Apache POI XWPF มีชื่อเสียงในด้านการสร้างไฟล์ Microsoft Word & การแก้ไขเอกสาร การจัดรูปแบบข้อความและย่อหน้า การแทรกรูปภาพ การสร้างตารางและการแยกวิเคราะห์ คุณลักษณะจดหมายเวียน การจัดการองค์ประกอบแบบฟอร์ม และอื่นๆ อีกมากมาย

Previous Next

เริ่มต้นใช้งาน Apache POI XWPF

ก่อนอื่น คุณต้องมี Java Development Kit (JDK) ติดตั้งอยู่ในระบบของคุณ หากคุณมีอยู่แล้ว ให้ไปที่หน้าดาวน์โหลดของ Apache POI เพื่อรับเวอร์ชันเสถียรล่าสุดในไฟล์เก็บถาวร แยกเนื้อหาของไฟล์ ZIP ในไดเร็กทอรีใดๆ จากตำแหน่งที่ไลบรารีที่จำเป็นสามารถเชื่อมโยงกับโปรแกรม Java ของคุณได้ นั้นคือทั้งหมด!

การอ้างอิง Apache POI ในโปรเจ็กต์ Java ที่ใช้ Maven นั้นง่ายกว่า สิ่งที่คุณต้องมีคือเพิ่มการพึ่งพาต่อไปนี้ใน pom.xml ของคุณและให้ IDE ดึงข้อมูลและอ้างอิงไฟล์ Apache POI Jar

Apache POI Maven Dependency

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>4.1.0</version>
</dependency>

สร้างและแก้ไขเอกสาร Word โดยใช้ Java API

Apache POI XWPF ช่วยให้โปรแกรมเมอร์ซอฟต์แวร์สร้างเอกสาร Word ใหม่ในรูปแบบไฟล์ DOCX นักพัฒนายังสามารถโหลดไฟล์ Microsoft Word DOCX ที่มีอยู่เพื่อแก้ไขตามความต้องการของแอพพลิเคชั่น ช่วยให้คุณสามารถเพิ่มย่อหน้าใหม่ แทรกข้อความ ใช้การจัดแนวข้อความและเส้นขอบ เปลี่ยนรูปแบบข้อความ และอื่นๆ

สร้างไฟล์ DOCX ตั้งแต่เริ่มต้น

// initialize a blank document
XWPFDocument document = new XWPFDocument();
// create a new file
FileOutputStream out = new FileOutputStream(new File("document.docx"));
// create a new paragraph paragraph
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("File Format Developer Guide - " +
  "Learn about computer files that you come across in " +
  "your daily work at: www.fileformat.com ");
document.write(out);
out.close();

เพิ่มย่อหน้า รูปภาพ & ตารางลงในเอกสาร Word

Apache POI XWPF อนุญาตให้นักพัฒนาเพิ่มย่อหน้าและรูปภาพลงในเอกสาร Word API ยังมีคุณลักษณะในการเพิ่มตารางลงในเอกสาร DOCX ในขณะที่สร้างตารางที่เรียบง่ายและซ้อนกันด้วยข้อมูลที่ผู้ใช้กำหนดเองได้

สร้างไฟล์ DOCX ใหม่ด้วยตาราง

// initialize a blank document
XWPFDocument document = new XWPFDocument();
// create a new file
FileOutputStream out = new FileOutputStream(new File("table.docx"));
// create a new table
XWPFTable table = document.createTable();
// create first row
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("Serial No");
tableRowOne.addNewTableCell().setText("Products");
tableRowOne.addNewTableCell().setText("Formats");
// create second row
XWPFTableRow tableRowTwo = table.createRow();
tableRowTwo.getCell(0).setText("1");
tableRowTwo.getCell(1).setText("Apache POI XWPF");
tableRowTwo.getCell(2).setText("DOCX, HTML, FO, TXT, PDF");
// create third row
XWPFTableRow tableRowThree = table.createRow();
tableRowThree.getCell(0).setText("2");
tableRowThree.getCell(1).setText("Apache POI HWPF");
tableRowThree.getCell(2).setText("DOC, HTML, FO, TXT");
document.write(out);
out.close();

แยกข้อความจากเอกสาร Word OOXML

Apache POI XWPF มีคลาสเฉพาะในการดึงข้อมูลจากเอกสาร Microsoft Word DOCX ด้วยโค้ดเพียงไม่กี่บรรทัด ในทำนองเดียวกัน ก็สามารถแยกหัวเรื่อง เชิงอรรถ ข้อมูลตาราง และอื่นๆ ออกจากไฟล์ Word ได้

แยกข้อความจากไฟล์ Word

// load DOCX file
FileInputStream fis = new FileInputStream("document.docx");
// open file
XWPFDocument file  = new XWPFDocument(OPCPackage.open(fis));
// read text
XWPFWordExtractor ext = new XWPFWordExtractor(file);
// display text
System.out.println(ext.getText());

เพิ่มส่วนหัวและส่วนท้ายที่กำหนดเองในเอกสาร DOCX

ส่วนหัวและส่วนท้ายเป็นส่วนสำคัญของเอกสาร Word เนื่องจากมักจะมีข้อมูลเพิ่มเติม เช่น วันที่ หมายเลขหน้า ชื่อผู้เขียน และเชิงอรรถ ซึ่งช่วยให้เอกสารยาวขึ้นและอ่านง่ายขึ้น Apache POI XWPF ช่วยให้นักพัฒนา Java สามารถเพิ่มส่วนหัวและส่วนท้ายที่กำหนดเองลงในเอกสาร Word

จัดการส่วนหัวและส่วนท้ายที่กำหนดเองในไฟล์ Word DOCX


public class HeaderFooterTable {
  public static void main(String[] args) throws IOException {
    try (XWPFDocument doc = new XWPFDocument()) {
      // Create a header with a 1 row, 3 column table
      XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
      XWPFTable tbl = hdr.createTable(1, 3);
      // Set the padding around text in the cells to 1/10th of an inch
      int pad = (int) (.1 * 1440);
      tbl.setCellMargins(pad, pad, pad, pad);
      // Set table width to 6.5 inches in 1440ths of a point
      tbl.setWidth((int) (6.5 * 1440));
      CTTbl ctTbl = tbl.getCTTbl();
      CTTblPr ctTblPr = ctTbl.addNewTblPr();
      CTTblLayoutType layoutType = ctTblPr.addNewTblLayout();
      layoutType.setType(STTblLayoutType.FIXED);
      BigInteger w = new BigInteger("3120");
      CTTblGrid grid = ctTbl.addNewTblGrid();
      for (int i = 0; i < 3; i++) {
        CTTblGridCol gridCol = grid.addNewGridCol();
        gridCol.setW(w);
      }
      // Add paragraphs to the cells
      XWPFTableRow row = tbl.getRow(0);
      XWPFTableCell cell = row.getCell(0);
      XWPFParagraph p = cell.getParagraphArray(0);
      XWPFRun r = p.createRun();
      r.setText("header left cell");
      cell = row.getCell(1);
      p = cell.getParagraphArray(0);
      r = p.createRun();
      r.setText("header center cell");
      cell = row.getCell(2);
      p = cell.getParagraphArray(0);
      r = p.createRun();
      r.setText("header right cell");
      // Create a footer with a Paragraph
      XWPFFooter ftr = doc.createFooter(HeaderFooterType.DEFAULT);
      p = ftr.createParagraph();
      r = p.createRun();
      r.setText("footer text");
      try (OutputStream os = new FileOutputStream(new File("headertable.docx"))) {
        doc.write(os);
      }
    }
  }
}
 ไทย