コード例 #1
0
        public override uint GetNumberingFormatId(string formatCode)
        {
            var key = OpenXMLExcels.GetNumberingFormatKey(formatCode);
            var id  = GetStyleId(key);

            if (!id.HasValue)
            {
                var numberingFormat = OpenXMLExcels.GetNumberingFormat(formatCode);
                id = _styleIdDic.Value[key] = AddNumberingFormats(numberingFormat);
            }

            return(id.Value);
        }
コード例 #2
0
        public override uint GetCellFormatIndex(uint?borderId = null, uint?fontId = null, uint?fillId = null, uint?formatId = null, uint?numberFormatId = null, Alignment alignment = null)
        {
            var cellFormatKey = OpenXMLExcels.GetCellFormatStyleKey(borderId, fontId, fillId, formatId, numberFormatId, alignment);
            var cellFormatId  = GetStyleId(cellFormatKey);

            if (!cellFormatId.HasValue)
            {
                var cellFormat = OpenXMLExcels.GetCellFormat(borderId, fontId, fillId, formatId, numberFormatId, alignment);
                cellFormatId = _styleIdDic.Value[cellFormatKey] = AddCellFormats(cellFormat);
            }

            return(cellFormatId.Value);
        }
コード例 #3
0
        public override uint GetFillId(PatternValues pattern, DColor foreDColor, DColor backDColor)
        {
            var fillKey = OpenXMLExcels.GetFillStyleKey(pattern, foreDColor, backDColor);
            var fillId  = GetStyleId(fillKey);

            if (!fillId.HasValue)
            {
                var fill = OpenXMLExcels.GetFill(pattern, foreDColor, backDColor);
                fillId = _styleIdDic.Value[fillKey] = AddFills(fill);
            }

            return(fillId.Value);
        }
コード例 #4
0
        public override uint GetBorderId(BorderStyleValues style, DColor dColor, bool includeDiagonal = false, bool isDiagonalDown = true)
        {
            var borderKey = OpenXMLExcels.GetBorderStyleKey(style, dColor, includeDiagonal, isDiagonalDown);
            var borderId  = GetStyleId(borderKey);

            if (!borderId.HasValue)
            {
                var border = OpenXMLExcels.GetBorder(style, dColor, includeDiagonal, isDiagonalDown);
                borderId = _styleIdDic.Value[borderKey] = AddBorders(border);
            }

            return(borderId.Value);
        }
コード例 #5
0
        public override uint GetFontId(double fontSize, string fontName, DColor dColor, Bold bold = null, Underline underline = null)
        {
            var fontKey = OpenXMLExcels.GetFontStyleKey(fontSize, fontName, dColor, bold, underline);
            var fontId  = GetStyleId(fontKey);

            if (!fontId.HasValue)
            {
                var font = OpenXMLExcels.GetFont(fontSize, fontName, dColor, bold, underline);
                fontId = _styleIdDic.Value[fontKey] = AddFonts(font);
            }

            return(fontId.Value);
        }
コード例 #6
0
 /// <summary>
 /// 获取单元格引用
 /// </summary>
 /// <param name="columnIndex"></param>
 /// <param name="rowIndex"></param>
 /// <returns></returns>
 protected string GetCellReference(uint columnIndex, uint rowIndex)
 => $"{ OpenXMLExcels.GetColumnNameByIndex(columnIndex) }{ rowIndex }";
コード例 #7
0
        private void CreateDrawingBySAX(DrawingsPart drawingsPart)
        {
            var data    = _data as IReadOnlyList <Sheet3Data>;
            var dataDic = new Dictionary <C.Values, C.SeriesText>();

            for (uint i = 1; i <= data.First().DataDic.Count; i++)
            {
                var columnName = OpenXMLExcels.GetColumnNameByIndex(i);
                dataDic[new C.Values()
                        {
                            NumberReference = new C.NumberReference()
                            {
                                Formula = new C.Formula($"{_sheetName}!${columnName}$2:${columnName}${data.Count + 2}")
                            }
                        }]
                    = new C.SeriesText()
                    {
                    StringReference = new C.StringReference()
                    {
                        Formula = new C.Formula($"{_sheetName}!${columnName}$1")
                    }
                    };
            }

            var chartPart = drawingsPart.AddNewPart <ChartPart>();

            drawingsPart.WorksheetDrawing = new Xdr.WorksheetDrawing();
            var twoCellAnchor = drawingsPart.WorksheetDrawing.AppendChild(new Xdr.TwoCellAnchor()
            {
                FromMarker = new Xdr.FromMarker()
                {
                    ColumnId     = new Xdr.ColumnId("5"),
                    ColumnOffset = new Xdr.ColumnOffset("581025"),
                    RowId        = new Xdr.RowId("4"),
                    RowOffset    = new Xdr.RowOffset("114300")
                },
                ToMarker = new Xdr.ToMarker()
                {
                    ColumnId     = new Xdr.ColumnId("13"),
                    ColumnOffset = new Xdr.ColumnOffset("276225"),
                    RowId        = new Xdr.RowId("19"),
                    RowOffset    = new Xdr.RowOffset("0")
                }
            });

            twoCellAnchor.Append(new Xdr.GraphicFrame()
            {
                NonVisualGraphicFrameProperties = new Xdr.NonVisualGraphicFrameProperties()
                {
                    NonVisualDrawingProperties = new Xdr.NonVisualDrawingProperties()
                    {
                        Id = 2, Name = "Chart 1", Title = "产品每月产量折线图"
                    },
                    NonVisualGraphicFrameDrawingProperties = new Xdr.NonVisualGraphicFrameDrawingProperties()
                },
                Transform = new Xdr.Transform()
                {
                    Offset = new Offset()
                    {
                        X = 0, Y = 0
                    },
                    Extents = new Extents()
                    {
                        Cx = 0, Cy = 0
                    }
                },
                Graphic = new Graphic(new GraphicData(new C.ChartReference()
                {
                    Id = drawingsPart.GetIdOfPart(chartPart)
                })
                {
                    Uri = "http://schemas.openxmlformats.org/drawingml/2006/chart",
                })
            });
            twoCellAnchor.Append(new Xdr.ClientData());

            using (var writer = OpenXmlWriter.Create(chartPart))
            {
                //S: ChartSpace
                writer.WriteStartElement(new C.ChartSpace());
                writer.WriteElement(new C.EditingLanguage()
                {
                    Val = "zh-CN"
                });

                //S: Chart
                writer.WriteStartElement(new C.Chart());

                //S: PlotArea
                writer.WriteStartElement(new C.PlotArea());
                writer.WriteElement(new C.Layout());

                //S: LineChart
                writer.WriteStartElement(new C.LineChart());
                writer.WriteElement(new C.Grouping()
                {
                    Val = C.GroupingValues.Standard
                });

                uint index = 0;
                foreach (var dataKvp in dataDic)
                {
                    //S: LineChartSeries
                    writer.WriteStartElement(new C.LineChartSeries());
                    writer.WriteElement(dataKvp.Key);
                    writer.WriteElement(new C.Index()
                    {
                        Val = index
                    });
                    writer.WriteElement(new C.Order()
                    {
                        Val = index
                    });
                    writer.WriteElement(dataKvp.Value);
                    if (index++ == 0)
                    {
                        var axisData = new C.CategoryAxisData()
                        {
                            StringReference = new C.StringReference()
                            {
                                Formula = new C.Formula($"{_sheetName}!$A$2:$A${data.Count + 2}")
                            }
                        };

                        writer.WriteElement(axisData);
                    }
                    //E: LineChartSeries
                    writer.WriteEndElement();
                }

                writer.WriteElement(new C.AxisId()
                {
                    Val = 0
                });
                writer.WriteElement(new C.AxisId()
                {
                    Val = 1
                });

                //E: LineChart
                writer.WriteEndElement();

                writer.WriteElement(new C.CategoryAxis(
                                        new C.Crosses()
                {
                    Val = C.CrossesValues.AutoZero
                },
                                        new C.AutoLabeled()
                {
                    Val = true
                },
                                        new C.LabelAlignment()
                {
                    Val = C.LabelAlignmentValues.Center
                },
                                        new C.LabelOffset()
                {
                    Val = 100
                })
                {
                    AxisId = new C.AxisId()
                    {
                        Val = 0
                    },
                    Scaling = new C.Scaling(new C.Orientation()
                    {
                        Val = C.OrientationValues.MinMax
                    }),
                    AxisPosition = new C.AxisPosition()
                    {
                        Val = C.AxisPositionValues.Bottom
                    },
                    TickLabelPosition = new C.TickLabelPosition()
                    {
                        Val = C.TickLabelPositionValues.NextTo
                    },
                    CrossingAxis = new C.CrossingAxis()
                    {
                        Val = 1
                    },
                });

                writer.WriteElement(new C.ValueAxis(
                                        new C.Crosses()
                {
                    Val = C.CrossesValues.AutoZero
                },
                                        new C.CrossBetween()
                {
                    Val = C.CrossBetweenValues.Between
                })
                {
                    AxisId = new C.AxisId()
                    {
                        Val = 1
                    },
                    Scaling = new C.Scaling(new C.Orientation()
                    {
                        Val = C.OrientationValues.MinMax
                    }),
                    AxisPosition = new C.AxisPosition()
                    {
                        Val = C.AxisPositionValues.Left
                    },
                    MajorGridlines  = new C.MajorGridlines(),
                    NumberingFormat = new C.NumberingFormat()
                    {
                        FormatCode = "General", SourceLinked = true
                    },
                    TickLabelPosition = new C.TickLabelPosition()
                    {
                        Val = C.TickLabelPositionValues.NextTo
                    },
                    CrossingAxis = new C.CrossingAxis()
                    {
                        Val = 0
                    },
                });
                //E: PlotArea
                writer.WriteEndElement();

                writer.WriteElement(new C.Legend(
                                        new C.LegendPosition()
                {
                    Val = C.LegendPositionValues.Right
                },
                                        new C.Layout()
                                        ));
                writer.WriteElement(new C.PlotVisibleOnly()
                {
                    Val = true
                });
                //E: Chart
                writer.WriteEndElement();
                //E: ChartSpace
                writer.WriteEndElement();

                writer.Close();
            }
        }