如何使用VB2010将图片资源插入Excel表格 [英] How to insert picture from resources to Excel sheet, using VB2010

查看:723
本文介绍了如何使用VB2010将图片资源插入Excel表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用VB2010将图片添加到Excel工作表中,使用资源文件中的图片。我可以使用: xlDATAWorkSheet.Shapes.AddPicture(...)与路径编码,但我想从资源文件中使用它。
这是我试过的:
xlDATAWorkSheet.Shapes.AddPicture(CType(My.Resources.ResourceManager.GetObject(Logo),Bitmap),False,True, 0,0,300,50),但我收到一个错误:找不到文件(指向徽标字)*

I'm trying to add a picture to an Excel sheet, using a picture in the resources file, using VB2010. I can do it using: xlDATAWorkSheet.Shapes.AddPicture(...) with the path coded in, but I want to use it from the resources file. This is what I've tried: xlDATAWorkSheet.Shapes.AddPicture(CType(My.Resources.ResourceManager.GetObject("Logo"), Bitmap), False, True, 0, 0, 300, 50) , but I get an error: file not found (pointing to the Logo word)*

我还试过:
xlDATAWorkSheet.Shapes.AddPicture(My.Resources.Logo,False,True,0,0,300,50) ,但我得到相同的错误:文件未找到。

I also tried: xlDATAWorkSheet.Shapes.AddPicture(My.Resources.Logo, False, True, 0, 0, 300, 50) , but I get same error: file not found.

正确的图像硬编码的路径,如 xlDATAWorkSheet.Shapes.AddPicture(C:/Logo.jpg,False,True,0,0,300,50),但是我想从资源文件中使用它(所以它将运行在另一台机器上)

It works fine with the path to the picture hard coded, like xlDATAWorkSheet.Shapes.AddPicture("C:/Logo.jpg", False, True, 0, 0, 300, 50), but I want to use it from the resource file (so it will run on another machine)

此外,这样做可以,所以Logo图像真的在那里:

Also, this works ok, so Logo image is really there:

frmMain.pic1.Image = CType(My.Resources.ResourceManager.GetObject("Logo"), Image)

看来,根据MSDN文档的形状,似乎没有出现AddPictur e需要除字符串之外的任何其他字符串来确定要插入的图像。

It seems though, according to the MSDN docs for Shapes, there does not appear that AddPicture takes anything other than a string to determine what image you want to insert.

任何人都可以在VB2010中将资源插入到Excel工作表中?

Can anyone help insert a picture from the resources to an Excel sheet in VB2010?

推荐答案

您可以在代码中自动使用剪贴板将图像添加到excel。

You can add an image to excel using the clipboard automatically in your code.

以下代码在vb net 2008中工作。

The below code works in vb net 2008.

Dim MyScoreCard As Excel.Worksheet = Nothing
Dim MyRange As Excel.Range = Nothing
Dim AwayBit As Bitmap =CType(My.Resources.ResourceManager.GetObject("picturename"),Bitmap)

Clipboard.Clear()
Clipboard.SetDataObject(AwayBit, True)
MyRange = CType(MyScoreCard.Range("Q41:Q41"), Excel.Range)
MyScoreCard.Paste(MyRange, AwayBit)

'Count will return the index of the last shape pasted to the worksheet
Dim idx As Integer
idx = MyScoreCard.Shapes.Count

'The code below will allow you to scale the image in excel from vb net
MyScoreCard.Shapes.Item(idx).LockAspectRatio =   Microsoft.Office.Core.MsoTriState.msoFalse

MyScoreCard.Shapes.Item(idx).ScaleWidth(0.75, Microsoft.Office.Core.MsoTriState.msoFalse, Office.MsoScaleFrom.msoScaleFromTopLeft)

MyScoreCard.Shapes.Item(idx).ScaleHeight(0.75, Microsoft.Office.Core.MsoTriState.msoFalse, Office.MsoScaleFrom.msoScaleFromTopLeft)

这篇关于如何使用VB2010将图片资源插入Excel表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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