示例#1
0
        private static void processDataControl(WordprocessingDocument document, DCTWordDataObject wdo, List <int> ignoreControls)
        {
            var AllSdtElements = document.MainDocumentPart.Document.Body.Descendants <SdtElement>().ToList();

            foreach (SdtElement se in AllSdtElements)
            {
                var curAlias = se.SdtProperties.Descendants <SdtAlias>().ToList();
                if (curAlias.Count == 0)
                {
                    continue;
                }
                var curId = se.SdtProperties.Descendants <SdtId>().ToList();
                if (curId.Count == 0)
                {
                    continue;
                }
                //如果控件在黑名单中,忽略
                if (curAlias[0].Val == null || ignoreControls.Contains(curId[0].Val.Value))
                {
                    continue;
                }
                //生成简单类型
                DCTSimpleProperty sp = new DCTSimpleProperty()
                {
                    TagID = curAlias[0].Val.Value, Value = se.InnerText
                };
                wdo.PropertyCollection.Add(sp);
            }
        }
示例#2
0
        private static void CopyPageFillTemplate(DataRowView drv, Body wordBody, SimplePropertyCollection templateSimples)
        {
            foreach (SdtElement fillItem in wordBody.Descendants <SdtElement>())
            {
                foreach (SdtAlias sdtAlias in fillItem.Descendants <SdtAlias>())
                {
                    DCTSimpleProperty dcts = templateSimples[sdtAlias.Val];
                    if (dcts != null)
                    {
                        var items = fillItem.Descendants <Run>();
                        if (items.Count() > 0)
                        {
                            Run runElement = items.First();
                            runElement.RemoveAllChildren <Text>();
                            runElement.AppendChild <Text>(new Text(GeneralFormatter.ToString(drv[dcts.TagID], dcts.FormatString)));

                            if (dcts.IsReadOnly)
                            {
                                Lock lockControl = new Lock();
                                lockControl.Val = LockingValues.SdtContentLocked;
                                fillItem.SdtProperties.Append(lockControl);
                            }
                        }
                    }
                }
            }
        }
示例#3
0
        private static void ClonePageAndFill(DataRowView drv, OpenXmlElement item, ref OpenXmlElement addItem, SimplePropertyCollection templateSimples)
        {
            foreach (SdtElement sdtElement in item.Descendants <SdtElement>())
            {
                foreach (SdtAlias ite11 in sdtElement.Descendants <SdtAlias>())
                {
                    DCTSimpleProperty temSimple = templateSimples[ite11.Val];
                    if (temSimple != null)
                    {
                        var items = sdtElement.Descendants <Run>();
                        if (items.Count() > 0)
                        {
                            Run runElement = items.First();
                            runElement.RemoveAllChildren <Text>();
                            runElement.AppendChild <Text>(new Text(GeneralFormatter.ToString(drv[temSimple.TagID], temSimple.FormatString)));

                            if (temSimple.IsReadOnly)
                            {
                                Lock lockControl = new Lock();
                                lockControl.Val = LockingValues.SdtContentLocked;
                                sdtElement.SdtProperties.Append(lockControl);
                            }
                        }
                    }
                }
            }
        }
        private static DCTSimpleProperty ConvertToSimpleProperty(PropertyInfo propInfo,object source,string tagID)
        {
            ExceptionHelper.TrueThrow(propInfo == null, "DCTSimpleProperty源对象不能为空");

            DCTSimpleProperty simpleProp = null;
            var value = propInfo.GetValue(source,null);
            Type type = value.GetType();
            PropertyInfo[] propInfos = type.GetProperties();
            object[] attrs = propInfo.GetCustomAttributes(typeof(WordPropertyAttribute), true);
            
            if (attrs.Length > 0)
            {
                simpleProp = new DCTSimpleProperty();
                WordPropertyAttribute attr = (WordPropertyAttribute)attrs[0];
                simpleProp.TagID = string.IsNullOrEmpty(tagID) ? tagID : attr.TagID;
                simpleProp.FormatString = attr.FormatString;
                simpleProp.Value = propInfo.GetValue(source, null);
            }
                //WordPropertyAttribute attrs = attrs.Where(p => p is WordPropertyAttribute).ToArray();
            return simpleProp;
        }
示例#5
0
        public override void Process()
        {
            var titleRow = document.MainDocumentPart.Document.Body
                           .Descendants <TableRow>().Where(o => o.Descendants <BookmarkStart>().Any(mark => mark.Name == DataProperty.TagID)).FirstOrDefault();

            var sdtElements = titleRow.Descendants <SdtElement>().Where(o => o.SdtProperties.Descendants <SdtAlias>().Any(a => a.Val != null)).ToList();

            var bookmarkStart = titleRow.Descendants <BookmarkStart>().Where(o => o.Name == DataProperty.TagID).FirstOrDefault();

            List <string> properties = new List <string>();

            for (int i = 0; i < sdtElements.Count; i++)
            {
                properties.Add(sdtElements[i].Descendants <SdtAlias>().FirstOrDefault().Val.Value);
            }

            TableRow curRow = titleRow;

            foreach (DCTWordDataObject wordDataObj in DataProperty.DataObjects)
            {
                TableRow newRow = curRow.InsertAfterSelf <TableRow>((TableRow)curRow.NextSibling <TableRow>().CloneNode(true));

                TableCell firstCell = newRow.GetFirstChild <TableCell>();

                for (int j = bookmarkStart.ColumnFirst; j <= bookmarkStart.ColumnLast; j++)
                {
                    DCTDataProperty dataProperty = wordDataObj.PropertyCollection[properties[j - bookmarkStart.ColumnFirst]];
                    if (dataProperty is DCTSimpleProperty)
                    {
                        DCTSimpleProperty simpleProperty = dataProperty as DCTSimpleProperty;
                        TableCell         cell           = getCellByIndex(firstCell, j);
                        Paragraph         p = cell.Descendants <Paragraph>().FirstOrDefault();
                        p.RemoveAllChildren();
                        p.AppendChild <Run>(new Run(new Text((GeneralFormatter.ToString(simpleProperty.Value, simpleProperty.FormatString)))));
                    }
                }

                curRow = newRow;
            }
        }
示例#6
0
        private static DCTSimpleProperty ConvertToSimpleProperty(PropertyInfo propInfo, object source, string tagID)
        {
            ExceptionHelper.TrueThrow(propInfo == null, "DCTSimpleProperty源对象不能为空");

            DCTSimpleProperty simpleProp = null;
            var  value = propInfo.GetValue(source, null);
            Type type  = value.GetType();

            PropertyInfo[] propInfos = type.GetProperties();
            object[]       attrs     = propInfo.GetCustomAttributes(typeof(WordPropertyAttribute), true);

            if (attrs.Length > 0)
            {
                simpleProp = new DCTSimpleProperty();
                WordPropertyAttribute attr = (WordPropertyAttribute)attrs[0];
                simpleProp.TagID        = string.IsNullOrEmpty(tagID) ? tagID : attr.TagID;
                simpleProp.FormatString = attr.FormatString;
                simpleProp.Value        = propInfo.GetValue(source, null);
            }
            //WordPropertyAttribute attrs = attrs.Where(p => p is WordPropertyAttribute).ToArray();
            return(simpleProp);
        }
示例#7
0
        public static DCTWordDataObject Build(object source, string tagID)
        {
            ExceptionHelper.TrueThrow(source == null, "源对象不能为空");
            DCTWordDataObject wordObj = new DCTWordDataObject();

            Type sourceType = source.GetType();

            PropertyInfo[] fieldInfos = sourceType.GetProperties();

            foreach (PropertyInfo propInfo in fieldInfos)
            {
                if (propInfo.PropertyType.Name != typeof(object).Name)
                {
                    DCTSimpleProperty simpleProp = ConvertToSimpleProperty(propInfo, source, "");
                    if (simpleProp != null)
                    {
                        wordObj.PropertyCollection.Add(simpleProp);
                    }
                }
            }
            return(wordObj);
        }
示例#8
0
        public override void Process()
        {
            DCTSimpleProperty property = DataProperty as DCTSimpleProperty;

            if (null != property)
            {
                var containerElement = document.MainDocumentPart.Document.Body
                                       .Descendants <SdtElement>().Where(o => o.SdtProperties.Descendants <SdtAlias>().Any(a => a.Val == DataProperty.TagID)).FirstOrDefault();
                if (null == containerElement)
                {
                    return;
                }
                var runElement = containerElement.Descendants <Run>().First();
                runElement.RemoveAllChildren();
                runElement.AppendChild <Text>(new Text(GeneralFormatter.ToString(property.Value, property.FormatString)));
                if (property.IsReadOnly)
                {
                    Lock lockControl = new Lock();
                    lockControl.Val = LockingValues.SdtContentLocked;
                    containerElement.SdtProperties.Append(lockControl);
                }
            }
        }
示例#9
0
        protected string[] GetMultiRowsValues(DCTSimpleProperty property)
        {
            string[] rows = null;

            if (property.Value != null)
            {
                string template = "{0}";

                if (property.FormatString.IsNotEmpty())
                {
                    template = "{0:" + property.FormatString + "}";
                }

                string   strAlltext = string.Format(template, property.Value);
                string[] splitArray = new string[] { "\r\n" };
                rows = strAlltext.Split(splitArray, StringSplitOptions.None);
            }
            else
            {
                rows = StringExtension.EmptyStringArray;
            }

            return(rows);
        }
        public override void Process()
        {
            DCTSimpleProperty property = DataProperty as DCTSimpleProperty;

            if (null != property)
            {
                var containerElement = document.MainDocumentPart.Document.Body
                                       .Descendants <SdtElement>().Where(o => o.SdtProperties.Descendants <SdtAlias>().Any(a => a.Val == DataProperty.TagID)).FirstOrDefault();
                if (null == containerElement)
                {
                    return;
                }

                object text = property.Value;

                var pas = containerElement.Descendants <Paragraph>().ToList();

                Paragraph p = pas.FirstOrDefault();
                if (p == null)
                {
                    var runElement = containerElement.Descendants <Run>().First();
                    FillText(ref runElement, GeneralFormatter.ToString(text, property.FormatString));

                    /*var runElement = containerElement.Descendants<Run>().First();
                     * runElement.RemoveAllChildren();
                     * runElement.AppendChild<Text>(new Text(GeneralFormatter.ToString(strAlltext, property.FormatString))); */
                }
                else
                {
                    string[] rows = GetMultiRowsValues(property);

                    for (int i = 0; i < rows.Length; i++)
                    {
                        if (i == 0)
                        {
                            Run runElement = GetRunElement(p);
                            FillText(runElement, GeneralFormatter.ToString(rows[i], property.FormatString));
                        }
                        else
                        {
                            Paragraph addrow;
                            if (i < pas.Count)
                            {
                                addrow = pas[i];
                            }
                            else
                            {
                                addrow = p.CloneNode(true) as Paragraph;
                            }

                            Run addrunelement = GetRunElement(addrow);

                            FillText(addrunelement, GeneralFormatter.ToString(rows[i], property.FormatString));

                            if (i >= pas.Count)
                            {
                                var lastCon = containerElement.Descendants <Paragraph>().Last();
                                lastCon.InsertAfterSelf <Paragraph>(addrow);
                            }
                        }
                    }
                }

                if (property.IsReadOnly)
                {
                    Lock lockControl = new Lock();
                    lockControl.Val = LockingValues.SdtContentLocked;
                    containerElement.SdtProperties.Append(lockControl);
                }
            }
        }
 public SimplePropertyProcessor(WordprocessingDocument document, DCTSimpleProperty dataProperty)
     : base(document, dataProperty)
 {
 }
        public override void Process()
        {
            var titleRow = document.MainDocumentPart.Document.Body
                           .Descendants <TableRow>().Where(o => o.Descendants <BookmarkStart>().Any(mark => mark.Name == DataProperty.TagID)).FirstOrDefault();

            var sdtElements = titleRow.Descendants <SdtElement>().Where(o => o.SdtProperties.Descendants <SdtAlias>().Any(a => a.Val != null)).ToList();

            var bookmarkStart = titleRow.Descendants <BookmarkStart>().Where(o => o.Name == DataProperty.TagID).FirstOrDefault();

            List <string> properties = new List <string>();

            for (int i = 0; i < sdtElements.Count; i++)
            {
                properties.Add(sdtElements[i].Descendants <SdtAlias>().FirstOrDefault().Val.Value);
            }

            TableRow curRow = titleRow;

            foreach (DCTWordDataObject wordDataObj in DataProperty.DataObjects)
            {
                TableRow newRow = curRow.InsertAfterSelf <TableRow>((TableRow)curRow.NextSibling <TableRow>().CloneNode(true));

                TableCell firstCell   = newRow.GetFirstChild <TableCell>();
                int       columnFirst = Int32Value.ToInt32(bookmarkStart.ColumnFirst);
                //== default(Int32Value) ? bookmarkStart.ColumnFirst.Value : 0;
                int columnLast = Int32Value.ToInt32(bookmarkStart.ColumnLast);

                for (int j = columnFirst; j <= columnLast; j++)
                {
                    DCTDataProperty dataProperty = wordDataObj.PropertyCollection[properties[j - columnFirst]];

                    if (dataProperty is DCTSimpleProperty)
                    {
                        DCTSimpleProperty simpleProperty = dataProperty as DCTSimpleProperty;
                        TableCell         cell           = GetCellByIndex(firstCell, j);
                        Paragraph         p = cell.GetFirstChild <Paragraph>();

                        string[] rows = GetMultiRowsValues(simpleProperty);

                        for (int i = 0; i < rows.Length; i++)
                        {
                            if (i == 0)
                            {
                                Run runElement = GetRunElement(p);
                                FillText(ref runElement, GeneralFormatter.ToString(rows[i], simpleProperty.FormatString));
                            }
                            else
                            {
                                Paragraph addrow        = p.CloneNode(true) as Paragraph;
                                Run       addrunelement = GetRunElement(addrow);
                                //addrow.GetFirstChild<Run>();
                                FillText(ref addrunelement, GeneralFormatter.ToString(rows[i], simpleProperty.FormatString));
                                p.InsertAfterSelf <Paragraph>(addrow);
                            }
                        }
                    }
                }

                curRow = newRow;
            }
        }