Add Image in Worksheet

Aspose.Cells - Add Image in Worksheet

C#

  

 //Instantiating a Workbook object  

Workbook workbook = new Workbook();  

//Adding a new worksheet to the Workbook object  

WorksheetCollection worksheets = workbook.Worksheets;  

Worksheet worksheet = worksheets.Add("My Worksheet");  

//Insert a string value to a cell  

worksheet.Cells["C2"].Value = "Image";  

//Set the 4th row height  

worksheet.Cells.SetRowHeight(3, 150);  

//Set the C column width  

worksheet.Cells.SetColumnWidth(2, 50);  

//Add a picture to the C4 cell  

int index = worksheet.Pictures.Add(3, 2, 3, 2, "../../data/aspose.png");  

//Get the picture object  

//Picture pic = worksheet.getPictures().get(index);  

Picture pic = worksheet.Pictures[index];  

//Set the placement type  

pic.Placement = PlacementType.FreeFloating;  

workbook.Save("../../data/image.xlsx");  

NPOI - HSSF XSSF - Add Image in Worksheet

C#

  

 IWorkbook wb = new XSSFWorkbook();  

ISheet sheet1 = wb.CreateSheet("First Sheet");  

// Add picture data to this workbook.  

byte[] bytes = File.ReadAllBytes("../../data/aspose.png");  

int pictureIdx = wb.AddPicture(bytes, PictureType.PNG);  

ICreationHelper helper = wb.GetCreationHelper();  

// Create the drawing patriarch.  This is the top level container for all shapes.  

IDrawing drawing = sheet1.CreateDrawingPatriarch();  

// Add a picture shape  

IClientAnchor anchor = helper.CreateClientAnchor();  

// Set top-left corner of the picture,  

// subsequent call of Picture#resize() will operate relative to it  

anchor.Col1 = 3;  

anchor.Row1 = 2;  

IPicture pict = drawing.CreatePicture(anchor, pictureIdx);  

// Auto-size picture relative to its top-left corner  

pict.Resize();  

FileStream sw = File.Create("../../data/image.xlsx");  

wb.Write(sw);  

sw.Close();  

Download Running Code

Download Add Image in Worksheet from any of the below mentioned social coding sites: