Esempio n. 1
0
        ///// <summary>
        ///// Saves the memory stream to my documents with the provided title
        ///// </summary>
        ///// <param name="title">The title.</param>
        ///// <param name="stream">The stream.</param>
        ///// <returns>
        ///// The full path of the saved file
        ///// </returns>
        //public static string Save(string title, MemoryStream stream)
        //{
        //    string filePath = FileHelper.GetMyDocumentFilePath(title);
        //    using (FileStream fileStream = File.OpenWrite(filePath))
        //    {
        //        stream.WriteTo(fileStream);
        //    }
        //    return filePath;
        //}

        ///// <summary>
        ///// Saves the stream to the my documents with the title
        ///// Optionally opends
        ///// </summary>
        ///// <param name="open">if set to <c>true</c> [open].</param>
        ///// <param name="title">The title.</param>
        ///// <param name="stream">The stream.</param>
        ///// <returns>
        ///// The full path of the saved file
        ///// </returns>
        //public static string SaveAndOpen(bool open, string title, MemoryStream stream)
        //{
        //    // call save and open
        //    string outputFile = Save(title, stream);
        //    if (open)
        //    {
        //        ThreadPool.QueueUserWorkItem(
        //            (obj) =>
        //            {
        //                Process.Start(outputFile);
        //            });
        //    }
        //    return outputFile;
        //}

        #region Build of export sets

        /// <summary>
        /// Creates a list of export sets matching parts based on the provided data.
        /// For each data part try and find a matching export part.
        /// If any mandatory export parts havent been provided with data an exception will be thrown.
        /// </summary>
        /// <param name="dataParts">The data parts.</param>
        /// <param name="exportMetadata">The export metadata.</param>
        /// <param name="templatePackage">The template package.</param>
        /// <returns></returns>
        /// <exception cref="MetadataException"></exception>
        private List <ExportTripleSet> BuildSets(IEnumerable <IDataPart> dataParts, ExportMetadata exportMetadata, ExcelTemplatePackage templatePackage)
        {
            // pair up the data with the export part and keep them for use later
            var sets = new List <ExportTripleSet>();

            // for each data part get the matching export part
            foreach (var dataPart in dataParts)
            {
                if (dataPart == null)
                {
                    continue;
                }

                // return the ExportPart(s) that matches with the provided data part
                foreach (var exportPart in SafePartMatch(dataPart, exportMetadata.Parts))
                {
                    // return a new instance of the template
                    var template = templatePackage.GetTemplateByTemplateId(exportPart.TemplateId);

                    // and add the 3 items to the set
                    sets.Add(new ExportTripleSet(dataPart, exportPart, template));
                }
            }

            // get a list of mandatory parts
            var mandatoryExportParts = from p in exportMetadata.Parts
                                       where p.IsMandatory
                                       select p;

            StringBuilder errors = new StringBuilder();

            // and for each of these check that they've been added to the set
            foreach (var mandatory in mandatoryExportParts)
            {
                bool match = (from s in sets
                              where s.Part == mandatory
                              select s).Any();
                // if we cant find one them add to our list of errors
                if (!match)
                {
                    errors.Append("No DataPart supplied for mandatory export part <");
                    errors.Append(mandatory.PartId);
                    errors.Append(">");
                    errors.Append(Environment.NewLine);
                }
            }

            // then throw any missing mandatorys as a metadata exception
            if (errors.Length > 0)
            {
                throw new MetadataException(errors.ToString());
            }

            return(sets);
        }
Esempio n. 2
0
        /// <summary>
        /// Adds a set of parameters as debug data parts.
        /// </summary>
        /// <param name="dataParts">The data parts.</param>
        /// <param name="metadata">The metadata.</param>
        /// <param name="exportParameters">The export parameters.</param>
        /// <returns></returns>
        private static IEnumerable <IDataPart> AddDebugPart(IEnumerable <IDataPart> dataParts, ExportMetadata metadata, ExportParameters exportParameters)
        {
            if (exportParameters != null && exportParameters.IncludeDebug)
            {
                var exportDP = new ExportParametersDataPart(exportParameters);
                dataParts = dataParts.Concat(new[] { exportDP });

                metadata.Parts.Add(new ExportPart
                {
                    PartId          = exportDP.PartId,
                    DataSheetHidden = true,
                    DataSheetName   = "_Parameters",
                    TemplateId      = "Common.DebugTemplate"
                });
            }
            return(dataParts);
        }
Esempio n. 3
0
        ///// <summary>
        ///// Creates an excel file containing data provided in the dataparts.
        ///// Each IDatapart will match with an ExportPart of the same PartId
        ///// and will use this information to output the data in the desired format.
        ///// </summary>
        ///// <param name="open">true to open, false to not</param>
        ///// <param name="title">The title.</param>
        ///// <param name="dataParts">The data parts to output</param>
        ///// <param name="metadata">The metadata.</param>
        ///// <param name="exportParameters">Optional export parameters.</param>
        ///// <returns>The full path of the created file</returns>
        //public string ExportToExcel(bool open, string title, IEnumerable<IDataPart> dataParts, ExportMetadata metadata, ExcelTemplatePackage templatePackage, ExportParameters exportParameters = null)
        //{
        //    if (string.IsNullOrEmpty(title))
        //    {
        //        throw new ArgumentNullException("targetFilePath");
        //    }
        //    if (dataParts == null)
        //    {
        //        throw new ArgumentNullException("dataParts");
        //    }
        //    if (metadata == null)
        //    {
        //        throw new ArgumentNullException("metadata");
        //    }
        //    if (templatePackage == null)
        //    {
        //        throw new ArgumentNullException("templatePackage");
        //    }

        //    // Adds the parameters as a data part (creates a hidden sheet)
        //    dataParts = AddDebugPart(dataParts, metadata, exportParameters);

        //    // build our sets of DataParts, ExportParts, ExportTemplates
        //    List<ExportTripleSet> sets = this.BuildSets(dataParts, metadata, templatePackage);
        //    if (sets.Count == 0)
        //    {
        //        throw new ExportException("Nothing to export, no matching DataParts, ExportParts, ExportTemplates found");
        //    }

        //    var stream = this.ExcelExportInternal(exportParameters, metadata, sets, dataParts, templatePackage);

        //    if (!title.EndsWith(".xls") && !title.EndsWith(".xlsx"))
        //    {
        //        title = string.Concat(title, ".xlsx");
        //    }

        //    return SaveAndOpen(open, title, stream);
        //}

        /// <summary>
        /// Exports to excel memory stream.
        /// </summary>
        /// <param name="dataParts">The data parts.</param>
        /// <param name="metadata">The metadata.</param>
        /// <param name="templatePackage">The template package.</param>
        /// <param name="exportParameters">The export parameters.</param>
        /// <returns></returns>
        public ExportToMemoryStreamResult ExportToExcelMemoryStream(IEnumerable <IDataPart> dataParts, ExportMetadata metadata, ExcelTemplatePackage templatePackage, ExportParameters exportParameters = null)
        {
            var result = new ExportToMemoryStreamResult();

            if (dataParts == null)
            {
                throw new ArgumentNullException("dataParts");
            }
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }
            if (templatePackage == null)
            {
                throw new ArgumentNullException("templatePackage");
            }

            dataParts = AddDebugPart(dataParts, metadata, exportParameters);

            // build our sets of DataParts, ExportParts, ExportTemplates
            List <ExportTripleSet> sets = this.BuildSets(dataParts, metadata, templatePackage);

            if (sets.Count == 0)
            {
                throw new ExportException("Nothing to export, no matching DataParts, ExportParts, ExportTemplates found");
            }

            try
            {
                result.MemoryStream = this.ExcelExportInternal(exportParameters, metadata, sets, dataParts, templatePackage);
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }

            return(result);
        }