Example #1
0
        public XWPFSDTContentCell(CT_SdtContentCell sdtContentCell,
                                  XWPFTableRow xwpfTableRow, IBody part)
        {
            StringBuilder sb = new StringBuilder();
            

            //keep track of the following,
            //and add "\n" only before the start of a body
            //element if it is not the first body element.

            //index of cell in row
            int tcCnt = 0;
            //count of body objects
            int iBodyCnt = 0;
            int depth = 1;
            string sdtXml = sdtContentCell.ToString();
            using (StringReader sr = new StringReader(sdtXml))
            {
                XmlParserContext context = new XmlParserContext(null, POIXMLDocumentPart.NamespaceManager,
                    null, XmlSpace.Preserve);
                using (XmlReader cursor = XmlReader.Create(sr, null, context))
                {
                    while (cursor.Read() && depth > 0)
                    {
                        if (cursor.NodeType == XmlNodeType.Text)
                        {
                            sb.Append(cursor.ReadContentAsString());
                        }
                        else if (IsStartToken(cursor, "tr"))
                        {
                            tcCnt = 0;
                            iBodyCnt = 0;
                        }
                        else if (IsStartToken(cursor, "tc"))
                        {
                            if (tcCnt++ > 0)
                            {
                                sb.Append("\t");
                            }
                            iBodyCnt = 0;
                        }
                        else if (IsStartToken(cursor, "p") ||
                              IsStartToken(cursor, "tbl") ||
                              IsStartToken(cursor, "sdt"))
                        {
                            if (iBodyCnt > 0)
                            {
                                sb.Append("\n");
                            }
                            iBodyCnt++;
                        }

                    }
                }
            }
            //IEnumerator cursor = sdtContentCell.Items.GetEnumerator();
            //while (cursor.MoveNext() && depth > 0)
            //{
            //    //TokenType t = cursor.ToNextToken();
            //    object t = cursor.Current;
            //    if (t is CT_Text)//??
            //    {
            //        //sb.Append(cursor.TextValue);
            //    }
            //    else if (IsStartToken(cursor, "tr"))
            //    {
            //        tcCnt = 0;
            //        iBodyCnt = 0;
            //    }
            //    else if (IsStartToken(cursor, "tc"))
            //    {
            //        if (tcCnt++ > 0)
            //        {
            //            sb.Append("\t");
            //        }
            //        iBodyCnt = 0;
            //    }
            //    else if (IsStartToken(cursor, "p") ||
            //          IsStartToken(cursor, "tbl") ||
            //          IsStartToken(cursor, "sdt"))
            //    {
            //        if (iBodyCnt > 0)
            //        {
            //            sb.Append("\n");
            //        }
            //        iBodyCnt++;
            //    }
            //    //if (cursor.IsStart())
            //    //{
            //    //    depth++;
            //    //}
            //    //else if (cursor.IsEnd())
            //    //{
            //    //    depth--;
            //    //}
            //}
            text = sb.ToString();
        }
Example #2
0
        public XWPFSDTContentCell(CT_SdtContentCell sdtContentCell,
                                  XWPFTableRow xwpfTableRow, IBody part)
        {
            StringBuilder sb = new StringBuilder();

            //keep track of the following,
            //and add "\n" only before the start of a body
            //element if it is not the first body element.

            //index of cell in row
            int tcCnt = 0;
            //count of body objects
            int    iBodyCnt = 0;
            int    depth    = 1;
            string sdtXml   = sdtContentCell.ToString();

            using (StringReader sr = new StringReader(sdtXml))
            {
                XmlParserContext context = new XmlParserContext(null, POIXMLDocumentPart.NamespaceManager,
                                                                null, XmlSpace.Preserve);
                using (XmlReader cursor = XmlReader.Create(sr, null, context))
                {
                    while (cursor.Read() && depth > 0)
                    {
                        if (cursor.NodeType == XmlNodeType.Text)
                        {
                            sb.Append(cursor.ReadContentAsString());
                        }
                        else if (IsStartToken(cursor, "tr"))
                        {
                            tcCnt    = 0;
                            iBodyCnt = 0;
                        }
                        else if (IsStartToken(cursor, "tc"))
                        {
                            if (tcCnt++ > 0)
                            {
                                sb.Append("\t");
                            }
                            iBodyCnt = 0;
                        }
                        else if (IsStartToken(cursor, "p") ||
                                 IsStartToken(cursor, "tbl") ||
                                 IsStartToken(cursor, "sdt"))
                        {
                            if (iBodyCnt > 0)
                            {
                                sb.Append("\n");
                            }
                            iBodyCnt++;
                        }
                    }
                }
            }
            //IEnumerator cursor = sdtContentCell.Items.GetEnumerator();
            //while (cursor.MoveNext() && depth > 0)
            //{
            //    //TokenType t = cursor.ToNextToken();
            //    object t = cursor.Current;
            //    if (t is CT_Text)//??
            //    {
            //        //sb.Append(cursor.TextValue);
            //    }
            //    else if (IsStartToken(cursor, "tr"))
            //    {
            //        tcCnt = 0;
            //        iBodyCnt = 0;
            //    }
            //    else if (IsStartToken(cursor, "tc"))
            //    {
            //        if (tcCnt++ > 0)
            //        {
            //            sb.Append("\t");
            //        }
            //        iBodyCnt = 0;
            //    }
            //    else if (IsStartToken(cursor, "p") ||
            //          IsStartToken(cursor, "tbl") ||
            //          IsStartToken(cursor, "sdt"))
            //    {
            //        if (iBodyCnt > 0)
            //        {
            //            sb.Append("\n");
            //        }
            //        iBodyCnt++;
            //    }
            //    //if (cursor.IsStart())
            //    //{
            //    //    depth++;
            //    //}
            //    //else if (cursor.IsEnd())
            //    //{
            //    //    depth--;
            //    //}
            //}
            text = sb.ToString();
        }