Click or drag to resize

Adding Images to a Worksheet with ExpertXls

Using the ExpertXls Excel Library for .NET you can easily add images to a worksheet. The worksheet has a collection of pictures represented by the Pictures property of the ExcelWorksheet class. A new picture can be added to this collection using the AddPicture() method. When adding a picture you can specify the range in the worksheet where this picture will be placed and the image to be added either as an image file path or as a .NET System.Drawing.Image object.

Code Samples

The sample code below ilustrates how easy it is to add an image to a worksheet:

// get a System.Drawing.Image object
string demoImagesPath = System.IO.Path.Combine(Server.MapPath("~"), @"Images");
System.Drawing.Image demoImage = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "demo_250x300.jpg"));

// add an image specifing the top left corner
ExcelPicture demoExcelPicture = worksheet.Pictures.AddPicture(1, 7, demoImage);

// get the coordinates of the image
int leftColumnIndex = demoExcelPicture.LeftColumnIndex;
int topRowIndex = demoExcelPicture.TopRowIndex;
int rightColumnIndex = demoExcelPicture.RightColumnIndex;
int bottomRowIndex = demoExcelPicture.BottomRowIndex;

string excelPictureSumary = String.Format("Left Column: {0}, Top Row: {1}, Right Column: {2}, Bottom Row: {3}",
    leftColumnIndex, topRowIndex, rightColumnIndex, bottomRowIndex);