How to read image from Excel file when user uploads Excel file

Last Reply 8 days ago By dharmendr

Posted 10 days ago

Is it possible

You are viewing reply posted by: dharmendr 8 days ago.
Posted 8 days ago

Hi Arvindk,

Refer below sample code. I have used Spire.Xls library to read images from Excel file. 

For more details refer below link.

https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Image/How-to-Extract-Image-from-Excel-Worksheet-in-C-VB.NET.html

You need to add the dll in your project using Nuget Package Manager console by searching spire.xls.

Once the dll added use below code.

HTML

<asp:FileUpload ID="fuExcel" runat="server" />
<asp:Button Text="Read Image" runat="server" OnClick="OnReadImage" />
<hr />
<asp:Panel ID="pnlImages" runat="server" />

Namespaces

C#

using System.Drawing.Imaging;
using Spire.Xls;

VB.Net

Imports System.Drawing.Imaging
Imports Spire.Xls

Code

C#

Imports System.Drawing.ImagingImports Spire.Xls

protected void OnReadImage(object sender, EventArgs e)
{
    Workbook workbook = new Workbook();
    workbook.LoadFromFile(fuExcel.PostedFile.FileName);
    Worksheet sheet = workbook.Worksheets[0];

    for (int i = 0; i < sheet.Pictures.Count; i++)
    {
        ExcelPicture picture = sheet.Pictures[i];
        picture.Picture.Save(Server.MapPath("~/Image/" + picture.Name.Replace(" ", "_") + ".jpg"), ImageFormat.Jpeg);
        Image img = new Image();
        img.ImageUrl = "/Image/" + picture.Name.Replace(" ", "_") + ".jpg";
        pnlImages.Controls.Add(img);
        pnlImages.Controls.Add(new Literal { Text = "<br/>" });
    }
}

VB.Net

Protected Sub OnReadImage(ByVal sender As Object, ByVal e As EventArgs)
    Dim workbook As Workbook = New Workbook()
    workbook.LoadFromFile(fuExcel.PostedFile.FileName)
    Dim sheet As Worksheet = workbook.Worksheets(0)
    For i As Integer = 0 To sheet.Pictures.Count - 1
        Dim picture As ExcelPicture = sheet.Pictures(i)
        picture.Picture.Save(Server.MapPath("~/Image/" & picture.Name.Replace(" ", "_") & ".jpg"), ImageFormat.Jpeg)
        Dim img As Image = New Image()
        img.ImageUrl = "/Image/" & picture.Name.Replace(" ", "_") & ".jpg"
        pnlImages.Controls.Add(img)
        pnlImages.Controls.Add(New Literal With {.Text = "<br/>"})
    Next
End Sub

Screenshots

The Excel

The Page