翻譯|行業(yè)資訊|編輯:胡濤|2024-01-05 13:18:23.867|閱讀 160 次
概述:在這篇博文中,我們將探討如何在 Java 應用程序中在 Word 文檔中創(chuàng)建表格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Microsoft Word 是一種流行的文字處理應用程序,用于創(chuàng)建各種類型的文檔。這些文檔可能包含多種類型的元素,包括文本、圖像、表格和圖表。當涉及到用 Java 自動創(chuàng)建和操作文檔時,您可能需要一個輕松的解決方案來在 Word 文檔中創(chuàng)建表格。因此,在這篇博文中,我們將探討如何在 Java 應用程序中在 Word 文檔中創(chuàng)建表格。
Aspose.Words 是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現(xiàn)和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
Aspose.words for.net下載 Aspose.words for for java下載
Aspose.Words for Java是一個 API,允許 Java 開發(fā)人員以編程方式處理 Microsoft Word 文檔。它提供了用于創(chuàng)建、修改和操作 Word 文檔的廣泛功能,使其成為自動化文檔生成和處理任務的寶貴工具。我們將使用該庫將表格插入到 Word 文檔中。
您可以下載該庫或使用以下 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>23.10</version> <classifier>jdk17</classifier> </dependency>
使用 Aspose.Words for Java 在 Word 文檔中創(chuàng)建表格有兩種方法:
您可以選擇最適合您要求的方法。那么讓我們詳細探討一下這些方法。
使用 DocumentBuilder 創(chuàng)建表
DocumentBuilder類為您提供了一種快速、簡單的方法來從頭開始創(chuàng)建動態(tài)文檔或處理現(xiàn)有文檔。它提供了一系列用于插入各種內容元素的功能,例如文本、復選框、OLE 對象、段落、列表、表格、圖像等。
以下是在Java中使用DocumentBuilder類在Word文檔中創(chuàng)建表格的步驟。
以下代碼片段展示了如何使用 Java 在 Word 文檔中創(chuàng)建表格。
// Create or load document. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a new table and insert cell. Table table = builder.startTable(); builder.insertCell(); // Table wide formatting must be applied after at least one row is present in the table. table.setLeftIndent(20.0); // Set height and define the height rule for the header row. builder.getRowFormat().setHeight(40.0); builder.getRowFormat().setHeightRule(HeightRule.AT_LEAST); builder.getCellFormat().getShading().setBackgroundPatternColor(new Color((198), (217), (241))); builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); builder.getFont().setSize(16.0); builder.getFont().setName("Arial"); builder.getFont().setBold(true); builder.getCellFormat().setWidth(100.0); builder.write("Header Row,\n Cell 1"); // We don't need to specify this cell's width because it's inherited from the previous cell. builder.insertCell(); builder.write("Header Row,\n Cell 2"); builder.insertCell(); builder.getCellFormat().setWidth(200.0); builder.write("Header Row,\n Cell 3"); builder.endRow(); builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE); builder.getCellFormat().setWidth(100.0); builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); // Reset height and define a different height rule for table body. builder.getRowFormat().setHeight(30.0); builder.getRowFormat().setHeightRule(HeightRule.AUTO); builder.insertCell(); // Reset font formatting. builder.getFont().setSize(12.0); builder.getFont().setBold(false); builder.write("Row 1, Cell 1 Content"); builder.insertCell(); builder.write("Row 1, Cell 2 Content"); builder.insertCell(); builder.getCellFormat().setWidth(200.0); builder.write("Row 1, Cell 3 Content"); builder.endRow(); builder.insertCell(); builder.getCellFormat().setWidth(100.0); builder.write("Row 2, Cell 1 Content"); builder.insertCell(); builder.write("Row 2, Cell 2 Content"); builder.insertCell(); builder.getCellFormat().setWidth(200.0); builder.write("Row 2, Cell 3 Content."); builder.endRow(); // End table. builder.endTable(); // Save document. doc.save("table.docx");
以下是我們使用上面的代碼示例創(chuàng)建的表的屏幕截圖。
文檔對象模型 (DOM)是 Word 文檔的內存中表示形式,允許您以編程方式讀取、操作和修改 Word 文檔的內容和格式。以下步驟演示如何使用 DOM 在 Word 文檔中創(chuàng)建表格。
以下代碼片段展示了如何使用 Java 在 Word 文檔中創(chuàng)建表格。
// Create or load document. Document doc = new Document(); // We start by creating the table object. Note that we must pass the document object // to the constructor of each node. This is because every node we create must belong // to some document. Table table = new Table(doc); doc.getFirstSection().getBody().appendChild(table); // Here we could call EnsureMinimum to create the rows and cells for us. This method is used // to ensure that the specified node is valid. In this case, a valid table should have at least one Row and one cell. // Instead, we will handle creating the row and table ourselves. // This would be the best way to do this if we were creating a table inside an algorithm. Row row = new Row(doc); row.getRowFormat().setAllowBreakAcrossPages(true); table.appendChild(row); // We can now apply any auto fit settings. table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS); Cell cell = new Cell(doc); cell.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE); cell.getCellFormat().setWidth(80.0); cell.appendChild(new Paragraph(doc)); cell.getFirstParagraph().appendChild(new Run(doc, "Row 1, Cell 1 Text")); row.appendChild(cell); // We would then repeat the process for the other cells and rows in the table. // We can also speed things up by cloning existing cells and rows. row.appendChild(cell.deepClone(false)); row.getLastCell().appendChild(new Paragraph(doc)); row.getLastCell().getFirstParagraph().appendChild(new Run(doc, "Row 1, Cell 2 Text")); // Save document. doc.save("table.docx");
還可能存在這樣的情況:您需要創(chuàng)建位于父表單元格內的嵌套表。您無需經(jīng)過復雜的過程即可做到這一點。首先,創(chuàng)建一個父表,然后調用DocumentBuilder.moveTo(Cell.getFirstParagraph())方法將控件移動到父表的所需單元格內。完成后,以同樣的方式創(chuàng)建一個新表。
以下代碼片段展示了如何使用 Java 在 Word 文檔中創(chuàng)建嵌套表格。
// Create or load document. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert cell. Cell cell = builder.insertCell(); builder.writeln("Outer Table Cell 1"); builder.insertCell(); builder.writeln("Outer Table Cell 2"); // This call is important to create a nested table within the first table. // Without this call, the cells inserted below will be appended to the outer table. builder.endTable(); // Move to the first cell of the outer table. builder.moveTo(cell.getFirstParagraph()); // Build the inner table. builder.insertCell(); builder.writeln("Inner Table Cell 1"); builder.insertCell(); builder.writeln("Inner Table Cell 2"); builder.endTable(); // Save document. doc.save("table.docx");
以下是我們上面創(chuàng)建的嵌套表的屏幕截圖。
您還可以使用 HTML 字符串在 Word 文檔中創(chuàng)建表格,以下是要遵循的步驟。
下面是從 HTML 字符串生成 Word 表格的代碼片段。
// Create or load document. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Note that AutoFitSettings does not apply to tables inserted from HTML. builder.insertHtml("<table>" "<tr>" "<td>Row 1, Cell 1</td>" "<td>Row 1, Cell 2</td>" "</tr>" "<tr>" "<td>Row 2, Cell 2</td>" "<td>Row 2, Cell 2</td>" "</tr>" "</table>"); // Save document. doc.save("table.docx");
在這篇博文中,我們探討了如何使用 Java 在 Word 文檔中創(chuàng)建表格。您已經(jīng)了解了如何使用文檔生成器或 DOM 創(chuàng)建表、創(chuàng)建嵌套表以及從 HTML 字符串創(chuàng)建表。通過安裝該庫并遵循指南,您可以輕松地將表創(chuàng)建功能集成到您的 Java 應用程序中。
歡迎下載|體驗更多Aspose產(chǎn)品
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn