欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Spire.XLS 教程:从C#的Excel形状中提取文本和图像

发布时间:2025/3/8 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Spire.XLS 教程:从C#的Excel形状中提取文本和图像 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘Python工程师标准>>>

一个excel形状可以用文字或图像填充,有时我们需要读取形状中的文字和图像信息。 在本文中,我们将介绍如何使用Spire.XLS和C#从Excel中的形状中提取文本和图像。

以下是我们用于演示的示例文档的屏幕截图:

详细步骤:

Step 1: 初始化Workbook类的对象并加载Excel文件。

Workbook workbook = new Workbook(); workbook.LoadFromFile("Input.xlsx");

Step 2: 获取第一张工作表。

Worksheet sheet = workbook.Worksheets[0];

Step 3: 从第一个形状中提取文本并保存到txt文件。

IPrstGeomShape shape1 = sheet.PrstGeomShapes[0]; string s = shape1.Text; StringBuilder sb = new StringBuilder(); sb.AppendLine(s); File.WriteAllText("ShapeText.txt", sb.ToString());

Step 4: 从第二个形状中提取图像并保存到本地文件夹。

IPrstGeomShape shape2 = sheet.PrstGeomShapes[1]; Image image = shape2.Fill.Picture; image.Save(@"Image\ShapeImage.png", ImageFormat.Png);

截图:

提取的文本:

提取的图像:

完整代码:

using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Text; using Spire.Xls; using Spire.Xls.Core;namespace Extract_text_and_image_from_Excel_shape {class Program{static void Main(string[] args){//Load the Excel fileWorkbook workbook = new Workbook();workbook.LoadFromFile("Input.xlsx");//Get the first worksheetWorksheet sheet = workbook.Worksheets[0];//Extract text from the first shape and save to a txt fileIPrstGeomShape shape1 = sheet.PrstGeomShapes[0];string s = shape1.Text;StringBuilder sb = new StringBuilder();sb.AppendLine(s);File.WriteAllText("ShapeText.txt", sb.ToString()); //Extract image from the second shape and save to a local folderIPrstGeomShape shape2 = sheet.PrstGeomShapes[1];Image image = shape2.Fill.Picture;image.Save(@"Image\ShapeImage.png", ImageFormat.Png);}} }

慧都控件网

转载于:https://my.oschina.net/u/3006003/blog/1593793

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是生活随笔为你收集整理的Spire.XLS 教程:从C#的Excel形状中提取文本和图像的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。