如何使用EPPlus在Excel文件(xlsx)中获取以像素为单位的形状/图片坐标 [英] How to get a shape/picture coordinates in pixels in an excel file (xlsx) using EPPlus

查看:525
本文介绍了如何使用EPPlus在Excel文件(xlsx)中获取以像素为单位的形状/图片坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人使用epplus成功地在excel 2010文件中获取了形状或图片的x和y坐标吗?

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?

推荐答案

我终于找到了使用内部xml数据的解决方案,看来excel使用的度量单位为EMU,即equal to 1/9525 pixel.解决方案如下

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

这篇关于如何使用EPPlus在Excel文件(xlsx)中获取以像素为单位的形状/图片坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆