翻譯|使用教程|編輯:李顯亮|2020-03-12 11:24:10.697|閱讀 1161 次
概述:近年來,Word文檔的動態生成已成為組成報告,報價,發票和其他類型文檔的流行功能。本文旨在針對文檔自動化過程,介紹如何以Java編程方式創建豐富的Word文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
近年來,Word文檔的動態生成已成為組成報告,報價,發票和其他類型文檔的流行功能。各種制造公司都基于數據庫中存儲的數據生成發票。在這種情況下,文檔自動化可以節省手動文檔創建過程中所需的時間,精力和資源。
本文旨在針對文檔自動化過程,介紹如何以Java編程方式創建豐富的Word文檔。如果想要測試這項新功能,可點擊此處下載最新版試用。
在以下各節中,您將學習如何使用Java以編程方式創建包含不同元素(例如文本,段落,表格,列表,圖像等)的Word文檔。
大多數情況下,Word文檔中相當一部分內容是基于文本的。因此,我們將通過創建帶有標題和段落的Word文檔來開始我們的旅程。以下是使用Aspose.Words for Java執行此操作的步驟:
下面的代碼示例演示如何創建包含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文檔中的表用于以行和列的形式組織內容。在本節中,我們將創建一個包含兩行兩列的簡單表。創建表包括四個基本操作:
以下是在Word文檔中創建表的步驟:
下面的示例演示如何在Java中的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");
輸出結果:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn