【问题标题】:Insert a picture into existing excel file将图片插入现有的 excel 文件
【发布时间】:2015-05-28 11:06:26
【问题描述】:

我是 vb.net 的新手,我想打开一个现有的 excel 文件,并将图像插入特定的单元格,例如“C16”。

已经三天了,我正在搜索这个主题,但我发现只是创建一个新的excel文件并在其中插入图片。

有人可以帮忙吗?

【问题讨论】:

  • 在现有文件的单元格中插入图片的方式不应与新文件的方式不同。一旦你打开它,只需像你一样添加你的图像。看看这个链接:visual-basic-tutorials.com/WriteToExcel2007.php。一旦你这样做了,如果你仍然不能,向我们展示一些代码,我们会指出你缺少什么。

标签: asp.net vb.net excel file


【解决方案1】:

可以使用的代码是这样的:

ActiveSheet.Pictures.Insert(pathForPicture & "\" & pictureName & ".jpg").Select 'Path to where pictures are stored

更多阅读本站内容:VBA to insert embeded picture excel

How to insert a picture into Excel at a specified cell position with VBA

【讨论】:

    【解决方案2】:

    我已经找到了解决方案: 请在下面找到它

                        workbook = APP.Workbooks.Open(filepath)
                        worksheet = workbook.Worksheets("name of the excel sheet")
    
                        worksheet.Visible = True
                        Dim pic1 As String = "picture path"
                        worksheet.Range("I51:I51").Select() 
    

    I51 是我想要图片的单元格

                        worksheet.PageSetup.Zoom = False
                        worksheet.PageSetup.FitToPagesWide = 1
                        worksheet.PageSetup.FitToPagesTall = 1
                        Dim opicture1 As Object
                        opicture1 = worksheet.Pictures.Insert(pic1)
    

    【讨论】:

    • 在我的测试中。它将插入图像的路径,而不是图像本身。
    • 所以我使用 WorkSheet.Shapes.AddPicture(picPath, False, True, 0, 0, Width, Height) 。
    【解决方案3】:

    我想提供我的代码。

    Dim picPath As String = "C:\..."
    Dim _Left   = xlWorkSheet.Range(WorkSheet.Cells(i, j).Address).Left
    Dim _Top    = xlWorkSheet.Range(WorkSheet.Cells(i, j).Address).Top
    Dim _Width  = xlWorkSheet.Range(WorkSheet.Cells(i, j).Address).Width
    Dim _Height = xlWorkSheet.Range(WorkSheet.Cells(i, j).Address).Height
    WorkSheet.Shapes.AddPicture(picPath, False, True, _Left, _Top, _Width, _Height)
    

    picPath是你要插入的图片的路径。

    (i,j) 是要插入图片的单元格的索引。

    【讨论】:

      猜你喜欢
      • 2012-05-24
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 2014-09-14
      • 1970-01-01
      相关资源
      最近更新 更多