原創|使用教程|編輯:王香|2017-12-22 17:07:26.000|閱讀 1129 次
概述:Spire.XLS 是一個專業的Excel控件,有 .NET、WPF和Silverlight 版本,通過Spire.XLS無需安裝微軟Excel,也能擁有Excel的全套功能,本文介紹了如何通過Spire.XLS從C#的Excel形狀中提取文本和圖像。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
一個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 file Workbook workbook = new Workbook(); workbook.LoadFromFile("Input.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Extract text from the first shape and save to a txt file IPrstGeomShape 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 folder IPrstGeomShape shape2 = sheet.PrstGeomShapes[1]; Image image = shape2.Fill.Picture; image.Save(@"Image\ShapeImage.png", ImageFormat.Png); } } }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn