How to add image to excel using NPOI?

mc 4,111 Reputation points
2021-05-13T09:55:33.667+00:00

I want to download image and add it to excel

but there is no image in the excel.

why?

(XSSFPicture)drawing.CreatePicture(anchor, beginPhotoId); I have created the picture.

and I can not use picture.Resize becuase there will be an error:
Unable to load shared library 'libgdiplus' or one of its dependencies. In order to help diagnose loading problems

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,650 questions
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,586 Reputation points
    2021-05-14T06:00:13.027+00:00

    Are you considering using other nuget packages?

    This is an example, add a reference after installing Microsoft.Office.Interop.Excel.

    96619-1.png

    Then try the following code:

                Application application = new Application();  
                Workbook workbook = application.Workbooks.Open(@"D:\test\excel\3.xlsx");  
                Worksheet worksheet = (Worksheet)workbook.ActiveSheet;  
                try  
                {  
                    string imageNetworkLocation = "https://www.gravatar.com/avatar/c5c9adf8dbcec1537e8e60ed8a6d1f13?s=328&d=identicon&r=PG&f=1";  
                    worksheet.Shapes.AddPicture(imageNetworkLocation, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 0, 0, 85, 85);  
      
                }  
                catch (Exception e)  
                {  
                    Console.WriteLine(e.Message);  
                }  
                finally   
                {  
                    workbook.Save();  
                    workbook.Close();  
                    application.Quit();  
                }  
    

    If you must use NPIO for some reasons, please provide a complete piece of code that can reproduce the problem for us to test.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful