question

6666666 avatar image
0 Votes"
6666666 asked TimonYang-MSFT commented

How to add image to excel using NPOI?

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

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.

0 Votes 0 ·

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

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.


1.png (16.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.