Images are highly effective in conveying thoughts and ideas. Sometimes, you may need to insert images into an Excel report so that audiences can grasp your intentions quickly and clearly. In this article, you will learn how to insert image into Excel in C# and VB.NET using Spire.XLS for .NET library.
- Insert Image from Disk into Excel in C# and VB.NET
- Insert Web Image from a URL into Excel in C# and VB.NET
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Insert Image from Disk into Excel in C# and VB.NET
The following are the steps to insert an image from disk into Excel:
- Initialize a Workbook instance.
- Get the desired worksheet using Workbook.Worksheets[sheetIndex] property.
- Insert an image into the worksheet using the Worksheet.Pictures.Add() method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls; namespace InsertImageInExcel { class Program { static void Main(string[] args) { //Initialize a Workbook instance Workbook workbook = new Workbook(); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Insert image into the worksheet sheet.Pictures.Add(1, 1, @"E:\work\sample.jpg"); //Save the result file workbook.SaveToFile("InsertImageFromDisk.xlsx", ExcelVersion.Version2016); } } }
Insert Web Image from a URL into Excel in C# and VB.NET
The following are the steps to insert a web image from a URL into Excel:
- Initialize a Workbook instance.
- Get the desired worksheet using Workbook.Worksheets[sheetIndex] property.
- Initialize a WebClient instance, then download the web image as a byte array from the specified URL using WebClient.DownloadData(urlAddress) method.
- Initialize a MemoryStream instance from the byte array.
- Create an Image object from the memory stream then insert it into the worksheet using Worksheet.Pictures.Add() method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls; using System.Drawing; using System.IO; using System.Net; namespace InsertWebImageInExcel { class Program { static void Main(string[] args) { //Initialize a Workbook instance Workbook workbook = new Workbook(); //Get the first sheet Worksheet sheet = workbook.Worksheets[0]; //Specify the image URL string URL = "https://www.e-iceblue.com/downloads/demo/Logo.png"; //Initialize a WebClient instance WebClient webClient = new WebClient(); //Download the image as a byte array from the URL byte[] imageData = webClient.DownloadData(URL); //Initialize a MemoryStream instance from the byte array MemoryStream objImage = new MemoryStream(imageData); //Create an Image object from the memory stream Image image = Image.FromStream(objImage); //Insert the image into the worksheet sheet.Pictures.Add(3, 2, image); //Save the result file workbook.SaveToFile("InsertWebImage.xlsx", ExcelVersion.Version2016); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.