News Category

C#/VB.NET: Extract Text and Images from Excel Shapes

2023-07-19 08:05:00 Written by  support iceblue
Rate this item
(0 votes)

Shapes in Excel serve as visual elements that can decorate or optimize worksheets, including objects such as text boxes and images. By inserting shapes, users are able to present data in a more intuitive manner and emphasize vital information, ultimately improving the readability of the spreadsheets. When it becomes necessary to deal with the contents within the shapes independently, you can programmatically extract them from shapes for further processing. In this article, we will show you how to extract text and images from excel shapes by using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Extract Text from Excel Shapes

Spire.XLS for .NET allows users to extract text from shape object by using IPrstGeomShape.Text property and write it to a new .txt file. The following are detailed steps.

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the first worksheet by  Workbook.Worksheets[] property.
  • Get the second shape by Worksheet.PrstGeomShapes[] property.
  • Extract text content from the second shape and save it to the string variable.
  • Create a StringBuilder object and append the extracted text to it.
  • Write the text to a .txt file using File.WriteAllText() method.
  • C#
  • VB.NET
using System.IO;
using System.Text;
using Spire.Xls;
using Spire.Xls.Core;

namespace Extracttext
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook workbook = new Workbook();

            //Load the Excel file
            workbook.LoadFromFile("sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get the second shape and extract text from it
            IPrstGeomShape shape1 = sheet.PrstGeomShapes[1];
            string s = shape1.Text;

            //Append the extracted text to StringBuilder object
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(s);

            //Write the text to a .txt file
            File.WriteAllText("ShapeText.txt", sb.ToString());
            workbook.Dispose();
        }
    }
}

C#/VB.NET: Extract Text and Images from Excel Shapes

Extract Images from Excel Shapes

Additionally, Spire.XLS for .NET also supports extracting the image by using IPrstGeomShape.Fill.Picture property and save it to a local folder. The related steps are as follows.

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the first worksheet by  Workbook.Worksheets[] property.
  • Get the first shape by Worksheet.PrstGeomShapes[] property.
  • Extract the image from the first shape by its Fill and Picture property.
  • Save the extracted image to a folder by using Image.Save() method.
  • C#
  • VB.NET
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Xls;
using Spire.Xls.Core;

namespace Extractimage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook workbook = new Workbook();

            //Load the Excel file
            workbook.LoadFromFile("sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get the first shape and extract the image from it
            IPrstGeomShape shape2 = sheet.PrstGeomShapes[0];
            Image image = shape2.Fill.Picture;

            //Save the extracted image to a folder
            image.Save(@"Image\ShapeImage.png", ImageFormat.Png);
            workbook.Dispose();
        }
    }
}

C#/VB.NET: Extract Text and Images from Excel Shapes

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Wednesday, 19 July 2023 00:57