ASP.NET Fetch Image in Excel File

Menggunakan Interop.Excel ternyata mengalami kendala ketika akan mengambil image dari file excel. Banyak website menyarankan menggunakan Interop.Excel dan Clipboard. Solusi tersebut sepertinya berfungsi untuk desktop app, tapi tidak berfungsi di web app.

Solusi yang saat ini saya temukan adalah mengganti library dari Interop.Excel menjadi NPOI. NPOI bisa di download menggunakan NuGET.

Contoh kode untuk mengambil image dari excel:

[code language=”csharp”]
XSSFWorkbook book;
using (FileStream file = new FileStream(@"d:\temp\test.xlsx", FileMode.Open, FileAccess.Read))
{
book = new XSSFWorkbook(file);
}

ISheet sheet = book.GetSheet("logo");
var lst = book.GetAllPictures();
for (int i = 0; i < lst.Count; i++)
{
var pic = (XSSFPictureData)lst[i];
byte[] data = pic.Data;
BinaryWriter writer = new BinaryWriter(System.IO.File.OpenWrite(String.Format("D:\\temp\\asdf.jpeg")));
writer.Write(data);
writer.Flush();
writer.Close();
}
[/code]

Chandra Oemaryadi has written 244 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>