翻譯|使用教程|編輯:胡濤|2023-03-31 10:47:17.600|閱讀 144 次
概述:本文旨在為您提供無(wú)需 MS Office 即可在 Java 中創(chuàng)建 MS Word 文檔的最佳但最簡(jiǎn)單的方法。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.Words 是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
近年來(lái),動(dòng)態(tài)生成 Word 文檔已成為編寫報(bào)告、報(bào)價(jià)單、發(fā)票和其他類型文檔的流行功能。有各種庫(kù)可供您使用 Java 以編程方式生成 MS Word 文檔。但是,其中一些需要 MS Office,而另一些則需要您編寫復(fù)雜的代碼。考慮到這些問(wèn)題,本文旨在為您提供無(wú)需 MS Office 即可在 Java 中創(chuàng)建 MS Word 文檔的最佳但最簡(jiǎn)單的方法。此外,您還將學(xué)習(xí)如何使用最少的簡(jiǎn)單代碼行來(lái)自動(dòng)化 MS Word功能。
在本文中,我們將使用Aspose.Words for Java,這是一個(gè)功能豐富的庫(kù),用于在基于 Java 的應(yīng)用程序中創(chuàng)建、編輯或轉(zhuǎn)換 Word 文檔。您可以下載API 的 JAR 或使用以下 Maven 配置安裝它:
儲(chǔ)存庫(kù):
<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>
在以下部分中,您將學(xué)習(xí)如何使用 Java 以編程方式創(chuàng)建包含文本、段落、表格、列表、圖像等不同元素的 Word 文檔。
大多數(shù)時(shí)候,Word文檔中有相當(dāng)一部分內(nèi)容是基于文本的。因此,我們將通過(guò)創(chuàng)建一個(gè)帶有標(biāo)題和段落的 Word 文檔來(lái)開始我們的旅程。
以下是從頭開始創(chuàng)建 Word 文檔的步驟:
以下代碼示例顯示了如何使用 Java 創(chuàng)建包含文本的 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 文檔中的表格用于以行和列的形式組織內(nèi)容。在本節(jié)中,我們將創(chuàng)建一個(gè)包含兩行和兩列的簡(jiǎn)單表格。創(chuàng)建表包括四個(gè)基本操作:
下面是用Java在Word文檔中創(chuàng)建表格的步驟:
以下示例顯示了如何生成 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 文檔中創(chuàng)建列表。
// 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文檔中就像餅圖一樣簡(jiǎn)單。以下是執(zhí)行此操作的一些簡(jiǎn)單步驟:
以下代碼示例顯示了如何使用 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");
輸出:
以上便是如何在 Java 中創(chuàng)建 Word 文檔 - MS Word Automation ,要是您還有其他關(guān)于產(chǎn)品方面的問(wèn)題,歡迎咨詢我們,或者加入我們官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn