Add Picture in Excel on Particular Cell with C#

大兔子大兔子 提交于 2019-12-04 19:55:02

you have to add picture like following

Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)ws.Cells[3, 1];
float Left = (float)((double)oRange.Left);
float Top = (float)((double)oRange.Top);
const float ImageSize = 32;
ws.Shapes.AddPicture("C:\\pic.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);

Aspose.Cells API can be used to add a picture in Excel on particular cell with C# or with other programming languages e.g. Java, C++ etc.

For demonstration, please see the following C# code and the Snapshot that shows the input Excel file and the output Excel file generated by Aspose.Cells API after the execution of the code. As you can see inside the snapshot, the cell C12 contains the picture.

Please also read the comments inside the code for more help.

C#

// Load input Excel file inside Aspose.Cells Workbook object.
Workbook wb = new Workbook("SampleAddPictureInExcelCell.xlsx");

// Access first worksheet.
Worksheet ws = wb.Worksheets[0];

// Access cell C12 by name.
Cell cell = ws.Cells["C12"];

// Add picture in Excel cell.
int idx = ws.Pictures.Add(cell.Row, cell.Column, "D:/Download/Penguins.jpg");

// Access the picture by index.
Picture pic = ws.Pictures[idx];

// Get the column width and row height of the cell in inches.
double w = ws.Cells.GetColumnWidthInch(cell.Column);
double h = ws.Cells.GetRowHeightInch(cell.Row);

// Adjust the picture width and height as per cell width and height.
pic.WidthInch = w;
pic.HeightInch = h;

// Save the workbook in output Excel file.
wb.Save("OutputAddPictureInExcelCell.xlsx", SaveFormat.Xlsx);

Snapshot showing the input Excel file and output Excel file generated by Aspose.Cells API. Here cell C12 contains the picture.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!