Ejemplo n.º 1
0
        /// <summary>
        /// Returns a new <see cref="ExcelCellBase"/> reference containing the initial range expanded by minichart size.
        /// </summary>
        /// <param name="range">Target range</param>
        /// <param name="size">Minichart size</param>
        /// <returns>
        /// A <see cref="ExcelAddressBase"/> containing the range address.
        /// </returns>
        public static ExcelCellBase Expand(this XlsxBaseRange range, XlsxMiniChartSize size)
        {
            int offsetY = size.VerticalCells == 1 ? 0 : size.VerticalCells - 1;
            int offsetX = size.HorizontalCells == 1 ? 0 : size.HorizontalCells - 1;

            var targetAddress = range.ToEppExcelAddress();
            var address       = ExcelCellBase.GetAddress(targetAddress.Start.Row, targetAddress.Start.Column, targetAddress.End.Row + offsetY, targetAddress.End.Column + offsetX);

            return(new ExcelAddressBase(address));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns range address.
        /// </summary>
        /// <param name="range">Target range</param>
        /// <returns>
        /// A <see cref="ExcelAddressBase"/> containing the range address.
        /// </returns>
        public static ExcelAddressBase ToEppExcelAddress(this XlsxBaseRange range)
        {
            ExcelAddressBase result = null;

            KnownRangeType rangeType = range.Type;

            switch (rangeType)
            {
            case KnownRangeType.String:
            {
                var target       = (XlsxStringRange)range;
                var hasDataRange = !string.IsNullOrEmpty(target.Address);
                if (hasDataRange)
                {
                    result = new ExcelAddressBase(target.Address);
                }
            }
            break;

            case KnownRangeType.Range:
            {
                var target  = (XlsxRange)range;
                var address = ExcelCellBase.GetAddress(
                    target.Start.Row,
                    target.Start.Column,
                    target.End.Row,
                    target.End.Column,
                    target.Start.AbsoluteStrategy == AbsoluteStrategy.Both && target.End.AbsoluteStrategy == AbsoluteStrategy.Both);
                result = new ExcelAddressBase(address);
            }
            break;

            case KnownRangeType.Point:
            {
                var target  = (XlsxPointRange)range;
                var address = ExcelCellBase.GetAddress(
                    target.Row,
                    target.Column,
                    target.Row,
                    target.Column,
                    target.AbsoluteStrategy == AbsoluteStrategy.Both);

                result = new ExcelAddressBase(address);
            }
            break;
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Try to modify the chart location settings.
        /// </summary>
        /// <param name="range">Reference to range location settings to apply</param>
        /// <returns>
        /// <para>
        /// A <see cref="BooleanResult"/> 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="BooleanResult"/>, which contains the operation result
        /// </para>
        /// </returns>
        public IResult SetPosition(XlsxBaseRange range)
        {
            if (range == null)
            {
                return BooleanResult.CreateErroResult("border can not be null");
            }

            try
            {
                ExcelAddressBase locationAddress = range.ToEppExcelAddress();
                Chart.SetPosition(locationAddress.Start.Row - 1, 0, locationAddress.Start.Column - 1, 0);

                return BooleanResult.SuccessResult;
            }
            catch (Exception e)
            {
                return BooleanResult.FromException(e);
            }
        }
Ejemplo n.º 4
0
        private static InsertResult InsertImpl(IInput context, Stream input, string sheetName, object data, XlsxBaseRange location, XlsxCellStyle style)
        {
            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(InsertResult.CreateErroResult(
                                   $"Sheet '{sheetName}' not found",
                                   new InsertResultData
                        {
                            Context = context,
                            InputStream = input,
                            OutputStream = input
                        }));
                    }

                    ExcelAddressBase locationAddress = location.ToEppExcelAddress();
                    XlsxCellStyle    safeStyle       = excel.CreateStyle(style);
                    XlsxCellMerge    merge           = safeStyle.Content.Merge;
                    string           range           = merge.Cells == 1
                        ? locationAddress.ToString()
                        : merge.Orientation == KnownMergeOrientation.Horizontal
                            ? ExcelCellBase.GetAddress(locationAddress.Start.Row, locationAddress.Start.Column, locationAddress.Start.Row, locationAddress.Start.Column + merge.Cells - 1)
                            : ExcelCellBase.GetAddress(locationAddress.Start.Row, locationAddress.Start.Column, locationAddress.Start.Row + merge.Cells - 1, locationAddress.Start.Column);

                    ExcelRange cell = ws.Cells[range];
                    cell.StyleName = locationAddress.End.Row.IsOdd()
                        ? $"{safeStyle.Name}_Alternate"
                        : safeStyle.Name ?? XlsxBaseStyle.NameOfDefaultStyle;

                    if (style.Content.Show == YesNo.Yes)
                    {
                        if (data != null)
                        {
                            cell.Value = safeStyle.Content.DataType.GetFormattedDataValue(data.ToString()).FormattedValue;

                            if (merge.Cells > 1)
                            {
                                cell.Merge = true;
                            }
                        }
                    }

                    excel.SaveAs(outputStream);

                    return(InsertResult.CreateSuccessResult(new InsertResultData
                    {
                        Context = context,
                        InputStream = input,
                        OutputStream = outputStream
                    }));
                }
            }
            catch (Exception ex)
            {
                return(InsertResult.FromException(
                           ex,
                           new InsertResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }
        }
Ejemplo n.º 5
0
        private static InsertResult InsertImpl(IInput context, Stream input, string sheetName, XlsxBaseRange location, QualifiedAggregateDefinition aggregate, XlsxCellStyle style)
        {
            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(InsertResult.CreateErroResult(
                                   $"Sheet '{sheetName}' not found",
                                   new InsertResultData
                        {
                            Context = context,
                            InputStream = input,
                            OutputStream = input
                        }));
                    }

                    string aggregateWorksheetName = aggregate.WorkSheet;
                    if (string.IsNullOrEmpty(aggregate.WorkSheet))
                    {
                        aggregateWorksheetName = sheetName;
                    }

                    var  aggregateWorksheet = ws;
                    bool sameSheetName      = aggregateWorksheetName.Equals(sheetName, StringComparison.OrdinalIgnoreCase);
                    if (!sameSheetName)
                    {
                        aggregateWorksheet = excel.Workbook.Worksheets.FirstOrDefault(worksheet => worksheet.Name.Equals(aggregateWorksheetName, StringComparison.OrdinalIgnoreCase));
                    }

                    if (aggregateWorksheet == null)
                    {
                        return(InsertResult.CreateErroResult(
                                   $"Aggregate sheet '{aggregateWorksheetName}' not found",
                                   new InsertResultData
                        {
                            Context = context,
                            InputStream = input,
                            OutputStream = input
                        }));
                    }

                    ExcelAddressBase locationAddress = location.ToEppExcelAddress();
                    XlsxCellStyle    safeStyle       = excel.CreateStyle(style);
                    XlsxCellMerge    merge           = safeStyle.Content.Merge;
                    string           range           = merge.Cells == 1
                        ? locationAddress.ToString()
                        : merge.Orientation == KnownMergeOrientation.Horizontal
                            ? ExcelCellBase.GetAddress(locationAddress.Start.Row, locationAddress.Start.Column, locationAddress.Start.Row, locationAddress.Start.Column + merge.Cells - 1)
                            : ExcelCellBase.GetAddress(locationAddress.Start.Row, locationAddress.Start.Column, locationAddress.Start.Row + merge.Cells - 1, locationAddress.Start.Column);

                    ExcelRange cell = aggregateWorksheet.Cells[range];
                    cell.StyleName = cell.StyleName = locationAddress.End.Row.IsOdd()
                        ? $"{safeStyle.Name}_Alternate"
                        : safeStyle.Name ?? XlsxBaseStyle.NameOfDefaultStyle;

                    if (style.Content.Show == YesNo.Yes)
                    {
                        var formula = new XlsxFormulaResolver(aggregate)
                        {
                            HasAutoFilter = aggregate.HasAutoFilter, WorkSheet = sheetName
                        };
                        cell.Formula = formula.Resolve();

                        if (merge.Cells > 1)
                        {
                            cell.Merge = true;
                        }
                    }

                    excel.SaveAs(outputStream);

                    return(InsertResult.CreateSuccessResult(new InsertResultData
                    {
                        Context = context,
                        InputStream = input,
                        OutputStream = outputStream
                    }));
                }
            }
            catch (Exception ex)
            {
                return(InsertResult.FromException(
                           ex,
                           new InsertResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }
        }
Ejemplo n.º 6
0
        private static InsertResult InsertImpl(IInput context, Stream input, string sheetName, XlsxBaseRange location, XlsxPicture xlsxPicture)
        {
            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(InsertResult.CreateErroResult(
                                   $"Sheet '{sheetName}' not found",
                                   new InsertResultData
                        {
                            Context = context,
                            InputStream = input,
                            OutputStream = input
                        }));
                    }

                    using (var image = xlsxPicture.GetImage())
                    {
                        if (image == null)
                        {
                            return(InsertResult.CreateErroResult(
                                       "The image could not be loaded, please check that the path is correct",
                                       new InsertResultData
                            {
                                Context = context,
                                InputStream = input,
                                OutputStream = input
                            }));
                        }

                        var pictureName = xlsxPicture.Name.HasValue() ? xlsxPicture.Name : $"picture{ws.Drawings.Count}";
                        foreach (var item in ws.Drawings)
                        {
                            var pic = item as ExcelPicture;
                            if (pic == null)
                            {
                                continue;
                            }

                            var existPicture = pic.Name.Equals(pictureName, StringComparison.OrdinalIgnoreCase);
                            if (!existPicture)
                            {
                                continue;
                            }

                            return(InsertResult.CreateErroResult(
                                       $"There is already an image with the name '{pictureName}' in the collection, please rename the image.",
                                       new InsertResultData
                            {
                                Context = context,
                                InputStream = input,
                                OutputStream = input
                            }));
                        }

                        ExcelPicture picture = ws.Drawings.AddPicture(pictureName, image);
                        var          writer  = new OfficeOpenPictureWriter(picture, ws);
                        writer.SetBorder(xlsxPicture.Border);
                        writer.SetContent(xlsxPicture.Content);
                        writer.SetPosition(location);
                        writer.SetSize(xlsxPicture.Size);
                        writer.SetShapeEffects(xlsxPicture.ShapeEffects, pictureName);
                    }

                    excel.SaveAs(outputStream);

                    return(InsertResult.CreateSuccessResult(new InsertResultData
                    {
                        Context = context,
                        InputStream = input,
                        OutputStream = outputStream
                    }));
                }
            }
            catch (Exception ex)
            {
                return(InsertResult.FromException(
                           ex,
                           new InsertResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }
        }
Ejemplo n.º 7
0
        private static InsertResult InsertImpl(IInput context, Stream input, string sheetName, XlsxBaseRange location, XlsxMiniChart miniChart)
        {
            var outputStream = new MemoryStream();

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

                    // Create Minichart
                    ExcelAddressBase locationAddress = location.ToEppExcelAddress();
                    ExcelAddressBase dataddress      = miniChart.ChartRanges.Data.ToEppExcelAddress();

                    ExcelSparklineGroup sparkline = ws.SparklineGroups.Add(miniChart.ChartType.Active.ToEppeSparklineType(), ws.Cells[locationAddress.ToString()], ws.Cells[dataddress.ToString()]);
                    sparkline.DisplayHidden = miniChart.DisplayHidden == YesNo.Yes;
                    sparkline.ColorSeries.SetColor(miniChart.ChartType.GetMiniChartSerieColor());
                    sparkline.DisplayEmptyCellsAs = miniChart.EmptyValueAs.ToEppeDisplayBlanksAs();

                    // Minichart Size
                    int           offsetY = miniChart.ChartSize.VerticalCells == 1 ? 0 : miniChart.ChartSize.VerticalCells - 1;
                    int           offsetX = miniChart.ChartSize.HorizontalCells == 1 ? 0 : miniChart.ChartSize.HorizontalCells - 1;
                    ExcelCellBase miniChartSizeAddress = location.Expand(miniChart.ChartSize);
                    ExcelRange    miniChartCell        = ws.Cells[miniChartSizeAddress.ToString()];
                    if (offsetX >= 1 || offsetY >= 1)
                    {
                        miniChartCell.Merge = true;
                    }

                    // Axes
                    // Horizontal axis
                    sparkline.DateAxisRange = null;
                    if (miniChart.ChartAxes.Horizontal.Type == MiniChartHorizontalAxisType.Date)
                    {
                        ExcelAddressBase axisRangeType = miniChart.ChartRanges.Axis.ToEppExcelAddress();
                        sparkline.DateAxisRange = ws.Cells[axisRangeType.ToString()];
                    }

                    sparkline.RightToLeft = miniChart.ChartAxes.Horizontal.RightToLeft == YesNo.Yes;
                    if (miniChart.ChartAxes.Horizontal.Show == YesNo.Yes)
                    {
                        sparkline.DisplayXAxis = true;
                        sparkline.ColorAxis.SetColor(miniChart.ChartAxes.Horizontal.GetColor());
                    }

                    // Vertical axis
                    bool maxVerticalAxisIsAuto = string.IsNullOrEmpty(miniChart.ChartAxes.Vertical.Max) || miniChart.ChartAxes.Vertical.Max.Equals("Automatic", StringComparison.OrdinalIgnoreCase);
                    sparkline.MaxAxisType = maxVerticalAxisIsAuto
                        ? eSparklineAxisMinMax.Individual
                        : eSparklineAxisMinMax.Custom;

                    bool minVerticalAxisIsAuto = string.IsNullOrEmpty(miniChart.ChartAxes.Vertical.Min) || miniChart.ChartAxes.Vertical.Min.Equals("Automatic", StringComparison.OrdinalIgnoreCase);
                    sparkline.MinAxisType = minVerticalAxisIsAuto
                        ? eSparklineAxisMinMax.Individual
                        : eSparklineAxisMinMax.Custom;

                    if (!maxVerticalAxisIsAuto)
                    {
                        sparkline.ManualMax = double.Parse(miniChart.ChartAxes.Vertical.Max);
                    }

                    if (!minVerticalAxisIsAuto)
                    {
                        sparkline.ManualMin = double.Parse(miniChart.ChartAxes.Vertical.Min);
                    }

                    // Points
                    switch (miniChart.ChartType.Active)
                    {
                    case MiniChartType.Column:
                        if (!miniChart.ChartType.Column.Points.Low.IsDefault)
                        {
                            sparkline.Low = true;
                            sparkline.ColorLow.SetColor(miniChart.ChartType.Column.Points.Low.GetColor());
                        }

                        if (!miniChart.ChartType.Column.Points.First.IsDefault)
                        {
                            sparkline.First = true;
                            sparkline.ColorFirst.SetColor(miniChart.ChartType.Column.Points.First.GetColor());
                        }

                        if (!miniChart.ChartType.Column.Points.High.IsDefault)
                        {
                            sparkline.High = true;
                            sparkline.ColorHigh.SetColor(miniChart.ChartType.Column.Points.High.GetColor());
                        }

                        if (!miniChart.ChartType.Column.Points.Last.IsDefault)
                        {
                            sparkline.Last = true;
                            sparkline.ColorLast.SetColor(miniChart.ChartType.Column.Points.Last.GetColor());
                        }

                        if (!miniChart.ChartType.Column.Points.Negative.IsDefault)
                        {
                            sparkline.Negative = true;
                            sparkline.ColorNegative.SetColor(miniChart.ChartType.Column.Points.Negative.GetColor());
                        }

                        break;

                    case MiniChartType.Line:
                        sparkline.LineWidth = double.Parse(miniChart.ChartType.Line.Serie.Width);

                        if (!miniChart.ChartType.Line.Points.Low.IsDefault)
                        {
                            sparkline.Low = true;
                            sparkline.ColorLow.SetColor(miniChart.ChartType.Line.Points.Low.GetColor());
                        }

                        if (!miniChart.ChartType.Line.Points.First.IsDefault)
                        {
                            sparkline.First = true;
                            sparkline.ColorFirst.SetColor(miniChart.ChartType.Line.Points.First.GetColor());
                        }

                        if (!miniChart.ChartType.Line.Points.High.IsDefault)
                        {
                            sparkline.High = true;
                            sparkline.ColorHigh.SetColor(miniChart.ChartType.Line.Points.High.GetColor());
                        }

                        if (!miniChart.ChartType.Line.Points.Last.IsDefault)
                        {
                            sparkline.Last = true;
                            sparkline.ColorLast.SetColor(miniChart.ChartType.Line.Points.Last.GetColor());
                        }

                        if (!miniChart.ChartType.Line.Points.Negative.IsDefault)
                        {
                            sparkline.Negative = true;
                            sparkline.ColorNegative.SetColor(
                                miniChart.ChartType.Line.Points.Negative.GetColor());
                        }

                        if (!miniChart.ChartType.Line.Points.Markers.IsDefault)
                        {
                            sparkline.Markers = true;
                            sparkline.ColorNegative.SetColor(miniChart.ChartType.Line.Points.Markers.GetColor());
                        }

                        break;

                    case MiniChartType.WinLoss:
                        if (!miniChart.ChartType.WinLoss.Points.Low.IsDefault)
                        {
                            sparkline.Low = true;
                            sparkline.ColorLow.SetColor(miniChart.ChartType.WinLoss.Points.Low.GetColor());
                        }

                        if (!miniChart.ChartType.WinLoss.Points.First.IsDefault)
                        {
                            sparkline.First = true;
                            sparkline.ColorFirst.SetColor(miniChart.ChartType.WinLoss.Points.First.GetColor());
                        }

                        if (!miniChart.ChartType.WinLoss.Points.High.IsDefault)
                        {
                            sparkline.High = true;
                            sparkline.ColorHigh.SetColor(miniChart.ChartType.WinLoss.Points.High.GetColor());
                        }

                        if (!miniChart.ChartType.WinLoss.Points.Last.IsDefault)
                        {
                            sparkline.Last = true;
                            sparkline.ColorLast.SetColor(miniChart.ChartType.WinLoss.Points.Last.GetColor());
                        }

                        if (!miniChart.ChartType.WinLoss.Points.Negative.IsDefault)
                        {
                            sparkline.Negative = true;
                            sparkline.ColorNegative.SetColor(miniChart.ChartType.WinLoss.Points.Negative.GetColor());
                        }

                        break;
                    }

                    excel.SaveAs(outputStream);

                    return(InsertResult.CreateSuccessResult(new InsertResultData
                    {
                        Context = context,
                        InputStream = input,
                        OutputStream = outputStream
                    }));
                }
            }
            catch (Exception ex)
            {
                return(InsertResult.FromException(
                           ex,
                           new InsertResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }
        }
Ejemplo n.º 8
0
        private static InsertResult InsertImpl(IInput context, Stream input, string sheetName, XlsxBaseRange location, string file)
        {
            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(InsertResult.CreateErroResult(
                                   $"Sheet '{sheetName}' not found",
                                   new InsertResultData
                        {
                            Context = context,
                            InputStream = input,
                            OutputStream = input
                        }));
                    }

                    ExcelAddressBase locationAddress = location.ToEppExcelAddress();
                    //ws.Cells[locationAddress.ToString()].lo(data, true);

                    var a = LoadXmlFromFile(file, "*");

                    excel.SaveAs(outputStream);

                    return(InsertResult.CreateSuccessResult(new InsertResultData
                    {
                        Context = context,
                        InputStream = input,
                        OutputStream = outputStream
                    }));
                }
            }
            catch (Exception ex)
            {
                return(InsertResult.FromException(
                           ex,
                           new InsertResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }
        }
Ejemplo n.º 9
0
        private static InsertResult InsertImpl <T>(IInput context, Stream input, string sheetName, IEnumerable <T> data, XlsxBaseRange location, YesNo showHeaders, XlsxCellStyle style)
        {
            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(InsertResult.CreateErroResult(
                                   $"Sheet '{sheetName}' not found",
                                   new InsertResultData
                        {
                            Context = context,
                            InputStream = input,
                            OutputStream = input
                        }));
                    }

                    var safeData = data.ToList();
                    ExcelAddressBase locationAddress = location.ToEppExcelAddress();
                    ws.Cells[locationAddress.ToString()].LoadFromCollection(safeData, showHeaders == YesNo.Yes);

                    XlsxCellStyle styleToUse = excel.CreateStyle(style);
                    var           range      = ws.Cells[locationAddress.Start.Row, locationAddress.Start.Column, locationAddress.End.Row + safeData.Count - 1, locationAddress.End.Column];
                    range.StyleName = styleToUse.Name;

                    excel.SaveAs(outputStream);

                    return(InsertResult.CreateSuccessResult(new InsertResultData
                    {
                        Context = context,
                        InputStream = input,
                        OutputStream = outputStream
                    }));
                }
            }
            catch (Exception ex)
            {
                return(InsertResult.FromException(
                           ex,
                           new InsertResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }
        }
Ejemplo n.º 10
0
        private static InsertResult InsertImpl(IInput context, Stream input, string sheetName, XlsxBaseRange location, XlsxShape xlsxShape)
        {
            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(InsertResult.CreateErroResult(
                                   $"Sheet '{sheetName}' not found",
                                   new InsertResultData
                        {
                            Context = context,
                            InputStream = input,
                            OutputStream = input
                        }));
                    }

                    var shapeName = xlsxShape.Name.HasValue() ? xlsxShape.Name : $"shape{ws.Drawings.Count}";
                    var shape     = ws.Drawings.AddShape(shapeName, eShapeStyle.Rect);

                    var writer = new OfficeOpenShapeWriter(shape, ws);
                    writer.SetBorder(xlsxShape.Border);
                    writer.SetContent(xlsxShape.Content);
                    writer.SetFont(xlsxShape.Content.Font);
                    writer.SetPosition(location);
                    writer.SetSize(xlsxShape.Size);
                    writer.SetShapeEffects(xlsxShape.ShapeEffects, shapeName);
                    writer.SetShapeStyle(xlsxShape.ShapeType);
                    writer.SetText(xlsxShape.Content.Text);

                    excel.SaveAs(outputStream);

                    return(InsertResult.CreateSuccessResult(new InsertResultData
                    {
                        Context = context,
                        InputStream = input,
                        OutputStream = outputStream
                    }));
                }
            }
            catch (Exception ex)
            {
                return(InsertResult.FromException(
                           ex,
                           new InsertResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }
        }
Ejemplo n.º 11
0
        private static InsertResult InsertImpl(IInput context, Stream input, string sheetName, XlsxBaseRange location, XlsxChart chart)
        {
            var outputStream = new MemoryStream();

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

                    // Create Chart
                    ExcelChart mainchart = null;
                    var        plots     = chart.Plots;
                    foreach (var plot in plots)
                    {
                        var series = plot.Series;
                        foreach (var serie in series)
                        {
                            // Create chart
                            ExcelChart workchart;
                            if (plot.UseSecondaryAxis.Equals(YesNo.No))
                            {
                                if (mainchart == null)
                                {
                                    mainchart      = ws.Drawings.AddChart(serie.Name, serie.ChartType.ToEppChartType());
                                    mainchart.Name = plot.Name;
                                }

                                workchart = mainchart;
                            }
                            else
                            {
                                workchart = mainchart.PlotArea.ChartTypes.Add(serie.ChartType.ToEppChartType());
                                workchart.UseSecondaryAxis = true;
                                workchart.XAxis.Deleted    = false;
                            }

                            var sr = workchart.Series.Add(serie.FieldRange.ToString(), serie.AxisRange.ToString());
                            sr.Header = serie.Name;
                        }
                    }

                    if (mainchart != null)
                    {
                        var writer = new OfficeOpenChartWriter(mainchart);
                        writer.SetSize(chart.Size);
                        writer.SetPosition(location);
                        writer.SetContent(chart);
                        writer.SetBorder(chart.Border);
                        writer.SetTitle(chart.Title);
                        writer.SetLegend(chart.Legend);
                        writer.SetAxes(chart.Axes);
                        writer.SetPlotArea(chart.Plots);
                        writer.SetShapeEffects(chart.Effects);
                    }

                    excel.SaveAs(outputStream);

                    return(InsertResult.CreateSuccessResult(new InsertResultData
                    {
                        Context = context,
                        InputStream = input,
                        OutputStream = outputStream
                    }));
                }
            }
            catch (Exception ex)
            {
                return(InsertResult.FromException(
                           ex,
                           new InsertResultData
                {
                    Context = context,
                    InputStream = input,
                    OutputStream = input
                }));
            }
        }