Esempio n. 1
0
        public static void InsertChart <Y>(this WorksheetPart sheet, BaseChart <Y> chart, string columnId,
                                           string columnOffset, string rowId, string rowOffset, string toColumnId,
                                           string toColumnOffset, string toRowId, string toRowOffset)
        {
            if (sheet.Worksheet.Elements <Drawing>().Count() <= 0)
            {
                sheet.Worksheet.Append(new Drawing()
                {
                    Id = sheet.GetIdOfPart(sheet.GetDrawingsPart())
                });
                sheet.Worksheet.Save();
            }

            // Position the chart on the worksheet using a TwoCellAnchor object.
            if (sheet.DrawingsPart.WorksheetDrawing == null)
            {
                sheet.DrawingsPart.WorksheetDrawing = new ss::WorksheetDrawing();
            }
            var twoCellAnchor = sheet.DrawingsPart.WorksheetDrawing.AppendChild(new ss::TwoCellAnchor());

            twoCellAnchor.Append(new ss::FromMarker(new ss::ColumnId(columnId), new ss::ColumnOffset(columnOffset),
                                                    new ss::RowId(rowId), new ss::RowOffset(rowOffset)));
            twoCellAnchor.Append(new ss::ToMarker(new ss::ColumnId(toColumnId),
                                                  new ss::ColumnOffset(toColumnOffset), new ss::RowId(toRowId),
                                                  new ss::RowOffset(toRowOffset)));

            // Append a GraphicFrame to the TwoCellAnchor object.
            var graphicFrame = twoCellAnchor.AppendChild(new ss::GraphicFrame());

            graphicFrame.Macro = "";
            graphicFrame.Append(new ss::NonVisualGraphicFrameProperties(
                                    new ss::NonVisualDrawingProperties()
            {
                Id = new UInt32Value(2u), Name = "Chart 1"
            },
                                    new ss::NonVisualGraphicFrameDrawingProperties()));

            graphicFrame.Append(new ss::Transform(new Offset()
            {
                X = 0L, Y = 0L
            },
                                                  new Extents()
            {
                Cx = 0L, Cy = 0L
            }));
            graphicFrame.Append(new Graphic(new GraphicData(new ChartReference()
            {
                Id = chart.Id
            })
            {
                Uri = "http://schemas.openxmlformats.org/drawingml/2006/chart"
            }));

            twoCellAnchor.Append(new ss::ClientData());

            // Save the WorksheetDrawing object.
            sheet.DrawingsPart.WorksheetDrawing.Save();
        }
Esempio n. 2
0
        public static void InsertShape(this WorksheetPart sheet, string columnId, string columnOffset,
                                       string rowId, string rowOffset, string toColumnId,
                                       string toColumnOffset, string toRowId, string toRowOffset)
        {
            if (sheet.Worksheet.Elements <Drawing>().Count() <= 0)
            {
                sheet.Worksheet.Append(new Drawing()
                {
                    Id = sheet.GetIdOfPart(sheet.GetDrawingsPart())
                });
                sheet.Worksheet.Save();
            }

            // Position the shape on the worksheet using a TwoCellAnchor object.
            if (sheet.DrawingsPart.WorksheetDrawing == null)
            {
                sheet.DrawingsPart.WorksheetDrawing = new ss::WorksheetDrawing();
            }

            var twoCellAnchor = sheet.DrawingsPart.WorksheetDrawing.AppendChild(new ss::TwoCellAnchor());

            twoCellAnchor.Append(new OpenXmlElement[] { new ss::FromMarker(new ss::ColumnId(columnId),
                                                                           new ss::ColumnOffset(columnOffset), new ss::RowId(rowId),
                                                                           new ss::RowOffset(rowOffset)),
                                                        new ss::ToMarker(new ss::ColumnId(toColumnId),
                                                                         new ss::ColumnOffset(toColumnOffset), new ss::RowId(toRowId),
                                                                         new ss::RowOffset(toRowOffset)),
                                                        new ss::ConnectionShape(new ss::NonVisualConnectionShapeProperties(new ss::NonVisualDrawingProperties()
                {
                    Name = "Straight Connector 2", Id = 2U
                },
                                                                                                                           new ss::NonVisualConnectorShapeDrawingProperties()),
                                                                                new ss::ShapeProperties(new Transform2D(new Offset()
                {
                    Y = 211282, X = 14096134
                }, new Extents()
                {
                    Cy = 22954576, Cx = 15875
                }),
                                                                                                        new PresetGeometry(new AdjustValueList())
                {
                    Preset = ShapeTypeValues.Line
                },
                                                                                                        new DocumentFormat.OpenXml.Drawing.Outline()
                {
                    Width = 38100
                }),
                                                                                new ss::ShapeStyle(new LineReference(new SchemeColor()
                {
                    Val = SchemeColorValues.Accent1
                })
                {
                    Index = 1U
                },
                                                                                                   new FillReference(new SchemeColor()
                {
                    Val = SchemeColorValues.Accent1
                })
                {
                    Index = 0U
                },
                                                                                                   new EffectReference(new SchemeColor()
                {
                    Val = SchemeColorValues.Accent1
                })
                {
                    Index = 0U
                },
                                                                                                   new FontReference(new SchemeColor()
                {
                    Val = SchemeColorValues.Text1
                })
                {
                    Index = FontCollectionIndexValues.Minor
                }))
                                                        {
                                                            Macro = ""
                                                        },
                                                        new ss::ClientData() });

            // Save the WorksheetDrawing object.
            sheet.DrawingsPart.WorksheetDrawing.Save();
        }