有个妹纸找我请教如何获取图片坐标,因此我到家后花了点时间,写了这份代码。

实测下来,可以正确获取 Excel 2003 版本的形状和图片这两种的坐标/锚点,以及 Excel 2007 版本的图片的坐标/锚点。

暂未解决如何将 Excel 2007 以上版本的形状(XSSFSimpleShape)转换成图片(XSSFPicture)的问题?

如有大佬懂的,还请多多指教。

class Program
{
static void Main(params string[] args)
{
string excel2003FilePath = @"D:\Users\Allen\Desktop\image.xls";
GetPictureAnchorTest(excel2003FilePath); string excel2007FilePath = @"D:\Users\Allen\Desktop\image.xlsx";
GetPictureAnchorTest(excel2007FilePath); Console.ReadKey();
} static void GetPictureAnchorTest(string excelFilePath)
{ IWorkbook workbook = WorkbookFactory.Create(excelFilePath);
ISheet sheet = workbook.GetSheetAt(0);
IEnumerable<IPicture> pictures = sheet.GetPictures();
foreach (IPicture picture in pictures)
{
PictureAnchor anchor = picture.GetPictureAnchor();
Console.WriteLine($"PictureType:{picture.GetType().FullName}, LeftmostCellIndex: {anchor.LeftmostCellIndex}, RightmostCellIndex: {anchor.RightmostCellIndex}, TopmostRowIndex: {anchor.TopmostRowIndex}, BottommostRowIndex: {anchor.BottommostRowIndex}");
}
}
} public readonly struct PictureAnchor
{
public PictureAnchor(int leftmostCellIndex, int rightmostCellIndex, int topmostRowIndex, int bottommostRowIndex)
{
LeftmostCellIndex = leftmostCellIndex;
RightmostCellIndex = rightmostCellIndex;
TopmostRowIndex = topmostRowIndex;
BottommostRowIndex = bottommostRowIndex;
} public int LeftmostCellIndex { get; } public int RightmostCellIndex { get; } public int TopmostRowIndex { get; } public int BottommostRowIndex { get; } public override string ToString()
{
return $"LeftmostCellIndex: {LeftmostCellIndex}, RightmostCellIndex: {RightmostCellIndex}, TopmostRowIndex: {TopmostRowIndex}, BottommostRowIndex: {BottommostRowIndex}";
}
} public static class NPOIExtensions
{
public static IEnumerable<IPicture> GetPictures(this ISheet sheet)
{
var dp = sheet.DrawingPatriarch; // Excel 2003
if (dp is HSSFPatriarch patriarch)
{
return patriarch.GetShapes().Select(x =>
{
if (x is HSSFPicture picture)
{
return picture;
}
else
{
return new HSSFPicture(x, x.Anchor);
}
}).Cast<IPicture>();
} // Excel 2007 above
if (dp is XSSFDrawing dr)
{
//TODO: How convert XSSFSimpleShape to XSSFPicture ???
return dr.GetShapes().Where(x => x is XSSFPicture).Cast<IPicture>();
} throw new NotSupportedException($"Unsupported DrawingPatriarch object type:{dp.GetType().FullName}");
} public static PictureAnchor GetPictureAnchor(this IPicture picture)
{
var anchor = picture.ClientAnchor;
return new PictureAnchor(anchor.Col1, anchor.Col2, anchor.Row1, anchor.Row2);
}
}

PS: WPS的插入图片函数 DISPIMG,是 WPS 特有的,不属于 Office 规范,因此获取的坐标不准。

NPOI获取Excel文件里的形状/图片的坐标/锚点的更多相关文章

  1. C#利用NPOI操作Excel文件

    NPOI作为开源免费的组件,功能强大,可用来读写Excel(兼容xls和xlsx两种版本).Word.PPT文件.可是要让我们记住所有的操作,这便有点困难了,至此,总结一些在开发中常用的针对Excel ...

  2. 使用NPOI导出Excel文件

    使用NPOI导出Excel文件,本实例使用了ASP.NET MVC. 1.使用NPOI导出Excel文件 实例:导出商品列表. 要求:1.通过NPOI导出导出商品列表信息: 2.使用Excel函数计算 ...

  3. VB6.0 获取Excel文件工作表Sheet的名称

    获取Excel文件工作表Sheet的名称 '产生Excel文档 Dim xlapp, xlbook As Object Dim sSheetName As String Set xlapp = Cre ...

  4. C#可以获取Excel文件中Sheet的名字

    C#可以获取Excel文件中Sheet的名字吗 C#可以获取Excel文件中Sheet的名字吗 我试过WPS的表格可以 可以 要代码么 百度都有 [深圳]Milen(99696619)  14:13: ...

  5. C#中获取Excel文件的第一个表名

    //    2.以数据库方式打开并输入数据//      此方式将xls文件所在目录看作数据库,其中的xls文件看作数据库表,表名即文件名(不加扩展名).//      函数importExcelTo ...

  6. asp.net 使用NPOI读取excel文件

    asp.net 使用NPOI读取excel文件内容 NPOI下载地址:NPOI public class ExcelHelper { /// <summary> /// 读取Excel文件 ...

  7. asp.net Mvc 使用NPOI导出Excel文件

    1.新建MVC项目,新建控制器.视图 添加控制器: 添加视图(将使用布局页前面的复选框里的勾勾去掉) 2.在Models里新建一个类 public class Shop { /// <summa ...

  8. 基于Vue + axios + WebApi + NPOI导出Excel文件

    一.前言 项目中前端采用的Element UI 框架, 远程数据请求,使用的是axios,后端接口框架采用的asp.net webapi,数据导出成Excel采用NPOI组件.其业务场景,主要是列表页 ...

  9. excel to datatable (c#用NPOI将excel文件内容读取到datatable数据表中)

    将excel文件内容读取到datatable数据表中,支持97-2003和2007两种版本的excel 1.第一种是根据excel文件路径读取excel并返回datatable /// <sum ...

  10. NPOI对excel文件的导入导出

    现理解:将一个Excel文件(工作簿-IWorkBook)看做是一个你要操作的对象,每个工作簿包含多个工作表(ISheet)对象,每个工作表中又包含多个行对象(IRow),每行又包含多个单元格(ICe ...

随机推荐

  1. PHP插件

  2. Django错误:ERRORS: ?: (staticfiles.E001) The STATICFILES_DIRS setting is not a tuple or list. HINT: Perhaps you forgot a trailing comma?

    报错的原因是因为我们的STATICFILES_DIRS赋值时,形式不对,其应该赋数组对象,具体如下: 找到settings.py文件, 把 STATICFILES_DIRS=(os.path.join ...

  3. 常用c++ STL 汇总

    常用STL: vector 变长数组,倍增的思想 初始化: //初始化 vector<int> a; vector<int> a(n); vector<int> a ...

  4. Programming abstractions in C阅读笔记:p84-p87

    <Programming Abstractions In C>学习第43天,p84-p87总结. 一.技术总结 1.record record也称为structure(结构体),是一种数据 ...

  5. VMware中的虚拟机Debian10的服务器配置,使主机(win10)能够通过本地域名(如www.xxx.com)访问该服务器

    VMware中的虚拟机Debian10的服务器配置,使主机(win10)能够通过本地域名(如www.xxx.com)访问该服务器 安装过程 下载debian-10.13.0-amd64-DVD-1.i ...

  6. [kubernetes]二进制部署k8s集群-基于containerd

    0. 前言 k8s从1.24版本开始不再直接支持docker,但可以自行调整相关配置,实现1.24版本后的k8s还能调用docker.其实docker自身也是调用containerd,与其k8s通过d ...

  7. [Lua] 实现所有类的基类Object、模拟单继承OO、实现抽象工厂

    所有类的基类 Object Lua 没有严格的 oo(Object-Oriented)定义,可以利用元表特性来实现 先定义所有类的基类,即Object类.代码顺序从上到下,自成一体.完整代码 定义一个 ...

  8. IDEA集成码云gitee

    参考链接:https://blog.csdn.net/bing_bg/article/details/106437008 1.下载安装git https://git-scm.com/download ...

  9. Python初步了解装饰器

    Python初步了解装饰器 装饰器的概念 装饰器的简单使用 装饰器的进阶 装饰器的练习 装饰器的固定模块 装饰器的语法糖 装饰器的概念 装饰器它不是一个新的知识点,它是有之前我们学习的名称空间.函数嵌 ...

  10. WorkPress使用BackWPup插件备份后手动还原方法记录

    前提 拿到BackWPup插件备份的zip包(下文均以backup.zip来指代).这个是备份包是事先从源WorkPress上备份好的. 环境 OS:Centos7.9 Apache:2.4.6 PH ...