如何在Excel单元格VB.NET中调整图像大小

时间:2015-01-02 04:36:19

标签: vb.net

我是vb.net的新手,或者更喜欢所有编程。我已经尝试搜索与我的问题相关的每个链接的答案,但我仍然找不到任何答案,或者我只是不理解这些代码。

我的问题是如何使用VB.NET在excel中调整图像大小。我在插入图像到excel的代码已经有效,但是我想要更大的图像是固定大小。

这是我插入图片的代码,请帮我如何将图片大小调整为2英寸或者其他任何东西。请...

Private Sub AddPicture_Click(sender As System.Object, e As System.EventArgs) 
Handles AddPicture.Click

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
            'Start Excel
            APP = New Excel.Application
            'Open an existing excel application
            Dim workbook = APP.Workbooks.OpenXML("C:\Users\user\My Documents\
            PROJ 141\System\Book1.xlsx")
            Dim worksheet = workbook.Worksheets("sheet1")
            Dim oRng As Excel.Range
            Dim misValue As Object = System.Reflection.Missing.Value

            With worksheet
                oRng = DirectCast(.Cells(32, 1), Excel.Range)
                oRng.Cells.Select()

            End With

           'Add a picture to excel
            APP.ActiveSheet.Pictures.Insert(OpenFileDialog1.FileName).Select()
            workbook.Save()
            APP.Workbooks.Close()
            workbook = Nothing
            worksheet = Nothing
            APP.Quit()
            APP = Nothing 

        End If

    End Sub

1 个答案:

答案 0 :(得分:0)

我很高兴我解决了自己的问题...

我只是更改了这行代码:

APP.ActiveSheet.Pictures.Insert(OpenFileDialog1.FileName).Select()

成:

With APP.ActiveSheet.Pictures.Insert(OpenFileDialog1.FileName)
            .Width = "50"
            .Height = "50"
            .Placement = 1

        End With