如何在 excel 中对齐图像

How to align image in excel

我使用以下代码在 excel 单元格中插入图像,但结果不如预期

bitmap[] ImageArray = GetImageArray();
bitmap currvalue = null;
System.Windows.Forms.Clipboard.SetDataObject(ImageArray[0], true);
currvalue = ImageArray[0];

currentRange.Cells.HorizontalAlignment =Excel.XlHAlign.xlHAlignCenterAcrossSelection; 
currentRange.Cells.VerticalAlignment = Excel.XlHAlign.xlHAlignCenter;

excelWorkSheet.Paste(currentRange.Cells, currvalue);
excelWorkSheet.Columns.AutoFit();

this is output

我无法使用 objSheet.Shapes.AddPicture();

我希望图片居中对齐

将图像粘贴到excel seheet 后,需要通过代码调整图像在单元格中的位置。类似下面的内容,HTH

excelWorkSheet.Paste(currentRange.Cells, currvalue);

foreach (var shp in excelWorkSheet.Shapes)
{
    if (shp.TopLeftCell.Address != currentRange.Address)
        continue;
    shp.Left = shp.TopLeftCell.Left + (shp.TopLeftCell.Width - shp.Width) / 2;
    shp.Top = shp.TopLeftCell.Top + (shp.TopLeftCell.Height - shp.Height) / 2;
    break;
 }