依賴: <dependency><groupId>com.techCoLtd</groupId><artifactId>aspose-words-16.4.0-jdk16</artifactId><classifier>jdk16</classifier> </dependency>/*** 刪除表格及表格的行* @throws Exception*/ private static void deletedTable() throws Exception {Document doc = new Document("表格路徑");Table table = doc.getFirstSection().getBody().getTables().get(0);// 刪除整個表的方法table.remove();// 刪除某一行 的方法if (table.getRows().getCount() > 1) { // 確保至少有兩行才進行刪除操作table.getRows().removeAt(0);// 刪除第一行}doc.save("D:\\work\\save\\ceshi.docx"); }
/**** 讀取表格的內容* @throws Exception*/ public static void readTableTxtByIndex() throws Exception {Document doc = new Document("表格路徑");//獲取所有的表格NodeCollection tableNodes = doc.getChildNodes(NodeType.TABLE, true);int tableCount = tableNodes.getCount();System.out.println("表格數量:{}"+ tableCount);for (int i = 0; i < tableCount; i++) {Table table = (Table)tableNodes.get(i);//按表格索引 單獨寫邏輯進行數據的讀取 本示例中就1個表格 因此就放1個邏輯進行解析了if(0 == i){//獲取表格中的所有行節點RowCollection rows = table.getRows();int rowCount = rows.getCount();System.out.println("共有表格行:{}" + rowCount);//獲取每一行的單元格節點for (int j = 0; j < rowCount; j++) {Row row = rows.get(j);CellCollection cells = row.getCells();int cellsCount = cells.getCount();System.out.println("第" + j + "行有" + rowCount + "個單元格{}");for (int k = 0; k < cellsCount; k++) {Cell cell = cells.get(k);String cellText = cell.getText();System.out.println("第" + j + "行 第" + k + "個單元格的文本:{}" + cellText);}}}} }