将图片和数据从一个excel文件复制到另一个excel文件
我有一个Excel文件,其中包含前五行的图片,然后开始数据。 我的工作是将文件复制到另一个excel文件中。 首先,我打开文件并复制使用的范围。 接下来我将这些内容粘贴到另一个excel文件中。
lobjCurrentWorkSheet.UsedRange.Copy() lobjTargetExcelWorkSheet.PasteSpecial(XlPasteType.xlPasteAll)
通过这样做,我只能复制数据,而不是图片? 如何将文件复制到另一个?
如果我使用SaveAs
选项,如果源文件是密码encryption,那么我的目标文件也将被encryption,我不想要的密码。
我永远不会推荐使用UsedRange
原因:
您的数据可能是从单元格A1到B4,图像是在说D1。 在这种情况下UsedRange
将始终是A1:B4
而不是A1:F6
如果您想要复制图片,请找出工作表的“实际范围”或指定范围
lobjCurrentWorkSheet.Range("A1:F6").Copy() lobjTargetExcelWorkSheet.Paste()
编辑 :我试图把我的手放在VB.Net,以便我可以给你一个确切的示例代码。 我终于做到了:)
看到这( 这将复制的实际范围,而不必指定它。 )
Dim shp As Excel.Shape Dim lCol As Integer = 0 '~~> Loop through all shapes and find the last col of the shape For Each shp In lobjCurrentWorkSheet.Shapes If shp.BottomRightCell.Column > lCol Then _ lCol = shp.BottomRightCell.Column Next With lobjCurrentWorkSheet '~~> Find actual last Row Dim LastRow As Integer = .Cells.Find(What:="*", _ After:=.Range("A1"), _ LookAt:=Excel.XlLookAt.xlPart, _ LookIn:=Excel.XlFindLookIn.xlFormulas, _ SearchOrder:=Excel.XlSearchOrder.xlByRows, _ SearchDirection:=Excel.XlSearchDirection.xlPrevious, _ MatchCase:=False).Row '~~> Find actual last column Dim LastColumn As Integer = .Cells.Find(What:="*", _ After:=.Range("A1"), _ LookAt:=Excel.XlLookAt.xlPart, _ LookIn:=Excel.XlFindLookIn.xlFormulas, _ SearchOrder:=Excel.XlSearchOrder.xlByColumns, _ SearchDirection:=Excel.XlSearchDirection.xlPrevious, _ MatchCase:=False).Column '~~> Check if we have the correct last columnm If LastColumn < lCol Then LastColumn = lCol .Range("A1:" & Split(.Cells(, LastColumn).Address, "$")(1) & LastRow).Copy() End With '~~> Copies to the current active cell in lobjTargetExcelWorkSheet lobjTargetExcelWorkSheet.Paste()