【问题标题】:How to align image in excel如何在excel中对齐图像
【发布时间】:2017-09-29 19:43:49
【问题描述】:

我使用以下代码在 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.XlVAlign 而不是 Excel.XlHAlign

标签: c# excel winforms interop


【解决方案1】:

图片粘贴到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;
 }

【讨论】:

  • 先生,但是当列大小不够大然后图像对齐受到干扰并且看起来比以前不好时会出现一些问题
  • 如果图片太大,我们可以尝试在粘贴之前调整列的大小吗?
  • 我使用 excelWorkSheet.Columns.AutoFit();
猜你喜欢
  • 2013-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 2020-01-02
  • 2011-10-05
  • 2012-06-29
相关资源
最近更新 更多