- Apache POI Word 教程
- Apache POI Word - 首頁
- Apache POI Word - 概述
- Apache POI Word - 安裝
- Apache POI Word - 核心類
- Apache POI Word - 文件
- Apache POI Word - 段落
- Apache POI Word - 邊框
- Apache POI Word - 表格
- Apache POI Word - 字型和對齊
- Apache POI Word - 文字提取
- Apache POI Word 資源
- Apache POI Word - 快速指南
- Apache POI Word - 有用資源
- Apache POI Word - 討論
Apache POI Word - 段落
在本章中,你將學習如何使用 Java 建立段落,以及如何將其新增到文件中。段落是 Word 檔案中頁面的一部分。
完成本章後,你將能夠建立段落並在其上執行讀取操作。
建立段落
首先,讓我們使用前面章節中討論的引用類建立段落。按照上一章的做法,先建立一個文件,然後我們就可以建立段落了。
以下程式碼片段用於建立電子表格 -
//Create Blank document XWPFDocument document = new XWPFDocument(); //Create a blank spreadsheet XWPFParagraph paragraph = document.createParagraph();
在段落上執行
你可以使用Run輸入文字或任何物件元素。使用段落例項,你可以建立run。
以下程式碼片段用於建立一個 Run。
XWPFRun run = paragraph.createRun();
寫入段落
我們嘗試向某個文件中輸入一些文字。考慮以下文字資料 -
At tutorialspoint.com, we strive hard to provide quality tutorials for self-learning purpose in the domains of Academics, Information Technology, Management and Computer Programming Languages.
以下程式碼用於將上述資料寫入段落。
import java.io.File;
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 CreateParagraph {
public static void main(String[] args)throws Exception {
//Blank Document
XWPFDocument document = new XWPFDocument();
//Write the Document in file system
FileOutputStream out = new FileOutputStream(new File("createparagraph.docx"));
//create Paragraph
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("At tutorialspoint.com, we strive hard to " +
"provide quality tutorials for self-learning " +
"purpose in the domains of Academics, Information " +
"Technology, Management and Computer Programming Languages.");
document.write(out);
out.close();
System.out.println("createparagraph.docx written successfully");
}
}
將上面的 Java 程式碼儲存為CreateParagraph.java,然後從命令列透過以下方式對其進行編譯並執行 -
$javac CreateParagraph.java $java CreateParagraph
它將編譯並執行,在你的當前目錄中生成名為createparagraph.docx的 Word 檔案,並且你將在命令列中獲得以下輸出 -
createparagraph.docx written successfully
createparagraph.docx檔案如下所示。
廣告