Esempio n. 1
0
        private static SetResult SetImpl(IInput context, Stream input, XlsxDocumentMetadataSettings settings)
        {
            var outputStream = new MemoryStream();

            try
            {
                using (var excel = new ExcelPackage(input))
                {
                    excel.Workbook.Properties.SetDocumentMetadata(settings);
                    excel.SaveAs(outputStream);

                    return(SetResult.CreateSuccessResult(new SetResultData
                    {
                        Context = context,
                        InputStream = input,
                        OutputStream = outputStream
                    }));
                }
            }
            catch (Exception ex)
            {
                return(SetResult.FromException(
                           ex,
                           new SetResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }
        }
        /// <summary>
        /// Sets the document metadata from model.
        /// </summary>
        /// <param name="properties">The document properties.</param>
        /// <param name="metadata">Model metadata information.</param>
        /// <returns>
        /// An <see cref="OfficeProperties"/> reference which contains the document metadata.
        /// </returns>
        public static OfficeProperties SetDocumentMetadata(this OfficeProperties properties, XlsxDocumentMetadataSettings metadata)
        {
            SentinelHelper.ArgumentNull(properties, nameof(properties));
            SentinelHelper.ArgumentNull(metadata, nameof(metadata));

            // Core properties
            properties.Created  = DateTime.Now;
            properties.Modified = DateTime.Now;

            if (!string.IsNullOrEmpty(metadata.Title))
            {
                properties.Title = metadata.Title;
            }

            if (!string.IsNullOrEmpty(metadata.Subject))
            {
                properties.Subject = metadata.Subject;
            }

            if (!string.IsNullOrEmpty(metadata.Author))
            {
                properties.Author = metadata.Author;
            }

            if (!string.IsNullOrEmpty(metadata.Manager))
            {
                properties.Manager = metadata.Manager;
            }

            if (!string.IsNullOrEmpty(metadata.Company))
            {
                properties.Company = metadata.Company;
            }

            if (!string.IsNullOrEmpty(metadata.Category))
            {
                properties.Category = metadata.Category;
            }

            if (!string.IsNullOrEmpty(metadata.Keywords))
            {
                properties.Keywords = metadata.Keywords;
            }

            if (!string.IsNullOrEmpty(metadata.Comments))
            {
                properties.Comments = metadata.Comments;
            }

            if (!string.IsNullOrEmpty(metadata.Url))
            {
                var isValid = Uri.IsWellFormedUriString(metadata.Url, UriKind.RelativeOrAbsolute);
                if (isValid)
                {
                    try
                    {
                        properties.HyperlinkBase = new Uri(metadata.Url, UriKind.RelativeOrAbsolute);
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }

            return(properties);
        }