/// <summary> /// Import general properties from a configuration file. /// </summary> /// <param name="configuration">The configuration.</param> /// <returns> /// RootModel object /// </returns> /// <exception cref="T:System.Xml.Schema.XmlSchemaValidationException">Occurs if the model contains errors.</exception> /// <exception cref="T:System.InvalidOperationException">Occurs if a style assigned to a field is blank or does not exist.</exception> /// <exception cref="T:System.IO.FileNotFoundException">Occurs if not found the path to configuration file.</exception> private static ExportsModel LoadModelFrom(Uri configuration) { ExportsModel model; try { model = ExportsModel.LoadFromFile(configuration.OriginalString); } catch (InvalidOperationException ex) { var modelErrorMessage = new StringBuilder(); modelErrorMessage.AppendLine(ex.Message); var inner = ex.InnerException; while (true) { if (inner == null) { break; } modelErrorMessage.AppendLine(inner.Message); inner = inner.InnerException; } throw new XmlSchemaValidationException(modelErrorMessage.ToString()); } return(model); }
/// <summary> /// Runs the sample. /// </summary> public static void RunFromConfigurationFileSample() { Console.WriteLine(Header); Console.WriteLine(FirstSampleStepText); var inputDataFile = new Uri(Settings.Default.InventoryXmlInput, UriKind.Relative); var input = new XmlInput(inputDataFile); var configurationFile = PathHelper.ResolveRelativePath(Settings.Default.ExportEngineSample11Configuration); var models = ExportsModel.LoadFromFile(configurationFile); var model = models.Items.FirstOrDefault(); model.Table.Output.File = "sample11-custom-file-name-from-code"; model.Table.Output.Path = @"~\output\writer\xlsx\ExportEngine\"; input.Export(ExportSettings.CreateFromModels(models, "Sample11")); }
/// <inheritdoc /> /// <summary> /// Exports the input data using the specified configuration in xml configuration file. /// </summary> /// <remarks> /// If <see cref="P:iTin.Export.ExportSettings.From" /> is <c>null</c> or <see cref="F:System.String.Empty" />, /// always use the first section with <see cref="P:iTin.Export.Model.ExportModel.Current" /> attribute sets to <see cref="F:iTin.Export.Model.YesNo.Yes" />. /// </remarks> /// <param name="settings">Export settings</param> public void Export(ExportSettings settings) { SentinelHelper.ArgumentNull(settings); bool hasConfiguration = ExportSettings.TryGetConfigurationFile(settings, out Uri configuration); if (!hasConfiguration) { return; } ExportsModel root = LoadModelFrom(configuration); if (root == null) { return; } string candidateModelName = settings.From; ExportModel model = string.IsNullOrEmpty(candidateModelName) ? root.Items.FirstOrDefault(e => e.Current == YesNo.Yes) : root.Items.SingleOrDefault(e => e.Name.Equals(candidateModelName)); if (model == null) { return; } //exportModel.Table.Fields.Validate(); //exportModel.Table.Charts.Validate(); ProvidersCache providers = ProvidersCache.Instance(this); IProvider provider = providers.GetProvider(InputMetadata); InputDataModel inputDataModel = new InputDataModel { Model = model, Resources = root.Resources, References = root.References }; provider.SetInputDataModel(inputDataModel); provider.Export(settings); //// Proceso de validación. ////var aaa = root.Items[0].BlockLines[0]; ////ValidationContext validationContext = new ValidationContext(aaa, null, null); ////List<ValidationResult> errors = new List<ValidationResult>(); ////Validator.TryValidateObject(aaa, validationContext, errors, true); //// Si hay errores, los recorremos y los mostramos (versión demo). ////if (errors.Any()) ////{ //// string errorMessages = string.Empty; //// foreach (var error in errors) //// { //// errorMessages += error.ErrorMessage + Environment.NewLine; //// } //// //MessageBox.Show(errorMessages); ////} ////else ////{ //// var ss = "OK"; //// //MessageBox.Show("Entidad correcta"); ////} ////var s = ((TextLineModel)root.Resources.Lines[1]).GetStyle(); }
/// <summary> /// Gets data format from model. /// </summary> /// <param name="format">The format.</param> /// <param name="modelDataType">Data model.</param> /// <returns> /// A <see cref="T:System.String" /> containing the data format. /// </returns> public static string ToEppDataFormat(this string format, BaseDataTypeModel modelDataType) { SentinelHelper.ArgumentNull(modelDataType); var formatBuilder = new StringBuilder(); var culture = CultureInfo.CurrentCulture; var formatPatternsArray = format.Split(';'); var dataFormat = modelDataType.Type; switch (dataFormat) { #region Type: Numeric case KnownDataType.Numeric: var number = (NumberDataTypeModel)modelDataType; var numberPositivePattern = formatPatternsArray[0]; var numberNegativePattern = formatPatternsArray[1]; var numberNegativeColor = number.Negative.GetColor().ToString().Split(' ')[1]; formatBuilder.Append(numberPositivePattern); formatBuilder.Append(";"); formatBuilder.Append(numberNegativeColor); formatBuilder.Append("\\"); formatBuilder.Append(numberNegativePattern); return(formatBuilder.ToString()); #endregion #region Type: Currency case KnownDataType.Currency: var currency = (CurrencyDataTypeModel)modelDataType; var currencyPositivePattern = formatPatternsArray[0]; var lcidBuilder = new StringBuilder(); if (currency.Locale != KnownCulture.Current) { culture = CultureInfo.GetCultureInfo(ExportsModel.GetXmlEnumAttributeFromItem(currency.Locale)); lcidBuilder.Append("[$-"); lcidBuilder.Append(culture.LCID.ToString("X", CultureInfo.InvariantCulture)); lcidBuilder.Append("]"); } lcidBuilder.Append(currencyPositivePattern); var currencyPositiveFormatPattern = lcidBuilder.ToString().Replace(culture.NumberFormat.CurrencySymbol, culture.NumberFormat.CurrencySymbol); formatBuilder.Append(currencyPositiveFormatPattern); var color = currency.Negative.GetColor().ToString().Split(' ')[1]; formatBuilder.Append(";"); formatBuilder.Append(color); switch (currency.Negative.Sign) { case KnownNegativeSign.None: formatBuilder.Append(currencyPositiveFormatPattern); break; case KnownNegativeSign.Standard: formatBuilder.Append("-"); formatBuilder.Append(currencyPositiveFormatPattern); break; case KnownNegativeSign.Parenthesis: formatBuilder.Append(@"\"); formatBuilder.Append("("); formatBuilder.Append(currencyPositiveFormatPattern); formatBuilder.Append(@"\"); formatBuilder.Append(")"); break; case KnownNegativeSign.Brackets: formatBuilder.Append(@"\"); formatBuilder.Append("["); formatBuilder.Append(currencyPositiveFormatPattern); formatBuilder.Append(@"\"); formatBuilder.Append("]"); break; } return(formatBuilder.ToString()); #endregion #region Type: Percentage case KnownDataType.Percentage: var percent = (PercentageDataTypeModel)modelDataType; formatBuilder.Append("###0"); var percentDecimals = percent.Decimals; if (percentDecimals > 0) { var digits = new string('0', percentDecimals); formatBuilder.Append("."); formatBuilder.Append(digits); } formatBuilder.Append("%"); return(formatBuilder.ToString()); #endregion #region Type: Scientific case KnownDataType.Scientific: var scientific = (ScientificDataTypeModel)modelDataType; formatBuilder.Append("0"); var scientificDecimals = scientific.Decimals; if (scientificDecimals > 0) { var digits = new string('0', scientificDecimals); formatBuilder.Append("."); formatBuilder.Append(digits); } formatBuilder.Append("E+00"); return(formatBuilder.ToString()); #endregion #region Type: DateTime case KnownDataType.Datetime: var datetime = (DatetimeDataTypeModel)modelDataType; if (datetime.Locale != KnownCulture.Current) { culture = CultureInfo.GetCultureInfo(ExportsModel.GetXmlEnumAttributeFromItem(datetime.Locale)); formatBuilder.Append("[$-"); formatBuilder.Append(culture.LCID.ToString("X", CultureInfo.InvariantCulture)); formatBuilder.Append("]"); } formatBuilder.Append(format); return(formatBuilder.ToString()); #endregion #region Type: Text default: return(format); #endregion } }