Spire.XLS 教程:从C#的Excel形状中提取文本和图像
生活随笔
收集整理的這篇文章主要介紹了
Spire.XLS 教程:从C#的Excel形状中提取文本和图像
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
一個(gè)excel形狀可以用文字或圖像填充,有時(shí)我們需要讀取形狀中的文字和圖像信息。 在本文中,我們將介紹如何使用Spire.XLS和C#從Excel中的形狀中提取文本和圖像。
以下是我們用于演示的示例文檔的屏幕截圖:
詳細(xì)步驟:
Step 1: 初始化Workbook類的對(duì)象并加載Excel文件。
Workbook workbook = new Workbook(); workbook.LoadFromFile("Input.xlsx");Step 2: 獲取第一張工作表。
Worksheet sheet = workbook.Worksheets[0];Step 3: 從第一個(gè)形狀中提取文本并保存到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: 從第二個(gè)形狀中提取圖像并保存到本地文件夾。
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);}} }慧都控件網(wǎng)
轉(zhuǎn)載于:https://my.oschina.net/u/3006003/blog/1593793
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Spire.XLS 教程:从C#的Excel形状中提取文本和图像的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开发:异常收集之 ibatis+Orac
- 下一篇: php基础2