Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fileformat-cells-gists/922373a68e97c04684ce6f6d8a5a9711 to your computer and use it in GitHub Desktop.
Save fileformat-cells-gists/922373a68e97c04684ce6f6d8a5a9711 to your computer and use it in GitHub Desktop.
Code snippet to extract and save images from a worksheet to a specified directory
using FileFormat.Cells;
class Program
{
static void Main(string[] args)
{
string filePath = "Z:\\Downloads\\spreadsheet_test.xlsx"; // The path to your workbook
string outputDirectory = "Z:\\Downloads\\ExtractedImages"; // The directory where you want to save the extracted images
// Load the workbook from the specified file path
Workbook wb = new Workbook(filePath);
// Select the first worksheet from the workbook
var worksheet = wb.Worksheets[0];
// Extract images from the worksheet
var images = worksheet.ExtractImages();
// Check if the output directory exists; if not, create it
if (!Directory.Exists(outputDirectory))
{
Directory.CreateDirectory(outputDirectory);
}
// Loop through each extracted image
foreach (var image in images)
{
// Construct a unique file path for the image using a GUID
var outputFilePath = Path.Combine(outputDirectory, $"Image_{Guid.NewGuid()}.{image.Extension}");
// Save the image data to the constructed file path
using (var fileStream = File.Create(outputFilePath))
{
image.Data.CopyTo(fileStream);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment