Java使用Spire.Presentation在PowerPoint中添加或刪除表格行與列
引言
在日常開發(fā)中,Java 開發(fā)者常需自動化生成報告PPT,手動編輯PowerPoint 表格行與列效率低下,尤其批量操作時易出錯。Spire.Presentation for Java 庫提供高效解決方案,支持添加行與列、刪除行與列等 PPT 表格操作,無需 Office 依賴?;谧钚?7.24.x 版本,本文分享實用代碼與步驟,助你快速上手。
Spire.Presentation for Java 庫介紹與安裝
Spire.Presentation for Java 是 e-iceblue 推出的專業(yè)PPT處理庫,支持PPTX創(chuàng)建、編輯、轉(zhuǎn)換與表格高級操作,如動態(tài)添加行與列、刪除行與列。最新7.24.x版本優(yōu)化了表格性能,支持大表格批量處理,適用于報表自動化場景。
安裝步驟:
- Maven(推薦):
在pom.xml添加依賴:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>11.1.3</version>
</dependency>
</dependencies>
- 驗證:新建 Java 項目,運行簡單代碼加載 PPTX 確認環(huán)境。
準備就緒后即可操作表格。該庫體積小、API 直觀,遠超手動 VBA。
Java 在 PowerPoint 中添加表格的行與列
添加行與列 核心使用 ITable 接口的 append()、insert() 方法,支持克隆現(xiàn)有行/列快速擴展。
步驟:
- 創(chuàng)建
Presentation對象,加載或新建幻燈片。 - 在幻燈片添加表格:
shapes.appendTable(x, y, widths, heights),或者使用presentation.getSlides().get().getShapes()獲取已存在的表格。 - 添加行:克隆行后
tableRows.append(clonedRow)。 - 添加列:類似
columnsList.append(clonedColumn)。 - 填充數(shù)據(jù),保存 PPTX。
完整代碼示例(添加行與列):
import com.spire.presentation.*;
public class AddRowAndColumn {
public static void main(String[] args) throws Exception {
//加載示例文檔
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\Users\Administrator\Desktop\input.pptx");
//獲取表格
ITable table = null;
for (Object shape : presentation.getSlides().get(0).getShapes()) {
if (shape instanceof ITable) {
table = (ITable) shape;
//將最后一行作為新行添加到表格下方
int rowCount = table.getTableRows().getCount();
TableRow row = table.getTableRows().get(rowCount - 1);
table.getTableRows().append(row);
//獲取新行,并設(shè)置每個單元格的文本
rowCount = table.getTableRows().getCount();
row = table.getTableRows().get(rowCount - 1);
row.get(0).getTextFrame().setText("美國");
row.get(1).getTextFrame().setText("華盛頓");
row.get(2).getTextFrame().setText("北美洲");
row.get(3).getTextFrame().setText("9372610");
//將最后一列作為新列添加到表格右方
int colCount = table.getColumnsList().getCount();
TableColumn column =table.getColumnsList().get(colCount-1);
table.getColumnsList().add(column);
//獲取新列,并設(shè)置每個單元格的文本
colCount = table.getColumnsList().getCount();
column = table.getColumnsList().get(colCount-1);
column.get(0).getTextFrame().setText("人口");
column.get(1).getTextFrame().setText("32370000");
column.get(2).getTextFrame().setText("7350000");
column.get(3).getTextFrame().setText("15040000");
column.get(4).getTextFrame().setText("26500000");
column.get(5).getTextFrame().setText("329740000");
}
}
//保存文檔
presentation.saveToFile("output/AddRowAndColumn.pptx", FileFormat.PPTX_2013);
}
}
常見問題:克隆后記得更新單元格文本,避免數(shù)據(jù)重復。批量添加時用循環(huán),性能優(yōu)異。
Java 在 PowerPoint 中刪除表格的行與列
刪除行與列使用 removeAt(index, isClone),第二個參數(shù) false 表示直接移除。注意索引從0開始,避免越界。
步驟:
- 加載 PPT,獲取
ITable。 - 刪除行:
tableRows.removeAt(rowIndex, false)。 - 刪除列:
columnsList.removeAt(colIndex, false)。 - 處理異常(如空表),保存。
完整代碼示例(刪除第二行和第二列):
import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
public class DeleteRowAndColumn {
public static void main(String[] args) throws Exception {
//加載示例文檔
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\Users\Administrator\Desktop\input.pptx");
//獲取表格
ITable table = null;
for (Object shape : presentation.getSlides().get(0).getShapes()) {
if (shape instanceof ITable) {
table = (ITable) shape;
//刪除第二列
table.getColumnsList().removeAt(1, false);
//刪除第二行
table.getTableRows().removeAt(1, false);
}
}
//保存文檔
presentation.saveToFile("output/DeleteRowAndColumn.pptx", FileFormat.PPTX_2013);
}
}
常見問題:刪除后索引變化,建議倒序遍歷??毡頇z查用 getCount() > index。
總結(jié)
掌握 Spire.Presentation for Java 的在幻燈片添加行與列、刪除行與列操作,可大幅提升處理 PowerPoint 演示文稿的自動化效率。立即運行以上代碼實踐,結(jié)合循環(huán)處理批量報表。
到此這篇關(guān)于Java使用Spire.Presentation在PowerPoint中添加或刪除表格行與列的文章就介紹到這了,更多相關(guān)Java PowerPoint添加或刪除行與列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WebUploader客戶端批量上傳圖片 后臺使用springMVC
這篇文章主要為大家詳細介紹了WebUploader客戶端批量上傳圖片,后臺使用springMVC接收實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09
java編程實現(xiàn)屏幕截圖(截屏)代碼總結(jié)
這篇文章主要介紹了java編程實現(xiàn)屏幕截圖(截屏)代碼,結(jié)合3個實例總結(jié)分析了Java截屏時頁面抓取及圖片保存的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Java結(jié)構(gòu)性設(shè)計模式中的裝飾器模式介紹使用
裝飾器模式又名包裝(Wrapper)模式。裝飾器模式以對客戶端透明的方式拓展對象的功能,是繼承關(guān)系的一種替代方案,本篇文章以虹貓藍兔生動形象的為你帶來詳細講解2022-09-09
Java17中record替代Lombok部分功能使用場景探究
這篇文章主要介紹了使用Java17中的record替代Lombok的部分功能,本文來為大家小小的總結(jié)下,我們可以在哪些地方,利用record來替換Lombok2024-01-01
如何解決springcloud feign 首次調(diào)用100%失敗的問題
這篇文章主要介紹了如何解決springcloud feign 首次調(diào)用100%失敗的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot-JPA刪除不成功,只執(zhí)行了查詢語句問題
這篇文章主要介紹了SpringBoot-JPA刪除不成功,只執(zhí)行了查詢語句問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
MyBatis批量插入大量數(shù)據(jù)(1w以上)
MyBatis進行批量插入數(shù)時,一次性插入超過一千條的時候MyBatis開始報錯,本文主要介紹了MyBatis批量插入大量數(shù)據(jù)的解決方法,感興趣的可以了解一下2022-01-01
Java數(shù)組創(chuàng)建的3種方法6種寫法代碼示例
這篇文章主要給大家介紹了關(guān)于Java數(shù)組創(chuàng)建的3種方法6種寫法,在Java中我們可以使用關(guān)鍵字new來創(chuàng)建一個數(shù)組,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-01-01

