How to get a shape/picture coordinates in pixels in an excel file (xlsx) using EPPlus

岁酱吖の 提交于 2019-12-12 03:26:28

问题


Has anyone succeeded in obtaining the x and y coordinate in pixels of a shape or a picture in a sheet in an excel 2010 file using epplus?


回答1:


I finally found a solution using internal xml data, it seems that excel is using a unit for measure called EMU which is equal to 1/9525 pixel. the solution is below

public static Rectangle GetDimensions(string shapeName, ExcelWorksheet sheet)
{
  const int EMU = 9525;
  var shapeBaseNodexPath = string.Format("//*[@name='{0}']", shapeName);

  //get node that contains name of the shape
  var shapeNameNode = sheet.Drawings.DrawingXml.SelectSingleNode(shapeBaseNode);

  if (shapeNameNode == null)
    throw new ArgumentException("Invalid shape name");
  //go 2 levels up and select shape properties node <a:xfrm> node
  var propertiesNode = shapeNameNode
    .SelectSingleNode("../../*[local-name() = 'spPr']/*[local-name() = 'xfrm']");
  if (propertiesNode == null)
    throw new InvalidOperationException("Could not parse Excel file xml data");

  //get coodinates and size nodes
  var locationNode = propertiesNode.SelectSingleNode("*[local-name() = 'off']");
  var sizeNode = propertiesNode.SelectSingleNode("*[local-name() = 'ext']");

  //create Rectangle
  int x, y, w, h = 0;
  x = int.Parse(locationNode.Attributes["x"].Value) / EMU;
  y = int.Parse(locationNode.Attributes["y"].Value) / EMU;
  w = int.Parse(sizeNode.Attributes["cx"].Value) / EMU;
  h = int.Parse(sizeNode.Attributes["cy"].Value) / EMU;

  return new Rectangle(x, y, w, h);
}



回答2:


Its not pretty unfortunately. The functions you really need are GetPixelTop and GetPixelLeft in the file ExcelDrawingBase.cs but they are marked as internal: http://epplus.codeplex.com/SourceControl/latest#EPPlus/Drawing/ExcelDrawingBase.cs. So you cannot really call them without modification to the source code.

The simplest would simply be to recreate your own version. Here is a quick cut/paste (make sure you QA well before going to production):

public int DrawingPixelX(ExcelWorksheet worksheet, ExcelDrawing drawing)
{
    const int emuPerPixel = ExcelDrawing.EMU_PER_PIXEL;
    decimal mdw = worksheet.Workbook.MaxFontWidth;

    var pix = 0;
    for (var i = 1; i <= drawing.From.Column; i++)
        pix += (int)decimal.Truncate(((256 * (decimal)worksheet.Column(i).Width + decimal.Truncate(128 / mdw)) / 256) * mdw);

    pix += drawing.From.ColumnOff / emuPerPixel;
    return pix;
}

public int DrawingPixelY(ExcelWorksheet worksheet, ExcelDrawing drawing)
{
    const int emuPerPixel = ExcelDrawing.EMU_PER_PIXEL;

    var piy = 0;
    for (var i = 1; i <= drawing.From.Row; i++)
        piy += (int)(worksheet.Row(i).Height / 0.75);

    piy += drawing.From.RowOff / emuPerPixel;
    return piy;
}

And you can use it like this:

var workbook = package.Workbook;
var ws = workbook.Worksheets.First();
var pic = ws.Drawings.First();

Console.WriteLine(DrawingPixelX(ws, pic));
Console.WriteLine(DrawingPixelY(ws, pic));

I get a Left and Top of 141 and 43 for the attached image.




回答3:


You can read it from internal ExcelDrawing internal fields _widht and _height. Thanks to @Ernie for pointing to the ExcelDrawingBase.cs.

shape = (ExcelShape) xls.Workbook.Worksheets[1].Drawings["MyShape"];
var width = (int) shape.GetType()
        .GetProperty("_width", BindingFlags.NonPublic | BindingFlags.Instance)
        .GetValue(shape);
var height = (int) shape.GetType()
        .GetProperty("_height", BindingFlags.NonPublic | BindingFlags.Instance)
        .GetValue(shape);   


来源:https://stackoverflow.com/questions/36463291/how-to-get-a-shape-picture-coordinates-in-pixels-in-an-excel-file-xlsx-using-e

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!