public string GetCellValue(OpenXmlPartContainer wbPart, EnumValue <CellValues> dataType, string value)
        {
            if (dataType != null)
            {
                switch (dataType.Value)
                {
                case CellValues.SharedString:
                    var stringTable = wbPart.GetPartsOfType <SharedStringTablePart>().FirstOrDefault();
                    if (stringTable != null)
                    {
                        value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
                    }
                    break;

                case CellValues.Boolean:
                    switch (value)
                    {
                    case "0":
                        value = "False";
                        break;

                    default:
                        value = "True";
                        break;
                    }
                    break;
                }
            }

            return(value);
        }
Esempio n. 2
0
        private static void UpdateWordBlock(OpenXmlPartContainer container, OpenXmlElement block, string content)
        {
            OXW.Text new_text = new OXW.Text(content);
            if (!string.IsNullOrEmpty(content) && (char.IsWhiteSpace(content[0]) || char.IsWhiteSpace(content[content.Length - 1])))
            {
                new_text.Space = SpaceProcessingModeValues.Preserve;
            }
            OXW.Run           run             = new OXW.Run(new_text);
            OXW.RunProperties originalRunProp = block.Descendants <OXW.RunProperties>().FirstOrDefault();
            if (originalRunProp != null)
            {
                run.RunProperties = (OXW.RunProperties)originalRunProp.CloneNode(true);
            }
            OpenXmlElement finalBlock  = run;
            var            cbcontainer = block.Parent;

            if (null != cbcontainer)
            {
                cbcontainer.Parent.ReplaceChild(finalBlock, cbcontainer);
            }
            var docPart = container.GetPartsOfType <MainDocumentPart>().FirstOrDefault();

            if (docPart == null)
            {
                var p = container as OpenXmlPart;
                if (p != null)
                {
                    docPart = p.GetParentParts().FirstOrDefault(_ => _ is MainDocumentPart) as MainDocumentPart;
                }
            }
            if (docPart != null)
            {
                docPart.Document.Save();
            }
        }
        public string GetCellValue(OpenXmlPartContainer wbPart, EnumValue<CellValues> dataType, string value)
        {
            if (dataType != null)
            {
                switch (dataType.Value)
                {
                    case CellValues.SharedString:
                        var stringTable = wbPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
                        if (stringTable != null)
                        {
                            value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
                        }
                        break;

                    case CellValues.Boolean:
                        switch (value)
                        {
                            case "0":
                                value = "False";
                                break;
                            default:
                                value = "True";
                                break;
                        }
                        break;
                }

            }

            return value;
        }
Esempio n. 4
0
        private static void UpdateWordBlock(OpenXmlPartContainer container, OpenXmlElement block, string content)
        {
            OXW.Text new_text = new OXW.Text(content);
            if (!string.IsNullOrEmpty(content) && (char.IsWhiteSpace(content[0]) || char.IsWhiteSpace(content[content.Length - 1])))
            {
                new_text.Space = SpaceProcessingModeValues.Preserve;
            }
            OXW.Run           run             = new OXW.Run(new_text);
            OXW.RunProperties originalRunProp = block.Descendants <OXW.RunProperties>().FirstOrDefault();
            if (originalRunProp != null)
            {
                run.RunProperties = (OXW.RunProperties)originalRunProp.CloneNode(true);
            }
            OpenXmlElement finalBlock = run;

            if ("SdtRun" == block.Parent.GetType().Name)
            {
                // case text block in a content control
                var cbcontainer = block.Parent;
                cbcontainer?.Parent.ReplaceChild(finalBlock, cbcontainer);
            }
            else
            {
                // case text block is in a text box
                var oldTxt = block.Descendants <OXW.Run>().FirstOrDefault()?.Parent;
                oldTxt?.RemoveAllChildren();
                oldTxt?.AppendChild(finalBlock);
            }
            var docPart = container.GetPartsOfType <MainDocumentPart>().FirstOrDefault();

            if (docPart == null)
            {
                var p = container as OpenXmlPart;
                if (p != null)
                {
                    docPart = p.GetParentParts().FirstOrDefault(_ => _ is MainDocumentPart) as MainDocumentPart;
                }
            }
            docPart?.Document.Save();
        }