C# Aspose 读取word表格单元格中段落文本

由于Aspose读取时会把批注,修订都显示在一起,虽然para.runs里有明确指定类型,但是要想直接取出单元格中的纯文字也是挺麻烦的,如果大家有简便方式,请留言给我。谢谢

        /// 
        /// word中获取表格单元格的段落值
        /// 
        /// 
        /// 
        public static string GetCellText(Aspose.Words.Tables.Cell cell)
        {
            var oldText = new StringBuilder();
            cell.Paragraphs.Cast().ToList().ForEach(para =>
            {
                oldText.Append(getText(para));
            });
            return oldText.ToString();
        }


        /// 
        /// word中获取段落的实际文本信息
        /// 
        /// 
        /// 
        public static string GetParaText(Paragraph para)
        {
            var oldText = new StringBuilder();
            oldText.Append(getText(para));
            return oldText.ToString();
        }


        /// 
        /// 获取段落公共方法
        /// 
        /// 
        /// 
        public static string getText(Paragraph para) {
            StringBuilder builder = new StringBuilder();

            bool inField = false;
            bool FiledContent = false;
            foreach (Node item in para.ChildNodes)
            {
                if (inField)
                {
                    if (FiledContent)
                    {
                        if (item.NodeType == NodeType.FieldEnd)
                        {
                            FiledContent = false;
                        }
                        else
                        {
                            builder.Append(item.GetText());
                        }
                    }

                    if (item.NodeType == NodeType.FieldSeparator)
                    {
                        FiledContent = true;
                    }
                    else if (item.NodeType == NodeType.FieldEnd)
                    {
                        inField = false;
                    }
                    continue;
                }
                if (item.NodeType == NodeType.FieldStart )
                {
                    inField = true;
                }
                else if(item.NodeType == NodeType.Run)
                {
                    if (!(item as Aspose.Words.Run).IsDeleteRevision)
                    {
                        builder.Append(item.GetText());
                    }
                }
            }
            return builder.ToString().Replace("\v", "");
        }

你可能感兴趣的:(word操作,c#,word,开发语言)