Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单元格中的Excel图像_C#_Wpf_Image_Excel_Cell - Fatal编程技术网

C# 单元格中的Excel图像

C# 单元格中的Excel图像,c#,wpf,image,excel,cell,C#,Wpf,Image,Excel,Cell,如何将图像(图像类型)插入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 =

如何将图像(图像类型)插入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)
但我想这并不是那么容易

/斯特凡

谢谢你的意见,做得很好

下面的代码适合我

在我的例子中,我的图像源是一个视口(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, (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 { }
试试这个:

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
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;
    }
}
别忘了发布所有这些东西

或者试试这个:

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;
    }
}
private void PlacePicture(图像图片,范围目标)
{
工作表ws=目的地。工作表;
剪贴板。设置图像(图片);
粘贴(目的地,false);
Pictures p=ws.Pictures(System.Reflection.Missing.Value)作为图片;
Picture pic=p.项目(p.计数)作为图片;
ScalePicture(pic,(双)destination.Width,(双)destination.Height);
}
私人空间比例图(图片图片,双倍宽度,双倍高度)
{
双fX=宽度/图片宽度;
双fY=高度/图片高度;
双倍oldH=图片高度;
如果(外汇<财年)
{
图片宽度*=fX;
if(pic.Height==oldH)//如果纵横比被锁定,则不会发生变化
图.高度*=fX;
图顶+=(高度-图高)/2;
}
其他的
{
图宽度*=fY;
if(pic.Height==oldH)//如果纵横比被锁定,则不会发生变化
图.高度*=fY;
图片左+=(宽度-图片宽度)/2;
}
}

是否无法将图片直接“发送”到Excel,是否必须“浏览”文件系统?我更新了答案,允许将图像放置在Excel.Range中。对于插入不是来自文件系统的图片,我将进行一些测试并提供相关结果。注意:该代码并没有在每个场景下都经过测试。。。你也可以看看这篇文章,它建议使用sheet.Shapes.AddPicture而不是sheet.Pictures。从我能找到的图片来看,除了从文件系统插入图片之外,不可能插入其他图片,但是如果你想购买第三方组件,Aspose和Synfusion都在做你需要的事情,而不需要安装Excel。如果我能回答这个问题,我会给你100分。它引导我使用.COM版本的Interop将图像放入Excel解决了一个多月的问题。它在开发端工作得很好,但在现场会爆炸,因为服务器并没有安装Office。有了这项工作,一切都顺利!谢谢你,Jürgen,这对我很有效,正如这里提到的: