如何使用EPPlus获取excel文件(xlsx)中的像素形状/图片坐标

时间:2016-04-06 22:14:58

标签: c# epplus epplus-4

是否有人使用epplus成功获取excel 2010文件中表格形状或图片的x和y坐标?

3 个答案:

答案 0 :(得分:0)

不幸的是,它不是很好。您真正需要的功能是文件GetPixelTop中的GetPixelLeftExcelDrawingBase.cs,但它们标记为internalhttp://epplus.codeplex.com/SourceControl/latest#EPPlus/Drawing/ExcelDrawingBase.cs。因此,如果不修改源代码,就无法真正调用它们。

最简单的只是重新创建自己的版本。这是一个快速剪切/粘贴(确保您在投入生产前很好地进行QA):

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;
}

你可以像这样使用它:

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

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

我为附加图像得到了141和43的左和上。

enter image description here

答案 1 :(得分:0)

我终于找到了一个使用内部xml数据的解决方案,似乎excel正在使用一个名为EMU的度量单位equal to 1/9525 pixel。解决方案如下

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 :(得分:0)

您可以从内部ExcelDrawing内部字段_widht_height中读取它。感谢@Ernie指向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);