在细胞中的Excel图像 [英] Excel image in a cell

查看:155
本文介绍了在细胞中的Excel图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要如何插入(类型图像)的图像为特定的细胞在Excel工作表

  taperSheet =(Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(锥);Microsoft.Office.Interop.Excel.Range细胞= GetMyPictureCELL(taperSheet);图片MYIMAGE =新的图像();
RenderTargetBitmap BMP;BMP =新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

以下code对我的作品

在我的情况下,我的图片来源视域(myViewPort)
图像的位置由细胞determinated

 尝试
{
    图片MYIMAGE =新的图像();
    RenderTargetBitmap BMP;
    PngBitmapEn codeR EN codeR;
    字符串文件名;
    的System.IO.Stream流;
    反对失踪= System.Reflection.Missing.Value;
    Microsoft.Office.Interop.Excel.Picture PIC = NULL;
    Microsoft.Office.Interop.Excel.Pictures P = NULL;    BMP =新RenderTargetBitmap((INT)this.Width,(INT)this.Height,96,96,PixelFormats.Pbgra32);
    bmp.Render(myViewPort);    myImage.Source = BMP;
    myImage.Stretch = Stretch.Uniform;    文件名= System.IO.Path.GetTempFileName();
    流= System.IO.File.OpenWrite(文件名);    EN codeR =新PngBitmapEn codeR();
    EN coder.Frames.Add(BitmapFrame.Create(BMP));
    EN coder.Save(流);
    stream.Close();    P = taperSheet.Pictures(失踪)为Microsoft.Office.Interop.Excel.Pictures;
    PIC = p.Insert(文件名,丢失);
    pic.Left = cell.Left;
    pic.Top = cell.Top;}
赶上{}


解决方案

试试这个:

 对象缺少= System.Reflection.Missing.Value;
Excel.Range picPosition = GetPicturePosition(); //检索图片插入范围
Excel.Pictures P = yourWorksheet.Pictures(失踪)为Excel.Pictures;
Excel.Picture PIC = NULL;
PIC = p.Insert(yourImageFilePath,丢失);
pic.Left = Convert.ToDouble(picRange.Left);
pic.Top = picRange.Top;
pic.Placement = //可以是任何Excel.XlPlacement.XYZ价值

和不要忘记释放所有的东西!

How do I insert an image (of type Image) into a specific cell in a Excel sheet

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;

and now ? I was hoping for

cell.Add(myImage)

But I assume it is not that easy.

/Stefan

Thanks for your input doitgood

The following code works for me

In my case my Image source is a viewport (myViewPort) The placement of the image is determinated by 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 { }

解决方案

Try this:

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(picRange.Left);
pic.Top = picRange.Top;
pic.Placement = // Can be any of Excel.XlPlacement.XYZ value

And don't forget to release all that stuff!

这篇关于在细胞中的Excel图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆