示例#1
0
        /// <summary>
        /// Implementation to execute when set action.
        /// </summary>
        /// <param name="input">stream input</param>
        /// <param name="context">Input context</param>
        /// <returns>
        /// <para>
        /// A <see cref="SetResult"/> reference that contains the result of the operation, to check if the operation is correct, the <b>Success</b>
        /// property will be <b>true</b> and the <b>Value</b> property will contain the value; Otherwise, the the <b>Success</b> property
        /// will be false and the <b>Errors</b> property will contain the errors associated with the operation, if they have been filled in.
        /// </para>
        /// <para>
        /// The type of the return value is <see cref="SetResultData"/>, which contains the operation result
        /// </para>
        /// </returns>
        protected override SetResult SetImpl(Stream input, IInput context)
        {
            if (string.IsNullOrEmpty(SheetName))
            {
                return(SetResult.CreateErroResult(
                           "Sheet name can not be null or empty",
                           new SetResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }

            if (Columns == null)
            {
                return(SetResult.CreateSuccessResult(new SetResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }

            return(SetImpl(context, input, SheetName, Columns));
        }
示例#2
0
        /// <summary>
        /// Implementation to execute when set action.
        /// </summary>
        /// <param name="input">stream input</param>
        /// <param name="context">Input context</param>
        /// <returns>
        /// <para>
        /// A <see cref="SetResult"/> reference that contains the result of the operation, to check if the operation is correct, the <b>Success</b>
        /// property will be <b>true</b> and the <b>Value</b> property will contain the value; Otherwise, the the <b>Success</b> property
        /// will be false and the <b>Errors</b> property will contain the errors associated with the operation, if they have been filled in.
        /// </para>
        /// <para>
        /// The type of the return value is <see cref="SetResultData"/>, which contains the operation result
        /// </para>
        /// </returns>
        protected override SetResult SetImpl(Stream input, IInput context)
        {
            if (string.IsNullOrEmpty(SheetName))
            {
                return(SetResult.CreateErroResult(
                           "Sheet name can not be null or empty",
                           new SetResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }

            if (string.IsNullOrEmpty(NewName))
            {
                return(SetResult.CreateErroResult(
                           $"New sheet name '{NewName}' can not be null or empty",
                           new SetResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }

            return(SetImpl(context, input, SheetName, NewName));
        }
示例#3
0
        private static SetResult SetImpl(IInput context, Stream input, string sheetName, IEnumerable <ColumnDefinition> columns)
        {
            var outputStream = new MemoryStream();

            try
            {
                using (var excel = new ExcelPackage(input))
                {
                    var ws = excel.Workbook.Worksheets.FirstOrDefault(worksheet => worksheet.Name.Equals(sheetName, StringComparison.OrdinalIgnoreCase));
                    if (ws == null)
                    {
                        return(SetResult.CreateErroResult(
                                   $"Sheet '{sheetName}' not found",
                                   new SetResultData
                        {
                            Context = context,
                            InputStream = input,
                            OutputStream = input
                        }));
                    }

                    foreach (var column in columns)
                    {
                        ws.Column(column.Column).Width = column.Width;
                    }

                    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
                }));
            }
        }
示例#4
0
        private static SetResult SetImpl(IInput context, Stream input, string sheetName, YesNo show)
        {
            var outputStream = new MemoryStream();

            try
            {
                using (var excel = new ExcelPackage(input))
                {
                    var ws = excel.Workbook.Worksheets.FirstOrDefault(worksheet => worksheet.Name.Equals(sheetName, StringComparison.OrdinalIgnoreCase));
                    if (ws == null)
                    {
                        return(SetResult.CreateErroResult(
                                   $"Sheet '{sheetName}' not found",
                                   new SetResultData
                        {
                            Context = context,
                            InputStream = input,
                            OutputStream = input
                        }));
                    }

                    ws.View.ShowGridLines = show == YesNo.Yes;

                    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
                }));
            }
        }
示例#5
0
 private SetResult SetImplStrategy(ISet data, IInput context)
 => data == null?SetResult.CreateErroResult("Missing data") : data.Apply(ToStream(), context);