将图像从内存流添加到Excel文档

时间:2023-01-24 14:52:58

I have an image in a memory stream and I want to write this to an MS Excel document, the PIA only exposes the AddPicture method which takes a file path.

我在内存流中有一个图像,我想将其写入MS Excel文档,PIA只公开采用文件路径的AddPicture方法。

Is there away to add a picture without having to write the image to disc?

是否可以添加图片而无需将图像写入光盘?

MSDN

MSDN

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.shapes.addpicture(v=office.14).aspx

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.shapes.addpicture(v=office.14).aspx

1 个解决方案

#1


3  

Well, a bit of blind flying but assuming a thing or two about your code (e.g. the source of your stream, data type, etc) this could be a solution:
First, you need to create bitmap image data from the stream (which I assume is a byte stream, also assuming that the stream describes a bitmap image). There's a solution already for that here on Stack Overflow: Byte Array to Bitmap Image
I copy-paste the code from the solution:

好吧,有点盲目飞行,但假设你的代码有一两件事(例如你的流源,数据类型等),这可能是一个解决方案:首先,你需要从流中创建位图图像数据(我假设是字节流,也假设流描述了位图图像)。 Stack Overflow上有一个解决方案:Byte Array to Bitmap Image我从解决方案中复制粘贴代码:

int w= 100;
int h = 200;
int ch = 3; //number of channels (ie. assuming 24 bit RGB in this case)

byte[] imageData = new byte[whch]; //you image data here Bitmap bitmap = new Bitmap(w,h,PixelFormat.Format24bppRgb); BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); IntPtr pNative = bmData.Scan0; Marshal.Copy(imageData,0,pNative,whch); bitmap.UnlockBits(bmData);
Also assuming you have an object for your workbook and the worksheet you are about to work with, something like this:
xlBook = (Excel.Workbook)objExcel.Workbooks.Add("");
xlSheet = (Excel.Worksheet)xlBook.Worksheets1;
xlSheet.Activate();
Now that you have a Bitmap-type variable, and a worksheet, all you need is to paste the image to the sheet:
System.Windows.Forms.Clipboard.SetDataObject(bitmap, false);
xlsRange = xlSheet.get_Range((Excel.Range)xlSheet.Cells[5, 15], (Excel.Range)xlSheet.Cells[5, 15]);
xlSheet.Paste(xlsRange, bitmap);

So the key is this guy here (instead of using "AddPicture"): Worksheet.Paste Method

Hope this helps!

#1


3  

Well, a bit of blind flying but assuming a thing or two about your code (e.g. the source of your stream, data type, etc) this could be a solution:
First, you need to create bitmap image data from the stream (which I assume is a byte stream, also assuming that the stream describes a bitmap image). There's a solution already for that here on Stack Overflow: Byte Array to Bitmap Image
I copy-paste the code from the solution:

好吧,有点盲目飞行,但假设你的代码有一两件事(例如你的流源,数据类型等),这可能是一个解决方案:首先,你需要从流中创建位图图像数据(我假设是字节流,也假设流描述了位图图像)。 Stack Overflow上有一个解决方案:Byte Array to Bitmap Image我从解决方案中复制粘贴代码:

int w= 100;
int h = 200;
int ch = 3; //number of channels (ie. assuming 24 bit RGB in this case)

byte[] imageData = new byte[whch]; //you image data here Bitmap bitmap = new Bitmap(w,h,PixelFormat.Format24bppRgb); BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); IntPtr pNative = bmData.Scan0; Marshal.Copy(imageData,0,pNative,whch); bitmap.UnlockBits(bmData);
Also assuming you have an object for your workbook and the worksheet you are about to work with, something like this:
xlBook = (Excel.Workbook)objExcel.Workbooks.Add("");
xlSheet = (Excel.Worksheet)xlBook.Worksheets1;
xlSheet.Activate();
Now that you have a Bitmap-type variable, and a worksheet, all you need is to paste the image to the sheet:
System.Windows.Forms.Clipboard.SetDataObject(bitmap, false);
xlsRange = xlSheet.get_Range((Excel.Range)xlSheet.Cells[5, 15], (Excel.Range)xlSheet.Cells[5, 15]);
xlSheet.Paste(xlsRange, bitmap);

So the key is this guy here (instead of using "AddPicture"): Worksheet.Paste Method

Hope this helps!