Làm việc với cột và hàng
Để có quyền kiểm soát hơn về cách các bảng hoạt động, hãy tìm hiểu cách thao tác cột và hàng.
Tìm chỉ số phần tử bảng
Sọc, hàng và tế bào được quản lý bằng cách truy cập vào nút tài liệu được chọn theo chỉ số của nó. Tìm chỉ số của một nút nào đó bao gồm việc thu thập tất cả các nút con của loại phần tử từ nút cha, và sau đó sử dụng phương pháp IndexOf để tìm chỉ số của nút mong muốn trong bộ sưu tập.
Tìm Chỉ số của một Bảng trong một Tài liệu
Đôi khi bạn có thể cần phải thay đổi một bảng nhất định trong một văn bản. Để thực hiện điều này, bạn có thể tham chiếu đến một bảng bằng chỉ số của nó.
Mã ví dụ sau cho thấy cách lấy chỉ số của một bảng trong tài liệu:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); | |
NodeCollection allTables = doc.getChildNodes(NodeType.TABLE, true); | |
int tableIndex = allTables.indexOf(table); |
Tìm Chỉ Số của một Hàng trong Bảng
Tương tự như vậy, bạn có thể cần phải thực hiện những thay đổi đến một hàng cụ thể trong bảng được chọn. Để làm được điều đó, bạn cũng có thể tham khảo một hàng bằng chỉ số của nó.
Mã ví dụ sau cho thấy cách lấy chỉ số của một hàng trong bảng:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
int rowIndex = table.indexOf(table.getLastRow()); |
Tìm Chỉ số của một Ô trong một Hàng
Cuối cùng, bạn có thể cần thay đổi một ô cụ thể và bạn có thể làm điều này bằng chỉ mục của nó cũng vậy.
Ví dụ mã sau cho thấy cách lấy chỉ mục của một ô trong một hàng là như thế nào:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
int cellIndex = row.indexOf(row.getCells().get(4)); |
Làm việc với cột
Trong Aspose.Words Document Object Model (DOM) nút Table bao gồm Row nút và sau đó là Cell nút. Vì vậy, trong Document
Object Model của Aspose.Words, như trong các tài liệu Word, không có khái niệm về một cột.
Do thiết kế, các hàng trong Microsoft Word và Aspose.Words là hoàn toàn độc lập, và những tính chất cơ bản và hoạt động chỉ chứa được trong các hàng và tế bào của bảng. Đánh dấu này cho phép bảng có một số thuộc tính thú vị:
- Mỗi hàng bảng có thể có số lượng tế bào hoàn toàn khác nhau Tia dọc, các tế bào của mỗi hàng có thể có độ rộng khác nhau
- Có thể kết hợp bảng với định dạng hàng khác nhau và số lượng ô
Tất cả các thao tác thực hiện trên cột thực sự là" phím tắt “thực hiện thao tác bằng cách thay đổi hàng theo cách mà nó trông giống như đang được áp dụng cho cột. Đó là bạn có thể thực hiện các thao tác trên cột bằng cách lặp lại đơn giản chỉ trên cùng một bảng hàng ô.
Mã ví dụ sau đơn giản hóa các hoạt động bằng cách cung cấp lớp khuôn mặt mà thu thập các tế bào tạo nên một “cột” của bảng:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
/// <summary> | |
/// Represents a facade object for a column of a table in a Microsoft Word document. | |
/// </summary> | |
static class Column | |
{ | |
private Column(Table table, int columnIndex) { | |
if (table != null) { | |
mTable = table; | |
} else { | |
throw new IllegalArgumentException("table"); | |
} | |
mColumnIndex = columnIndex; | |
} | |
/// <summary> | |
/// Returns a new column facade from the table and supplied zero-based index. | |
/// </summary> | |
public static Column fromIndex(Table table, int columnIndex) | |
{ | |
return new Column(table, columnIndex); | |
} | |
private ArrayList<Cell> getCells() { | |
return getColumnCells(); | |
} | |
/// <summary> | |
/// Returns the index of the given cell in the column. | |
/// </summary> | |
public int indexOf(Cell cell) | |
{ | |
return getColumnCells().indexOf(cell); | |
} | |
/// <summary> | |
/// Inserts a brand new column before this column into the table. | |
/// </summary> | |
public Column insertColumnBefore() | |
{ | |
ArrayList<Cell> columnCells = getCells(); | |
if (columnCells.size() == 0) | |
throw new IllegalArgumentException("Column must not be empty"); | |
// Create a clone of this column. | |
for (Cell cell : columnCells) | |
cell.getParentRow().insertBefore(cell.deepClone(false), cell); | |
// This is the new column. | |
Column column = new Column(columnCells.get(0).getParentRow().getParentTable(), mColumnIndex); | |
// We want to make sure that the cells are all valid to work with (have at least one paragraph). | |
for (Cell cell : column.getCells()) | |
cell.ensureMinimum(); | |
// Increase the index which this column represents since there is now one extra column in front. | |
mColumnIndex++; | |
return column; | |
} | |
/// <summary> | |
/// Removes the column from the table. | |
/// </summary> | |
public void remove() | |
{ | |
for (Cell cell : getCells()) | |
cell.remove(); | |
} | |
/// <summary> | |
/// Returns the text of the column. | |
/// </summary> | |
public String toTxt() throws Exception | |
{ | |
StringBuilder builder = new StringBuilder(); | |
for (Cell cell : getCells()) | |
builder.append(cell.toString(SaveFormat.TEXT)); | |
return builder.toString(); | |
} | |
/// <summary> | |
/// Provides an up-to-date collection of cells which make up the column represented by this facade. | |
/// </summary> | |
private ArrayList<Cell> getColumnCells() | |
{ | |
ArrayList<Cell> columnCells = new ArrayList<Cell>(); | |
for (Row row : mTable.getRows()) | |
{ | |
Cell cell = row.getCells().get(mColumnIndex); | |
if (cell != null) | |
columnCells.add(cell); | |
} | |
return columnCells; | |
} | |
private int mColumnIndex; | |
private Table mTable; | |
} |
Mã ví dụ dưới đây cho thấy cách chèn cột trống vào một bảng:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Tables.docx"); | |
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); | |
Column column = Column.fromIndex(table, 0); | |
// Print the plain text of the column to the screen. | |
System.out.println(column.toTxt()); | |
// Create a new column to the left of this column. | |
// This is the same as using the "Insert Column Before" command in Microsoft Word. | |
Column newColumn = column.insertColumnBefore(); | |
for (Cell cell : newColumn.getColumnCells()) | |
cell.getFirstParagraph().appendChild(new Run(doc, "Column Text " + newColumn.indexOf(cell))); |
Mã ví dụ sau cho thấy cách loại bỏ một cột từ một bảng trong tài liệu:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Tables.docx"); | |
Table table = (Table) doc.getChild(NodeType.TABLE, 1, true); | |
Column column = Column.fromIndex(table, 2); | |
column.remove(); |
Chỉ định hàng làm tiêu đề hàng
Bạn có thể chọn lặp lại hàng đầu tiên trong bảng chỉ như hàng tiêu đề trên trang đầu tiên hoặc trên mỗi trang nếu bảng được chia thành nhiều. Trong Aspose.Words, bạn có thể lặp lại hàng tiêu đề trên mỗi trang sử dụng thuộc tính HeadingFormat.
Bạn có thể đánh dấu nhiều hàng tiêu đề nếu các dòng như vậy nằm liên tiếp đầu bảng. Để làm điều này, bạn cần áp dụng các HeadingFormat thuộc tính cho các hàng này.
Mã ví dụ sau cho thấy cách xây dựng một bảng bao gồm các hàng tiêu đề lặp lại trên các trang tiếp theo:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.startTable(); | |
builder.getRowFormat().setHeadingFormat(true); | |
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); | |
builder.getCellFormat().setWidth(100.0); | |
builder.insertCell(); | |
builder.writeln("Heading row 1"); | |
builder.endRow(); | |
builder.insertCell(); | |
builder.writeln("Heading row 2"); | |
builder.endRow(); | |
builder.getCellFormat().setWidth(50.0); | |
builder.getParagraphFormat().clearFormatting(); | |
for (int i = 0; i < 50; i++) | |
{ | |
builder.insertCell(); | |
builder.getRowFormat().setHeadingFormat(false); | |
builder.write("Column 1 Text"); | |
builder.insertCell(); | |
builder.write("Column 2 Text"); | |
builder.endRow(); | |
} | |
doc.save(getArtifactsDir() + "WorkingWithTables.RepeatRowsOnSubsequentPages.docx"); |
Giữ cho các bảng và hàng không bị tách ra trên các trang
Có lúc nào đó nội dung của một bảng không nên được chia trên nhiều trang. Ví dụ nếu một tiêu đề ở trên bảng, tiêu đề và bảng nên luôn được giữ lại cùng một trang để bảo tồn sự xuất hiện đúng đắn.
Có hai kỹ thuật riêng biệt có ích để đạt được chức năng này:
Allow row break across pages
được áp dụng cho các hàng bảngKeep with next
, được áp dụng cho các đoạn văn trong bảng ô
Theo mặc định, các thuộc tính ở trên là vô hiệu.

Giữ hàng khỏi phá vỡ trên trang
Đây liên quan đến việc hạn chế nội dung bên trong các tế bào của một hàng từ bị phân chia trên trang. Trong Microsoft Word, điều này có thể được tìm thấy dưới Table Properties với tùy chọn “Allow row to break across pages. Trong Aspose.Words, điều này được tìm thấy dưới đối tượng RowFormat của một Row với thuộc tính RowFormat.AllowBreakAcrossPages.

Mã ví dụ sau cho thấy cách vô hiệu hóa việc ngắt dòng trên trang cho từng hàng trong bảng:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Table spanning two pages.docx"); | |
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); | |
// Disable breaking across pages for all rows in the table. | |
for (Row row : (Iterable<Row>) table.getRows()) | |
row.getRowFormat().setAllowBreakAcrossPages(false); | |
doc.save(getArtifactsDir() + "WorkingWithTables.RowFormatDisableBreakAcrossPages.docx"); |
Giữ cho bảng không bị vỡ giữa các trang
Để ngăn chặn bảng bị chia theo trang chúng ta cần phải chỉ định rằng chúng ta muốn nội dung chứa bên trong bảng được ở lại cùng nhau.
Để thực hiện điều này, Aspose.Words sử dụng một phương pháp cho phép người dùng chọn một bảng và bật tham số KeepWithNext để true cho mỗi đoạn văn trong các ô của bảng. Ngoại lệ là đoạn văn cuối cùng trong bảng, nên được đặt thành false.

Mã ví dụ sau cho thấy cách thiết lập một bảng để giữ lại cùng trên trang:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Table spanning two pages.docx"); | |
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); | |
// We need to enable KeepWithNext for every paragraph in the table to keep it from breaking across a page, | |
// except for the last paragraphs in the last row of the table. | |
for (Cell cell : (Iterable<Cell>) table.getChildNodes(NodeType.CELL, true)) | |
{ | |
cell.ensureMinimum(); | |
for (Paragraph para : (Iterable<Paragraph>) cell.getParagraphs()) | |
if (!(cell.getParentRow().isLastRow() && para.isEndOfCell())) | |
para.getParagraphFormat().setKeepWithNext(true); | |
} | |
doc.save(getArtifactsDir() + "WorkingWithTables.KeepTableTogether.docx"); |