公司不让用EXCEL.用的这个鬼libreoffice.很简单的功能到这边就变复杂了,由于需求批量往表格里插入图片,故做了一翻研究,通过宏代码目前亲测可行,特提供代码跟大家分享,送给有需要的朋友,喜欢的帮顶!谢谢!
以下为插入至单元格图片的代码,图片大小是根据单元格大小定的!有需要插入批注里图片的,可以M我,我可以提供
Sub insert_pic()
Dim s$,x1%, x2%,i%, j%, y1%, y2%
Dim Sheet As Object
s = ""
Dim t$
t = ""
Dim Size As New com.sun.star.awt.Size
Dim FilePicker As Object
FilePicker=createUnoService("com.sun.star.ui.dialogs.FolderPicker")
FilePicker.execute
path_str = FilePicker.getDirectory()
Set oCell = ThisComponent.getCurrentSelection
Set aCellAddress = oCell.getRangeAddress
y1 = aCellAddress.StartColumn
x1 = aCellAddress.StartRow
y2 = aCellAddress.EndColumn
x2 = aCellAddress.EndRow
SheetIndex = aCellAddress.Sheet
Sheet = ThisComponent.Sheets.getByIndex(SheetIndex)
opage = Sheet.drawpage
For j = y1 to y2
For i = x1 to x2
Range1 = sheet.getCellByPosition(j, i)
t = Range1.String
t= converttourl(path_str & "/" & t & ".jpg")
size.Height = sheet.Rows(i).Height
size.width = sheet.Columns(j+(y2-y1)).Width
oshape = thiscomponent.createInstance("com.sun.star.drawing.GraphicObjectShape")
with oshape
.name = "Shape1"
.size = size
.position = sheet.getCellByPosition(j+(y2-y1), i).Position
.GraphicURL = t
end with
opage.add(oshape)
Next
Next End
End Sub
以下为插入至单元格图片的代码,图片大小是根据单元格大小定的!有需要插入批注里图片的,可以M我,我可以提供
Sub insert_pic()
Dim s$,x1%, x2%,i%, j%, y1%, y2%
Dim Sheet As Object
s = ""
Dim t$
t = ""
Dim Size As New com.sun.star.awt.Size
Dim FilePicker As Object
FilePicker=createUnoService("com.sun.star.ui.dialogs.FolderPicker")
FilePicker.execute
path_str = FilePicker.getDirectory()
Set oCell = ThisComponent.getCurrentSelection
Set aCellAddress = oCell.getRangeAddress
y1 = aCellAddress.StartColumn
x1 = aCellAddress.StartRow
y2 = aCellAddress.EndColumn
x2 = aCellAddress.EndRow
SheetIndex = aCellAddress.Sheet
Sheet = ThisComponent.Sheets.getByIndex(SheetIndex)
opage = Sheet.drawpage
For j = y1 to y2
For i = x1 to x2
Range1 = sheet.getCellByPosition(j, i)
t = Range1.String
t= converttourl(path_str & "/" & t & ".jpg")
size.Height = sheet.Rows(i).Height
size.width = sheet.Columns(j+(y2-y1)).Width
oshape = thiscomponent.createInstance("com.sun.star.drawing.GraphicObjectShape")
with oshape
.name = "Shape1"
.size = size
.position = sheet.getCellByPosition(j+(y2-y1), i).Position
.GraphicURL = t
end with
opage.add(oshape)
Next
Next End
End Sub