zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Aspose.Words for .NET使用表格教程之水平和垂直合并表格中单元格

Net教程 for 合并 表格 水平 垂直 单元格
2023-09-11 14:14:49 时间

Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

接下来我们将进入“使用格式”的介绍,其中包括应用格式、介绍和创建表、添加和拆分表以及使用列和行。本文将为大家讲解如何水平和垂直合并表格中单元格。

>>Aspose.Words for .NET更新至最新版,欢迎下载体验

 欢迎下载|体验更多Aspose文档管理产品  或 加入Aspose技术交流群(761297826)


表格中的垂直和水平合并单元格

MS Word中的表是一组独立的行。每行具有一组独立于其他行的单元格的单元格。因此,MS Word的表中没有逻辑“列”。“第一列”类似于“表中每一行的一组第一单元格”。 例如,可能有一个表格,其中第一行包含两个单元格:2cm和1cm,第二行包含不同的两个单元格:宽度为1cm和2cm。

HTML中的表格具有本质上不同的结构:每行具有相同数量的单元格,并且(对于问题而言很重要)每个单元格具有对应列的宽度,同一列中的所有单元格都具有相同的宽度。

如果CellFormat.HorizontalMerge和CellFormat.VerticalMerge返回不正确的值,请使用下面的代码示例。下面的示例演示单元格的水平和垂直合并。

Document doc = new Document(dataDir + "Table.MergedCells.doc");

// Create visitor
SpanVisitor visitor = new SpanVisitor(doc);

// Accept visitor
doc.Accept(visitor);


/// <summary>
/// Helper class that contains collection of rowinfo for each row
/// </summary>
public class TableInfo
{
    public List<RowInfo> Rows
    {
        get { return mRows; }
    }

    private List<RowInfo> mRows = new List<RowInfo>();
}

/// <summary>
/// Helper class that contains collection of cellinfo for each cell
/// </summary>
public class RowInfo
{
    public List<CellInfo> Cells
    {
        get { return mCells; }
    }

    private List<CellInfo> mCells = new List<CellInfo>();
}

/// <summary>
/// Helper class that contains info about cell. currently here is only colspan and rowspan
/// </summary>
public class CellInfo
{
    public CellInfo(int colSpan, int rowSpan)
    {
        mColSpan = colSpan;
        mRowSpan = rowSpan;
    }

    public int ColSpan
    {
        get { return mColSpan; }
    }

    public int RowSpan
    {
        get { return mRowSpan; }
    }

    private int mColSpan = 0;
    private int mRowSpan = 0;
}

public class SpanVisitor : DocumentVisitor
{

    /// <summary>
    /// Creates new SpanVisitor instance
    /// </summary>
    /// <param name="doc">Is document which we should parse</param>
    public SpanVisitor(Document doc)
    {
        //从文档中获取表的集合
        mWordTables = doc.GetChildNodes(NodeType.Table, true);

        //将文档转换为HTML
        //我们将解析HTML以确定每个单元格的rowpan和colspan
        MemoryStream htmlStream = new MemoryStream();

        HtmlSaveOptions options = new HtmlSaveOptions();
        options.ImagesFolder = Path.GetTempPath();

        doc.Save(htmlStream, options);

        //将 HTML加载到XML文档中
        XmlDocument xmlDoc = new XmlDocument();
        htmlStream.Position = 0;
        xmlDoc.Load(htmlStream);

        //获取HTML文档中的表集合
        XmlNodeList tables = xmlDoc.DocumentElement.SelectNodes("// Table");

        foreach (XmlNode table in tables)
        {
            TableInfo tableInf = new TableInfo();
            //获取表中的行集合
            XmlNodeList rows = table.SelectNodes("tr");

            foreach (XmlNode row in rows)
            {
                RowInfo rowInf = new RowInfo();

                //获取单元格的集合
                XmlNodeList cells = row.SelectNodes("td");

                foreach (XmlNode cell in cells)
                {
                    //确定当前单元格的行跨度和列跨度
                    XmlAttribute colSpanAttr = cell.Attributes["colspan"];
                    XmlAttribute rowSpanAttr = cell.Attributes["rowspan"];

                    int colSpan = colSpanAttr == null ? 0 : Int32.Parse(colSpanAttr.Value);
                    int rowSpan = rowSpanAttr == null ? 0 : Int32.Parse(rowSpanAttr.Value);

                    CellInfo cellInf = new CellInfo(colSpan, rowSpan);
                    rowInf.Cells.Add(cellInf);
                }

                tableInf.Rows.Add(rowInf);
            }

            mTables.Add(tableInf);
        }
    }

    public override VisitorAction VisitCellStart(Cell cell)
    {
        // 确定当前表的索引
        int tabIdx = mWordTables.IndexOf(cell.ParentRow.ParentTable);

        //确定当前行的索引
        int rowIdx = cell.ParentRow.ParentTable.IndexOf(cell.ParentRow);

        //确定当前单元格的索引
        int cellIdx = cell.ParentRow.IndexOf(cell);

        //确定当前单元格的colspan和rowpan
        int colSpan = 0;
        int rowSpan = 0;
        if (tabIdx < mTables.Count &&
            rowIdx < mTables[tabIdx].Rows.Count &&
            cellIdx < mTables[tabIdx].Rows[rowIdx].Cells.Count)
        {
            colSpan = mTables[tabIdx].Rows[rowIdx].Cells[cellIdx].ColSpan;
            rowSpan = mTables[tabIdx].Rows[rowIdx].Cells[cellIdx].RowSpan;
        }

        Console.WriteLine("{0}.{1}.{2} colspan={3}\t rowspan={4}", tabIdx, rowIdx, cellIdx, colSpan, rowSpan);

        return VisitorAction.Continue;
    }
    private List<TableInfo> mTables = new List<TableInfo>();
    private NodeCollection mWordTables = null;
}

转换为水平合并的单元格

在最新版本的MS Word中,单元格按其宽度水平合并。而合并标志是在较旧的技术中使用的,例如Cell.CellFormat.HorizontalMerge。当单元按其宽度水平合并时,将不使用合并标志  ,并且也无法检测到哪些单元被合并。Aspose.Words提供ConvertToHorizontallyMergedCells方法,以将按其宽度水平合并的单元格转换为通过标志水平合并的单元格。它只是  转换表并在需要时添加新的单元格。下面的代码示例演示上述方法的工作。

Document doc = new Document();

Table table = doc.FirstSection.Body.Tables[0];
table.ConvertToHorizontallyMergedCells();   //合并的单元格具有适当的合并标志