翻譯|使用教程|編輯:胡濤|2023-03-28 10:38:30.533|閱讀 153 次
概述:本文旨在為您提供最好的 在Java中創建MS Word文檔的最簡單方法不需要MS Office
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words 是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
近年來,動態生成Word文檔已成為撰寫報告、報價、發票和其他類型文檔的流行功能。有各種庫可供使用,它們允許您 在Java中以編程方式生成MS Word文檔.但是,其中一些需要MS Office,而另一些則需要編寫復雜的代碼??紤]到這些問題,本文旨在為您提供最好的 在Java中創建MS Word文檔的最簡單方法不需要MS Office。此外,您還將學習如何 自動化MS Word用最少的簡單代碼行來實現這些功能。
在本文中,我們將使用Aspose.WordsforJava,這是一個功能豐富的庫,用于在基于Java的應用程序中創建、編輯或轉換Word文檔。您可以下載API的JAR或使用以下Maven配置安裝它:
儲存庫:
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>//repository.aspose.com/repo/</url> </repository>
依賴性:
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>22.11</version> <classifier>jdk17</classifier> </dependency>
大多數時候,Word文檔中的相當一部分內容是基于文本的。因此,我們將通過創建一個帶有標題和段落的Word文檔開始我們的旅程。
以下是從頭開始創建Word文檔的步驟:
下面的代碼示例演示如何在Java中創建包含文本的Word文檔。
// Create a Document object Document doc = new Document(); // Create a DocumentBuilder object DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting Font font = builder.getFont(); font.setSize(18); font.setBold(true); font.setColor(Color.BLACK); font.setName("Arial"); builder.write("How to Create a Rich Word Document?"); builder.insertBreak(BreakType.LINE_BREAK); // Start the paragraph font.setSize(12); font.setBold(false); ParagraphFormat paragraphFormat = builder.getParagraphFormat(); paragraphFormat.setFirstLineIndent(12); paragraphFormat.setKeepTogether(true); builder.write("This article shows how to create a Word document containing text, images and lists."); // Save the document doc.save("Rich Word Document.docx");
輸出 :
Word文檔中的表格用于以行和列的形式組織內容。在本節中,我們將創建一個包含兩行和兩列的簡單表。創建表包括四個基本操作:
以下是在Java中創建Word文檔中的表的步驟:
下面的示例顯示如何生成Word文檔并向其中添加表。
// Create a Document object Document doc = new Document(); // Create a DocumentBuilder object DocumentBuilder builder = new DocumentBuilder(doc); // Create table Table table = builder.startTable(); // Insert a cell builder.insertCell(); table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW); builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); builder.write("This is Row 1 Cell 1"); builder.insertCell(); builder.write("This is Row 1 Cell 2"); // End row builder.endRow(); // start a next row and set its properties builder.getRowFormat().setHeight(100); builder.getRowFormat().setHeightRule(HeightRule.EXACTLY); builder.insertCell(); builder.write("This is Row 2 Cell 1"); builder.insertCell(); builder.write("This is Row 2 Cell 2"); builder.endRow(); // End table builder.endTable(); // Save the document doc.save("Rich Word Document.docx");
輸出 :
以下是向Word文檔添加列表的步驟。
以下代碼示例顯示了如何使用 Java 在 Word 文檔中創建列表。
// Create a Document object Document doc = new Document(); doc.getLists().add(ListTemplate.BULLET_CIRCLE); List list = doc.getLists().get(0); // Set true to specify that the list has to be restarted at each section. list.isRestartAtEachSection(true); DocumentBuilder builder = new DocumentBuilder(doc); builder.getListFormat().setList(list); for (int i = 1; i < 45; i++) { builder.writeln(String.format("List Item " + i)); // Insert section break. if (i == 15) builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); } builder.getListFormat().removeNumbers(); // Save the document doc.save("Rich Word Document.docx");
輸出 :
將圖像插入Word文檔就像餡餅一樣簡單。以下是執行此操作的一些簡單步驟:
下面的代碼示例顯示了如何在Java中將圖像插入Word文檔。
// Create a Document object Document doc = new Document(); // Create DocumentBuiler DocumentBuilder builder = new DocumentBuilder(doc); // Insert Image builder.insertImage("aspose-words.png"); // Save the document doc.save("Rich Word Document.docx");
輸出:
以上便是在 C++ 中將 Word DOC 或 DOCX 轉換為 PDF ,要是您還有其他關于產品方面的問題,歡迎咨詢我們,或者加入我們官方技術交流群。
歡迎下載|體驗更多Aspose產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn