翻譯|使用教程|編輯:李顯亮|2021-09-24 10:15:56.203|閱讀 221 次
概述:MS PowerPoint 還允許演示者在演示文稿中創(chuàng)建表格。因此,在本文中,將學(xué)習(xí)如何使用 Java 在 PowerPoint 演示文稿中創(chuàng)建和操作表格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
表格用于以行和列的形式很好地組織數(shù)據(jù)。此外,它們匯總了要查看和分析的數(shù)據(jù)。MS PowerPoint 還允許演示者在演示文稿中創(chuàng)建表格。因此,在本文中,將學(xué)習(xí)如何使用 Java 在 PowerPoint 演示文稿中創(chuàng)建和操作表格。
為了在 PowerPoint 演示文稿中創(chuàng)建和操作表格,我們將使用Aspose.Slides for Java,該 API 旨在創(chuàng)建、操作和轉(zhuǎn)換 PowerPoint 和 OpenOffice 演示文稿。
>>你可以點擊這里下載Aspose.Slides 最新版測試體驗。
使用 Aspose.Slides for Java 創(chuàng)建表格就像餡餅一樣簡單。以下步驟演示了如何使用 Java 從頭開始在 PowerPoint 演示文稿中創(chuàng)建表格。
以下代碼示例展示了如何在 PowerPoint 演示文稿中創(chuàng)建表格。
// 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(); }
以下屏幕截圖顯示了我們使用上述代碼創(chuàng)建的表。
還可以訪問現(xiàn)有 PowerPoint 演示文稿中的表格并根據(jù)需要對其進行操作。以下是訪問演示文稿中表格的步驟。
以下代碼示例展示了如何使用 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(); }
Aspose.Slides for Java 還允許您非常輕松地設(shè)置表格的格式,如下面的步驟所示。
以下代碼示例展示了如何使用 Java 在 PowerPoint 中設(shè)置表格的格式。
// 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(); }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn