SoFunction
Updated on 2025-03-06

c# Embed image streams into Excel implementation example using EPPlus

introduction

It is said that I haven't used EPPlus to generate Excel files for a long time. The article above was also 2019 (EPPlus secrets: Excel export chart export), and it was useful but all were very simple applications afterwards. Why did you mention it again this time? It is because it is good to use FastReport to make reports at the beginning, but later it is difficult to export to Excel and insert pictures into the file. Because the pictures are placed on the cloud server, the easiest way is to make a picture reference. But the problem is that after exporting Excel, it needs to be handled on the intranet (disconnected from the Internet). So what should I do? If you have difficulties, you have to overcome them, then you can only use our EPPlus

EPPlus secrets: Excel export chart export

Make a license

First of all, I will give you a real information, which is that the latest version of EPPlus has started to be commercially used to require license. How to solve it? It is simple to get a license. .

 = ;

This solves the License problem. OK, it’s time to get to the topic, because our pictures are placed on the cloud server, and it’s OK to directly quote them, but it won’t work without the Internet. What should I do?

The solution is very simple to embed the image into Excel, so how can I embed it? After studying it for hours, I finally got a idea.

Picture collection function

Convert images on the cloud server into a file stream and then write them to Excel via the file stream. How can I convert the cloud server's pictures into file streams? I have the function of writing an image collection before.

Core code

// Convert the image to a byte field public byte[] DownloadImageAsByteArray(string imageUrl)
 {
     using (var client = new HttpClient())
     {
         var response = (imageUrl).Result;
         return ().Result;
     }
 }
 MemoryStream imageData = new (DownloadImageAsByteArray(img_src));

The above DownloadImageAsByteArray method is to convert the image address into a byte field, and then write it to the MemoryStream class to complete the collection of the image.

How to get the write

Since the memory class has obtained the file stream of the image, how can I write it?

After checking EPPlus, I found that there are 2 methods:

  • 1. AddPicture is stored directly.
  • 2. AddPictureAsync literal parsing should be asynchronous.

Both of them are OK, but AddPictureAsync may be due to asynchronous reasons, and of course I may not find it. Anyway, I just use AddPicture, then SetSize and SetPosition to finish it. AddPictureAsync will not be tested in succession. If there are many pictures, you can study it again.

Let's go to the code

 string img_src = ps_list.("cloud://cloud1-8g4e2khfd2b8c508.636c-cloud1-8g4e2khfd2b8c508-1320236877", "");
 MemoryStream imageData = new (DownloadImageAsByteArray(img_src));
 ExcelPicture image = (, imageData);
 (300, 300);
 (rowIndex - 1, 30, 9, 30);

It is probably to convert the WeChat cloud image into http mode, then store the image into the file stream; then inject it into EPPlus, and then set the image size and location. It is worth mentioning here that SetPosition can have 4 parameters. The first Row is the row, the second is the position, the third is the column, and the last is the position. This perfectly embeds the image into Excel through EPPlus.

The above is the detailed content of the c# embedding image stream into Excel implementation example using EPPlus. For more information about embeding image stream into Excel for c# EPPlus, please pay attention to my other related articles!