Extract Images from XLSX using C#

Extract Images from XLSX using C# is an essential task for developers who need to handle Excel files containing embedded images, charts, or other visual elements. Whether you’re building a data extraction tool, generating reports, or migrating content, being able to programmatically access images in XLSX files can be incredibly useful. Using C#, developers can easily retrieve and save these images, enabling applications to process and manipulate visual data stored within spreadsheets. This tutorial will guide you through the process, including the C# code to extract images from XLSX, making it simple for developers to integrate image extraction into their applications.

Steps to Extract Images from XLSX using C#

  1. Install the GroupDocs.Parser for .NET library in your C# project through NuGet to enable advanced image extraction capabilities from XLSX files
  2. Create a Parser object by passing the path of the target XLSX file to its constructor, allowing the library to load and process the document
  3. Call the Parser.GetImages method to obtain a collection of image objects from the XLSX file
  4. Ensure the image collection is not null to confirm that the document contains extractable images and that the extraction process is supported for this file type
  5. Iterate through the image collection, extract relevant properties such as image dimensions, type, and content, then save each image to disk

First, add the GroupDocs.Parser for .NET library to your project. This makes it easier to handle various document formats, including XLSX. After integrating the library, create a Parser object and load the XLSX file. Next, use the GetImages method to get a collection of images from the file. You can then loop through the collection, retrieve image details. Using the Parser library, developers can read and extract XLSX images with C# across various platforms, including Windows, macOS, and Linux. The library abstracts the complexities of the XLSX file format, enabling you to extract images efficiently and integrate this functionality into your automation or reporting workflows. This makes it a powerful tool for developers needing to process and extract images from large volumes of Excel files with minimal effort.

Code to Extract Images from XLSX using C#

using System;
using GroupDocs.Parser;
using GroupDocs.Parser.Data;
using System.Collections.Generic;
namespace ExtractImagesfromXLSXusingCSharp
{
internal class Program
{
static void Main(string[] args)
{
// Apply the license to remove the restrictions imposed by the Parser library
License lic = new License();
lic.SetLicense(@"GroupDocs.Parser.lic");
int i = 1;
// Create an instance of the Parser class to access its methods
// and properties for data processing or manipulation.
using (Parser parser = new Parser("input.xlsx"))
{
// Extract images from XLSX
IEnumerable<PageImageArea> images = parser.GetImages();
// Check if images extraction is supported
if (images == null)
{
Console.WriteLine("Images extraction isn't supported");
return;
}
// Iterate over images
foreach (PageImageArea image in images)
{
// Print a page index, rectangle and image type:
Console.WriteLine(string.Format("Page: {0}, R: {1}, Type: {2}",
image.Page.Index, image.Rectangle, image.FileType));
// Save the document to disk
image.Save("image" + i++ + image.FileType.Extension);
}
}
}
}
}

In conclusion, the XLSX image extraction C# tutorial demonstrates how developers can easily handle visual content within spreadsheets. After reading this article, the process becomes manageable, and the ability to extract images can be seamlessly integrated into various applications. This capability is useful for tasks such as generating image archives, analyzing data with embedded visuals, or automating content migration from XLSX to other formats. With the provided C# code, developers can efficiently implement and customize the solution to meet specific project needs, making it both reliable and effective.

We’ve previously published a guide on extracting images from DOCX files using C#. For a detailed, step-by-step tutorial, make sure to explore our complete guide on how to extract images from DOCX using C#.

 English