示例#1
0
        private static List <LoopDefine> ParseLoops(XWPFDocument doc)
        {
            List <LoopDefine> loops   = new List <LoopDefine>();
            LoopDefine        curLoop = null;

            foreach (XWPFParagraph ph in GetAllParagraphs(doc))
            {
                string text = ph.Text;
                if (String.IsNullOrEmpty(text))
                {
                    continue;
                }

                if (text.StartsWith("«Loop:", true, null) && text.EndsWith("»"))
                {
                    int    index     = text.IndexOf(':');
                    string tableName = text.Substring(index + 1, text.Length - (index + 2));

                    if (curLoop == null)
                    {
                        curLoop                = new LoopDefine();
                        curLoop.Identify       = text;
                        curLoop.StartParagraph = ph;
                        curLoop.TableName      = tableName;
                    }
                    else
                    {
                        if (String.Compare(curLoop.Identify, text, true) == 0)
                        {
                            loops.Add(curLoop);
                            curLoop.EndParagraph = ph;
                            curLoop = null;
                        }
                        else
                        {
                            curLoop                = new LoopDefine();
                            curLoop.Identify       = text;
                            curLoop.StartParagraph = ph;
                            curLoop.TableName      = tableName;
                        }
                    }
                }
                else
                {
                    if (curLoop != null)
                    {
                        curLoop.Templates.Add(ph);
                    }
                }
            }

            return(loops);
        }
示例#2
0
        private static void FillLoop(XWPFDocument doc, LoopDefine loop, DataTable table)
        {
            foreach (DataRow row in table.Rows)
            {
                foreach (XWPFParagraph templateph in loop.Templates)
                {
                    XWPFParagraph ph = doc.CreateParagraph();

                    int paragraphPos = doc.Paragraphs.IndexOf(loop.Templates[0]);

                    //公开的API中信息不全
                    //修改位置
                    FieldInfo propertyCtDocument = typeof(XWPFDocument).GetField("ctDocument", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
                    if (propertyCtDocument != null)
                    {
                        CT_Document ctDocument = propertyCtDocument.GetValue(doc) as CT_Document;
                        if (ctDocument != null)
                        {
                            ArrayList items = ctDocument.body.Items;
                            List <DocumentBodyItemChoiceType> names = ctDocument.body.ItemsElementName;

                            int pos       = doc.Paragraphs.IndexOf(loop.Templates[0]);
                            int globalPos = GetObjectIndex(names, DocumentBodyItemChoiceType.p, paragraphPos);

                            items[globalPos] = items[items.Count - 1];
                            names[globalPos] = names[items.Count - 1];
                            for (int i = items.Count - 2; i >= globalPos; i--)
                            {
                                items[i + 1] = items[i];
                                names[i + 1] = names[i];
                            }
                        }
                    }

                    //修改列表中的位置
                    FieldInfo propertyParagraphs   = typeof(XWPFDocument).GetField("paragraphs", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
                    FieldInfo propertyBodyElements = typeof(XWPFDocument).GetField("bodyElements", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
                    if (propertyParagraphs != null && propertyBodyElements != null)
                    {
                        List <XWPFParagraph> paragraphs   = propertyParagraphs.GetValue(doc) as List <XWPFParagraph>;
                        List <IBodyElement>  bodyElements = propertyBodyElements.GetValue(doc) as List <IBodyElement>;

                        paragraphs.Remove(ph);
                        bodyElements.Remove(ph);
                        paragraphs.Insert(paragraphs.IndexOf(loop.Templates[0]), ph);
                        bodyElements.Insert(bodyElements.IndexOf(loop.Templates[0]), ph);
                    }

                    WordGenerator.CopyParagraph(templateph, ph);

                    foreach (XWPFRun run in ph.Runs)
                    {
                        WordGenerator.MergeField(run, row);
                    }
                }
            }

            //删除模板
            doc.RemoveBodyElement(doc.BodyElements.IndexOf(loop.StartParagraph));
            doc.RemoveBodyElement(doc.BodyElements.IndexOf(loop.EndParagraph));
            foreach (XWPFParagraph template in loop.Templates)
            {
                doc.RemoveBodyElement(doc.BodyElements.IndexOf(template));
            }
        }