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)
提取的文本:
提取的图片: