C#Arcobjects:将Excel对象插入到pagelayout中

时间:2017-12-22 13:56:32

标签: c# arcmap arcobjects

我正在寻找如何将excel表格添加到mxd的页面布局中的方向;非常类似于通过arcmap界面提供的功能。

我正在使用C#arcobjects(10.5)并将MXD输入内存。那时我将excel添加到pagelayout,然后导出为PDF。

我已经厌倦了下面的内容,但是当我施放'rawSrc'变量时,它是 null 。我不确定正确的 IElement 类型是什么:

 IPageLayout pageLayout = myMXD.PageLayout;
 IGraphicsContainer graphicsContainer = pageLayout as IGraphicsContainer;

            RectangleElement ExcelSrc = new RectangleElement();

            double posX = 0.8008;
            double posY = 6.9567;
            double Legwidth = 4.8;
            System.Double w = 11.0301;
            System.Double h = 9.0949;

            System.Double aspectRatio = w / h;
            var envelope = new EnvelopeClass();
            envelope.PutCoords(posX, posY, (posX * Legwidth), (posY * Legwidth / aspectRatio));
            ExcelSrc.Geometry = envelope;

            string xcelPath = System.IO.Path.Combine("string to my directory",  "Data.xlsx");

            var rawSrc = new XLWorkbook(xcelPath).Worksheet("Table");

            IVariantStream blobStream = new VariantStreamIOClass();
            blobStream = rawSrc as IVariantStream;

            ExcelSrc = blobStream.Read() as RectangleElement;

            graphicsContainer.AddElement(((IElement)ExcelSrc), 0);

            ((IActiveView)myMXD.PageLayout).PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            myMXD.Save();

            Marshal.FinalReleaseComObject(blobStream);

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

将实际工作表名称写入Excel。 如果工作表名称“sheet1”你应该写;

  

var rawSrc = new XLWorkbook(xcelPath).Worksheet(“sheet1”);