单元格中的Excel图像 [英] Excel image in a cell
本文介绍了单元格中的Excel图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将图像(Image类型)插入Excel表单中的特定单元格
taperSheet =(Microsoft .Office.Interop.Excel.Worksheet)excelSheets.get_Item( 锥度);
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)
/ pre>
但我认为这不是那么容易。
/ Stefan
感谢您的输入doitgood
以下代码适用于我
我的情况我的图像源是一个视口(myViewPort)
图像的位置由单元格决定try
{
Image myImage = new Image();
RenderTargetBitmap bmp;
PngBitmapEncoder编码器;
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(); //检索图片插入的范围
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 = //可以是任何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(picPosition .Left); pic.Top = Convert.ToDouble(picPosition .Top); pic.Placement = // Can be any of Excel.XlPlacement.XYZ value
And don't forget to release all that stuff!
这篇关于单元格中的Excel图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文