gpt4 book ai didi

c# - 单元格中的Excel图像

转载 作者:太空狗 更新时间:2023-10-29 20:53:20 25 4
gpt4 key购买 nike

如何将图像(图像类型)插入 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

下面的代码适合我

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

最佳答案

试试这个:

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

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

关于c# - 单元格中的Excel图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7413107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com