spire.xls组件支持给excel形状填充文本和图片,同时也支持从形状中提取填充的文本和图片。本文将介绍如何使用spire.xls提取excel形状中的文本和图片。
原文档截图如下:
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("addshapes.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//从第一个形状中提取文本并保存到txt文件中
iprstgeomshape shape1 = sheet.prstgeomshapes[0];
string s = shape1.text;
stringbuilder sb = new stringbuilder();
sb.appendline(s);
file.writealltext("shapetext.txt", sb.tostring());
//从第二个形状中提取图片并保存到本地文件夹
iprstgeomshape shape2 = sheet.prstgeomshapes[1];
image image = shape2.fill.picture;
image.save(@"image\shapeimage.png", imageformat.png);
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("addshapes.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'从第一个形状中提取文本并保存到txt文件中
dim shape1 as iprstgeomshape = sheet.prstgeomshapes(0)
dim s as string = shape1.text
dim sb as new stringbuilder()
sb.appendline(s)
file.writealltext("shapetext.txt", sb.tostring())
'从第二个形状中提取图片并保存到本地文件夹
dim shape2 as iprstgeomshape = sheet.prstgeomshapes(1)
dim image as image = shape2.fill.picture
image.save("image\shapeimage.png", imageformat.png)
提取的文本:
提取的图片: