Apache POI XWPF

 
 

Java API لمستندات Word OOXML

حل مفتوح المصدر لإنشاء ملفات Microsoft Word DOCX وقراءتها وتحريرها وتحويلها في تطبيقات Java.

يوفر Apache POI XWPF وظيفة قراءة وكتابة تنسيق ملف Microsoft Word 2007 DOCX. يحتوي XWPF على واجهة برمجة تطبيقات أساسية مستقرة إلى حد ما ، مما يوفر الوصول إلى الأجزاء الرئيسية من ملف 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

<!-- 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);
      }
    }
  }
}
 عربي