Excel open xml jdk - 控制图表位置

Excel open xml sdk - Controling chart position

我正在尝试弄清楚如何获取现有图表在 sheet 上的位置并了解它的范围(列和行)"hides"...
我有图表部分和图表元素。

图表有两部分 - 图表本身和包含图表位置信息的绘图元素。因此,要找出图表的位置,您需要找到 DrawingsPart。实际位置存储在 TwoCellAnchor which contains the zero-based column and row indices of the top-left and bottom-right cells of the drawing object. As a drawing object doesn't have to be right on the boundary of a cell there is also an offset to show where in the cell the drawing object starts. This value is in EMU's 中,除非数字后紧跟单位标识符。

下面的代码会遍历所有WorksheetParts中的DrawingsPart中的所有ChartParts,输出TwoCellAnchor的信息。

public static void FindCharts(string filename)
{
    using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filename, false))
    {
        foreach (WorksheetPart wsp in spreadsheetDocument.WorkbookPart.WorksheetParts)
        {
            if (wsp.DrawingsPart != null)
            {
                foreach (ChartPart chart in wsp.DrawingsPart.ChartParts)
                {
                    TwoCellAnchor tca = wsp.DrawingsPart.WorksheetDrawing.Descendants<TwoCellAnchor>().FirstOrDefault();
                    if (tca != null)
                    {
                        Console.WriteLine("A Chart starts at Column {0} ({1}), Row {2} ({3}) and ends at Column {4} ({5}), Row {6} ({7})",
                                            tca.FromMarker.ColumnId.Text,
                                            tca.FromMarker.ColumnOffset.Text,
                                            tca.FromMarker.RowId.Text,
                                            tca.FromMarker.RowOffset.Text,
                                            tca.ToMarker.ColumnId.Text,
                                            tca.ToMarker.ColumnOffset.Text,
                                            tca.ToMarker.RowId.Text,
                                            tca.ToMarker.RowOffset.Text
                                            );
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find position of chart {0}", chart.ChartSpace.LocalName.ToString());
                    }
                }
            }
        }
    }
}