Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save images from Excel without using Clipboard, in C#?

I have looked into this fairly extensively, but have not found quite what I am looking for. The two methods I have found are

1: Using the Microsoft.Office.Interop.Excel, Iterate through the workbook's shapes, then copy the images to the clipboard and then take the clipboard data and put that into a bitmap, then finally save that bitmap. The problem with this method is the clipboard. We would like this to be used in a multi-threaded environment and are afraid of clipboard issues between threads. We would rather not deal with clipboard-locking.

2: Save the file as an .HTML file, then grab the images from the "_files" folder created where the document is saved to. The problem with this is that two images get created for each image (1 high res, 1 low res), and there is no good way to determine which images are low-res and which images are high-res, as they are all named image### and some files list them high, low, high, low, while some list them high, high, low, low. Using all of these files is slow and takes up space, which is not ideal. I could check images by aspect ratios, but this is obviously not great because multiple images could have the same aspect ratio.

Is there a way to parse the Excel.Shape directly as a bitmap (or any image format) without using the clipboard? It seems like there must be a way, because the Shape.CopyPicture method is able to send it to the clipboard in an image format. Otherwise, is there a way to do something similar to number 2 without getting duplicates? I would prefer a solution that avoids using 3rd party libraries.

Thank you.

like image 900
k.schroeder31 Avatar asked Jul 26 '11 20:07

k.schroeder31


1 Answers

I assume you use the Excel 2007/2010 format. Every Excel 2007/2010 file is a zip-file! So simply rename your file Excel.xlsx to Excel.zip. Then unzip the file to a folder. The images are in the folder '/xl/media'.

Try this manually. Knowing this, it's easy to automate this by writing a C# program for this.

like image 153
Chaim Zonnenberg Avatar answered Sep 28 '22 16:09

Chaim Zonnenberg