【问题标题】:Excel image in a cell单元格中的 Excel 图像
【发布时间】:2011-11-16 19:20:54
【问题描述】:

如何将图像(图像类型)插入 Excel 工作表中的特定单元格

taperSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item("Taper");

Microsoft.Office.Interop.Excel.Range cell = GetMyPictureCELL(taperSheet);

Image myImage = new Image();
RenderTargetBitmap bmp;

bmp = new RenderTargetBitmap((int)this.Width, (int)this.Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(myViewPort);

myImage.Source = bmp;
myImage.Stretch = Stretch.Uniform;

现在呢? 我希望

cell.Add(myImage)

但我认为这并不容易。

/斯蒂芬

感谢您的意见,doitgood

以下代码对我有用

在我的情况下,我的图像源是一个视口 (myViewPort) 图片的位置由cell决定

try
{
    Image myImage = new Image();
    RenderTargetBitmap bmp;
    PngBitmapEncoder encoder;
    string fileName;
    System.IO.Stream stream;
    object missing = System.Reflection.Missing.Value; 
    Microsoft.Office.Interop.Excel.Picture pic = null;
    Microsoft.Office.Interop.Excel.Pictures p = null;

    bmp = new RenderTargetBitmap((int)this.Width, (int)this.Height, 96, 96, PixelFormats.Pbgra32);
    bmp.Render(myViewPort);

    myImage.Source = bmp;
    myImage.Stretch = Stretch.Uniform;

    fileName = System.IO.Path.GetTempFileName();
    stream = System.IO.File.OpenWrite(fileName);

    encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bmp));
    encoder.Save(stream);
    stream.Close();

    p = taperSheet.Pictures(missing) as Microsoft.Office.Interop.Excel.Pictures; 
    pic = p.Insert(fileName, missing); 
    pic.Left = cell.Left;
    pic.Top = cell.Top;

}
catch { }

【问题讨论】:

    标签: c# wpf image excel cell


    【解决方案1】:

    试试这个:

    object missing = System.Reflection.Missing.Value;
    Excel.Range picPosition = GetPicturePosition(); // retrieve the range for picture insert
    Excel.Pictures p = yourWorksheet.Pictures(missing) as Excel.Pictures;
    Excel.Picture pic = null;
    pic = p.Insert(yourImageFilePath, missing);
    pic.Left = Convert.ToDouble(picPosition .Left);
    pic.Top = Convert.ToDouble(picPosition .Top);
    pic.Placement = // Can be any of Excel.XlPlacement.XYZ value
    

    别忘了释放所有这些东西!

    【讨论】:

    • 不能直接将图片“发送”到 Excel,是否必须“越过”文件系统?
    • 我更新了我的答案以允许将图像放置在 Excel.Range 中。对于插入不是来自文件系统的图片,我将进行一些测试并在相关时提供结果。注意:该代码尚未在每种情况下都经过测试...您还可以查看这篇建议使用 Worksheet.Shapes.AddPicture 而不是 WorkSheet.Pictures stackoverflow.com/questions/521979/… 的帖子
    • 据我所知,除了文件系统之外,无法插入图片,但是如果您想购买第三方组件,Aspose 和 Synfusion 都在做您需要的事情,而无需正在安装 Excel。
    • 如果我能回答这个问题,我会给你 100 分。它使我解决了使用 .COM 版本使用 Interop 将图像放入 Excel 的一个多月老问题。它在开发方面工作得很好,但由于服务器没有安装 Office,所以会在现场发生爆炸。通过这项工作,一切正常!
    【解决方案2】:

    或者试试这个:

    private void PlacePicture(Image picture, Range destination)
    {
        Worksheet ws = destination.Worksheet;
        Clipboard.SetImage(picture);
        ws.Paste(destination, false);
        Pictures p = ws.Pictures(System.Reflection.Missing.Value) as Pictures;
        Picture pic = p.Item(p.Count) as Picture;
        ScalePicture(pic, (double)destination.Width, (double)destination.Height);
    }
    
    private void ScalePicture(Picture pic, double width, double height)
    {
        double fX = width / pic.Width;
        double fY = height / pic.Height;
        double oldH = pic.Height;
        if (fX < fY)
        {
            pic.Width *= fX;
            if (pic.Height == oldH) // no change if aspect ratio is locked
                pic.Height *= fX;
            pic.Top += (height - pic.Height) / 2;
        }
        else
        {
            pic.Width *= fY;
            if (pic.Height == oldH) // no change if aspect ratio is locked
                pic.Height *= fY;
            pic.Left += (width - pic.Width) / 2;
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多