使用vb.net代码在excel的单元格中插入图像 [英] insert image in cell of excel by using vb.net code

查看:796
本文介绍了使用vb.net代码在excel的单元格中插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用vb.net代码在excel的单元格中插入图像

insert image in cell of excel by using vb.net code

Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet As Object



        oExcel = CreateObject("Excel.Application")
        oExcel.Visible = True
        oBook = oExcel.Workbooks.Add
        oSheet = oBook.Worksheets(1)
        oSheet.Shapes.AddPicture("C:\Sample Pictures\p.jpg", False, True, 0, 0, 45, 50) 





这是我们在excel中插入图像的半码槽,它不适合excel的一个单元................



请帮我这个..............



this is the half code trough which we insert image in excel, bt it not fit to the one cell of excel................

pls help me with this..............

推荐答案

范围 [ ^ ] Top和Left属性用于定义左上角。



宽度和高度应为点而不是像素。使用3/4倍增器。



您也可以缩放图片。



这是一个简短的VBA中的示例:

The Range[^] Top and Left properties are used to define the top left corner.

Width and Height should be in points instead of pixels. Use the 3/4 multiplier.

You may scale your pictures also.

Here is a short example in VBA:
Dim ws As Worksheet
Dim r As Range
Dim fileName As String
Dim pictureWidth As Integer
Dim pictureHeight As Integer
Dim shape As shape

Set ws = Worksheets(1)

Set r = ws.Cells(5, 2)

fileName = "d:\picture.png"

pictureWidth = 195
pictureHeight = 102

Set shape = ws.Shapes.AddPicture(fileName, msoTrue, msoTrue, r.Left, r.Top, pictureWidth * 3 / 4, pictureHeight * 3 / 4)

shape.LockAspectRatio = msoTrue
shape.ScaleHeight 2, msoTrue, msoScaleFromTopLeft


谢谢你的回复,bt它显示以下错误,我也使用'Imports Microsoft.Office.Interop.Excel'命名空间,仍然是它的错误



错误1:'工作表'是不明确的在命名空间Microsoft.Offic中e.Interop.Excel



错误2:在此上下文中无法访问System.Data.Range,因为它是朋友。

错误3:未声明'msoTrue'。由于它的防护等级,它可能无法访问。
Thank u for response, bt it shows following errors, i hv also used 'Imports Microsoft.Office.Interop.Excel' namespace, still its getting ths errors

Error 1 :'worksheet' is ambiguous in the namespace Microsoft.Office.Interop.Excel

Error 2 : 'System.Data.Range' is not accessible in this context because it is 'Friend'.
Error 3 : 'msoTrue' is not declared. It may be inaccessible due to its protection level.


谢谢你们的帮助.....

i自己解决了这个问题
thank u guys for ur respose.....
i solved ths by myself


这篇关于使用vb.net代码在excel的单元格中插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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