翻譯|使用教程|編輯:張瑩心|2021-10-08 10:42:50.283|閱讀 86 次
概述:表格用于以行和列的形式很好地組織數據。此外,它們匯總了要查看和分析的數據。MS PowerPoint 還允許演示者在演示文稿中創建表格。因此,在本文中,您將學習如何使用 Java 在 PowerPoint 演示文稿中創建和操作表格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
表格用于以行和列的形式很好地組織數據。此外,它們匯總了要查看和分析的數據。MS PowerPoint 還允許演示者在演示文稿中創建表格。因此,在本文中,您將學習如何使用 Java 在 PowerPoint 演示文稿中創建和操作表格。
為了在 PowerPoint 演示使用 Java 在 PowerPoint 中創建和操作表格,我們將使用Aspose.Slides for Java,它是一個強大且功能豐富的 API,用于在 Java 應用程序中創建和操作演示文稿。
>>你可以點擊這里下載Aspose.Slides 最新版測試體驗。
鎖定表格的縱橫比
在 PowerPoint 中創建和操作表格的 Java API
為了在 PowerPoint 演示文稿中創建和操作表格,我們將使用Aspose.Slides for Java。該 API 旨在創建、操作和轉換 PowerPoint 和 OpenOffice 演示文稿。您可以下載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-slides</artifactId> <version>21.8</version> <classifier>jdk16</classifier> </dependency>
使用 Java 在 PowerPoint 演示文稿中創建表格
使用 Aspose.Slides for Java 創建表格就像餡餅一樣簡單。以下步驟演示了如何使用 Java 從頭開始在 PowerPoint 演示文稿中創建表格。
使用ITable.getRows().get_Item(rowIndex).get_Item(cellIndex).getTextFrame().setText(String)方法設置單元格的文本。
將單元格格式的引用獲取到ICellFormat對象中。
如果需要,設置單元格的邊框樣式。
以下代碼示例展示了如何在 PowerPoint 演示文稿中創建表格。
// Create or load presentation Presentation pres = new Presentation(); try { // Access first slide ISlide sld = pres.getSlides().get_Item(0); // Define columns with widths and rows with heights double[] dblCols = { 50, 50, 50 }; double[] dblRows = { 50, 30, 30, 30, 30 }; // Add table shape to slide ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows); // Set text and border format for each cell for (int row = 0; row < tbl.getRows().size(); row++) { for (int cell = 0; cell < tbl.getRows().get_Item(row).size(); cell++) { // Set text tbl.getRows().get_Item(row).get_Item(cell).getTextFrame().setText("Cell_" + cell); // Set border ICellFormat cellFormat = tbl.getRows().get_Item(row).get_Item(cell).getCellFormat(); cellFormat.getBorderTop().getFillFormat().setFillType(FillType.Solid); cellFormat.getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED); cellFormat.getBorderTop().setWidth(5); cellFormat.getBorderBottom().getFillFormat().setFillType(FillType.Solid); cellFormat.getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED); cellFormat.getBorderBottom().setWidth(5); cellFormat.getBorderLeft().getFillFormat().setFillType(FillType.Solid); cellFormat.getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED); cellFormat.getBorderLeft().setWidth(5); cellFormat.getBorderRight().getFillFormat().setFillType(FillType.Solid); cellFormat.getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED); cellFormat.getBorderRight().setWidth(5); } } // Save PPTX to Disk pres.save("table.pptx", SaveFormat.Pptx); } finally { if (pres != null) pres.dispose(); }
以下屏幕截圖顯示了我們使用上述代碼創建的表。
使用 Java 訪問演示文稿中的表格
您還可以訪問現有 PowerPoint 演示文稿中的表格并根據需要對其進行操作。以下是訪問演示文稿中表格的步驟。
以下代碼示例展示了如何使用 Java 訪問 PowerPoint 演示文稿中的表格。
// Create or load presentation Presentation pres = new Presentation("UpdateExistingTable.pptx"); try { // Access the first slide ISlide sld = pres.getSlides().get_Item(0); // Initialize ITable ITable tbl = null; // Iterate through the shapes and get a reference to the table found for (IShape shp : sld.getShapes()) { if (shp instanceof ITable) { tbl = (ITable) shp; // Set the text of the first column of second row tbl.get_Item(0, 1).getTextFrame().setText("New"); } } // Write the PPTX to disk pres.save("table1_out.pptx", SaveFormat.Pptx); } finally { if (pres != null) pres.dispose(); }
使用 Java 格式化 PowerPoint 表格中的文本
Aspose.Slides for Java 還允許您非常輕松地設置表格的格式,如下面的步驟所示。
以下代碼示例展示了如何使用 Java 在 PowerPoint 中設置表格的格式。
// Load presentation Presentation pres = new Presentation("simpletable.pptx"); try { // Get reference of the table ITable someTable = (ITable) pres.getSlides().get_Item(0).getShapes().get_Item(0); // Set table cells' font height PortionFormat portionFormat = new PortionFormat(); portionFormat.setFontHeight(25); someTable.setTextFormat(portionFormat); // Set table cells' text alignment and right margin in one call ParagraphFormat paragraphFormat = new ParagraphFormat(); paragraphFormat.setAlignment(TextAlignment.Right); paragraphFormat.setMarginRight(20); someTable.setTextFormat(paragraphFormat); // Set table cells' text vertical type TextFrameFormat textFrameFormat = new TextFrameFormat(); textFrameFormat.setTextVerticalType(TextVerticalType.Vertical); someTable.setTextFormat(textFrameFormat); // Save presentation pres.save("result.pptx", SaveFormat.Pptx); } finally { if (pres != null) pres.dispose(); }
使用 Java 鎖定 PowerPoint 中表格的縱橫比
您還可以使用 Java 鎖定 PowerPoint 演示文稿中表格的縱橫比。以下是實現此目的的步驟。
下面的代碼示例展示了如何在 PowerPoint 演示文稿中鎖定表格的縱橫比。
// Load presentation Presentation pres = new Presentation("pres.pptx"); try { // Get reference of the table ITable table = (ITable)pres.getSlides().get_Item(0).getShapes().get_Item(0); System.out.println("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked()); // Lock aspect ratio table.getGraphicalObjectLock().setAspectRatioLocked(!table.getGraphicalObjectLock().getAspectRatioLocked()); // invert System.out.println("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked()); // Save presentation pres.save("pres-out.pptx", SaveFormat.Pptx); } finally { if (pres != null) pres.dispose(); }
如果你想試用Aspose的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn