/// <summary> /// 在固定表格里,按列填充数据 /// </summary> /// <param name="mark">书签名称</param> /// <param name="tableData">表格数据</param> /// <param name="builder">文档构造类</param> private void OnlyFillByColumn(Bookmark mark, string[,] tableData, DocumentBuilder builder) { Aspose.Words.Tables.Table table = (Aspose.Words.Tables.Table)mark.BookmarkStart.GetAncestor(NodeType.Table); Aspose.Words.Tables.Row row = (Aspose.Words.Tables.Row)mark.BookmarkStart.GetAncestor(NodeType.Row); Cell cell = (Cell)mark.BookmarkStart.GetAncestor(NodeType.Cell); NodeCollection allTables = this.doc.GetChildNodes(NodeType.Table, true); int tableIndex = allTables.IndexOf(table); int rowIndex = table.IndexOf(row); int cellIndex = row.IndexOf(cell); int columnIndex = cellIndex; for (int i = 0; i < tableData.GetLength(0) && rowIndex < table.Rows.Count; i++) { columnIndex = cellIndex; for (int j = 0; j < tableData.GetLength(1); j++) { if (columnIndex < row.Cells.Count) { builder.MoveToCell(tableIndex, rowIndex, columnIndex++, 0); // 垂直居中对齐 builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center; // 水平居中对齐 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.Write(tableData[i, j]); } } rowIndex++; } }
protected void MergeCells(Cell startCell, Cell endCell) { Aspose.Words.Tables.Table parentTable = startCell.ParentRow.ParentTable; // Find the row and cell indices for the start and end cell. Point startCellPos = new Point(startCell.ParentRow.IndexOf(startCell), parentTable.IndexOf(startCell.ParentRow)); Point endCellPos = new Point(endCell.ParentRow.IndexOf(endCell), parentTable.IndexOf(endCell.ParentRow)); // Create the range of cells to be merged based off these indices. Inverse each index if the end cell if before the start cell. Rectangle mergeRange = new Rectangle(Math.Min(startCellPos.X, endCellPos.X), Math.Min(startCellPos.Y, endCellPos.Y), Math.Abs(endCellPos.X - startCellPos.X) + 1, Math.Abs(endCellPos.Y - startCellPos.Y) + 1); foreach (Row row in parentTable.Rows) { foreach (Cell cell in row.Cells) { Point currentPos = new Point(row.IndexOf(cell), parentTable.IndexOf(row)); // Check if the current cell is inside our merge range then merge it. if (mergeRange.Contains(currentPos)) { if (mergeRange.Width > 1) { if (currentPos.X == mergeRange.X) { cell.CellFormat.HorizontalMerge = CellMerge.First; } else { cell.CellFormat.HorizontalMerge = CellMerge.Previous; } } if (mergeRange.Height > 1) { if (currentPos.Y == mergeRange.Y) { cell.CellFormat.VerticalMerge = CellMerge.First; } else { cell.CellFormat.VerticalMerge = CellMerge.Previous; } } } } } }