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

实测下来,可以正确获取 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. pyinstaller 安装报错,环境是python3.7

    在pycharm中安装,和直接输入pip install pyinstaller 均报错, 最后,输入pip install -i https://pypi.douban.com/simple/ py ...

  2. Java源代码是如何编译,加载到内存中的?

    1.前言 相信许多开发同学看过<深入理解java虚拟机>,也阅读过java虚拟机规范,书籍和文档给人的感觉不够直观,本文从一个简单的例子来看看jvm是如何工作的吧. 本文所有操作均在mac ...

  3. Strategy Pattern and Polymorphism —— Behavioral Class

    策略模式着重于封装和替换 不同的算法或行为,以便在运行时进行选择. Simple example - Computer and USB interface 现代人对计算机.USB接口还有各种设备之间的 ...

  4. 分布式环境下Session共享问题解决和原理讲解

    1.分布式环境下Session共享问题: 2.几种解决方法 3.通过后端统一存储方法在实际项目中问题的体现: 当session的作用域只限于auth.gulimall.com时,在auth.gulim ...

  5. MyBatis-Plus和PageHelper冲突导致Factory method sqlSessionFactory threw exception,并且如何分页显示全部

    springboot开始引入了mybaits-plus.后来想引入pagehelper进行分页,引入之后报错 Error starting ApplicationContext. To display ...

  6. 大模型时代,如何快速开发AI应用

    本文分享自华为云社区 <[云享问答]第3期:大模型时代,如何快速开发AI应用>,作者:华为云社区精选. 大模型快速普及应用的当下,AI浪潮汹涌而至,对于开发者来说,开发一款属于自己的AI应 ...

  7. Solution -「CF 1477A」Nezzar and Board

    Description Link. $ n $ distinct integers $ x_1,x_2,\ldots,x_n $ are written on the board. Nezzar ca ...

  8. MySQL高级13-MySQL管理工具

    一.系统数据库 MySQL数据库安装完成后,自带了四个数据库: mysql数据库:存储MySQL服务器正常运行所需要的各种信息如时区.主从.用户.权限等 infomation_schema:提供了访问 ...

  9. 《HelloGitHub》第 90 期

    兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣.入门级的开源项目. https://github.com/521xueweiha ...

  10. WebKit Inside: CSS 样式表的匹配时机

    WebKit Inside: CSS 的解析 介绍了 CSS 样式表的解析过程,这篇文章继续介绍 CSS 的匹配时机. 无外部样式表 内部样式表和行内样式表本身就在 HTML 里面,解析 HTML 标 ...