微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 单元格中的Excel图像

如何将图像(图像类型)插入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,PixelFormats.Pbgra32);
bmp.Render(myViewPort);

myImage.source = bmp;
myImage.Stretch = Stretch.Uniform;

现在 ?
我希望

cell.Add(myImage)

但我认为这并不容易.

/斯特凡

感谢您的输入doitgood

以下代码适用于我

在我的情况下,我的图像源是一个视口(myViewPort)
图像的放置由单元确定

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,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 { }

解决方法

试试这个:
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

别忘了发布所有这些东西!

原文地址:https://www.jb51.cc/csharp/98635.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐