以下是插入图片的代码,但是很奇怪,执行程序后,单无格的属性是居中的,但是图片就是不会居中,不知道是哪里出了问题.
希望高手告知,谢谢..i=10
xlsheet.Cells(i, 9).Select   
xlsheet.Cells(i, 9).HorizontalAlignment = xlCenter
xlsheet.Cells(i, 9).VerticalAlignment = xlCenter
Set p = xlsheet.Pictures.Insert("d:/images/products/b/dd.jpg")
p.Height = p.Height / 1.5
p.Width = p.Width / 1.5

解决方案 »

  1.   


    i=10 
    xlsheet.Cells(i, 9).Select  
    xlsheet.Cells(i, 9).HorizontalAlignment = xlCenter 
    xlsheet.Cells(i, 9).VerticalAlignment = xlCenter 
    Set p = xlsheet.Pictures.Insert("d:/images/products/b/dd.jpg") 
    'p.Height = p.Height / 1.5 
    'p.Width = p.Width / 1.5p.Left = (xlsheet.Cells(i, 9).Left - xlsheet.Cells(i, 9).Width / 2) - p.Width / 2
    p.Top = (xlsheet.Cells(i, 9).Top - xlsheet.Cells(i, 9).Height / 2) - p.Height / 2
       
      

  2.   

    因为你要居中.当你图片的大小大于单元格的时候:Left 与 Top 就会在单元格左方与上方.┌───────────────────┐
    │                   │
    │   图片              │
    │                   │
    │     ┌────┐        │
    │     │单元格  │        │
    │     └────┘        │
    │                   │
    │            图片     │
    │                   │
    └───────────────────┘像上面的图片.当图片大于单元格 (i,9)
    图片位置就会看起来在别的单元格了.但是你发现没有.相对于单元格平说.图片上下左右都是相等的.
     
      

  3.   

    您还画得图出来,谢谢您了.
    不过,我还是不是很明白.
    那如果我要这张图片在(i,9)单元格显示出来并居中,程序要如何写?我主要是想实现以下的功能:
    1.将单元格定于居中.我是用下面的代码:
    xlsheet.Cells(i, 9).Select  
    xlsheet.Cells(i, 9).HorizontalAlignment = xlCenter 
    xlsheet.Cells(i, 9).VerticalAlignment = xlCenter 2.因为实际的图片比较大,所以要先将图片的长度\高度都除以1.5.我是用下面的代码:
    p.Height = p.Height / 1.5 
    p.Width = p.Width / 1.5感觉我的代码应该是没有错的,如果xlsheet.Cells(i, 9).Select里面的内容我改成文字是会居中的.
    但是就是一用图片,这个图片居中功能就是没实现.
      

  4.   

    你是想让图片的大小小于单元格?单元格很小,如果图片大了,就将图片缩小.单元格很大,如果图片小了,就将图片放大.
    也就是将图片按照单元格自动适配大小?如果是上面的意思.就用下面代码i=10 
    xlsheet.Cells(i, 9).Select  
    xlsheet.Cells(i, 9).HorizontalAlignment = xlCenter 
    xlsheet.Cells(i, 9).VerticalAlignment = xlCenter 
    Set p = xlsheet.Pictures.Insert("d:/images/products/b/dd.jpg") '***************************************
    p.Height = xlsheet.Cells(i, 9).Height
    p.Width = xlsheet.Cells(i, 9).Width'如果直接上面那样,可以会改变图片的大小配置.'****************************************'***************************************
    dim wScale  as Double
    dim hScale as doublehScale=xlsheet.Cells(i, 9).Height/p.height
    wScale=xlsheet.Cells(i, 9).Width/p.Widthif hScale>wScale then
       p.Height = p.Height*wScale
       p.Width = p.Width*wScale
    else
       p.Height = p.Height*hScale
       p.Width = p.Width*hScale
    end if'这样比较合适一点'****************************************p.Left = (xlsheet.Cells(i, 9).Left - xlsheet.Cells(i, 9).Width / 2) - p.Width / 2
    p.Top = (xlsheet.Cells(i, 9).Top - xlsheet.Cells(i, 9).Height / 2) - p.Height / 2