翻譯|使用教程|編輯:李顯亮|2019-10-15 10:25:17.823|閱讀 1297 次
概述:HTML中的表格具有本質上不同的結構:每行具有相同數量的單元格,并且(對于問題而言很重要)每個單元格具有對應列的寬度,同一列中的所有單元格都具有相同的寬度。本文將為大家講解如何水平和垂直合并表格中單元格?。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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 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(); //合并的單元格具有適當的合并標志
ASPOSE技術交流QQ群(642018183)已開通,各類資源及時分享,歡迎交流討論!
如果您對Aspose有任何需求和疑難,記得掃描下方二維碼告訴我們哦~
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn