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

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

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

致改变世界的程序员——限时购买Aspose系列产品最高可享10000元高额减免!更多活动详情可咨询在线客服哦~


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

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

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

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

Document doc = new Document(dataDir + "Table.MergedCells.doc");// Create visitorSpanVisitor visitor = new SpanVisitor(doc);// Accept visitordoc.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 nbsp;0 : Int32.Parse(colSpanAttr.Value);                    int rowSpan = rowSpanAttr == null nbsp;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();   //合并的单元格具有适当的合并标志

ASPOSE技术交流QQ群()已开通,各类资源及时分享,欢迎交流讨论!

如果您对Aspose有任何需求和疑难,记得扫描下方二维码告诉我们哦~

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年9月12日
下一篇 2019年9月12日

相关推荐

发表回复

登录后才能评论