我有一个源 Excel 工作簿(XLSM 格式),其中包含包含图像的工作表。这些工作表使用 VB.NET 后期绑定以编程方式复制到另一个(目标)工作簿 (XLSM)。完成所有副本后,我保存工作簿并启动它。在打开的 Excel 工作簿中,我在放置图像的所有位置都看到错误消息“在文件中找不到关系 ID 为 rId1 的图像部分”。所有操作都在客户端机器上完成,没有可用的服务器端代码。
有趣的是,这个问题在 Excel 2013 中没有发生,它正确显示图像问题仅在 2010 和 2007 年观察到。这是 Excel 2010 和 2007 中的一个已知错误,如果是,任何人都可以向我提供票证的官方链接,以便我可以跟踪问题并在可用后获取热修复。
Dim SourceExcelWorkbook As Excel.Workbook = Nothing
Dim TargetExcelWorkbook As Excel.Workbook = Nothing
Dim TargetExcelSheets As Excel.Sheets = Nothing
Dim SourceExcelSheets As Excel.Sheets = Nothing
Dim CopyWorkSheet As Excel.Worksheet = Nothing
Dim XLApp As New Excel.Application
XLApp.Visible = False
XLApp.DisplayAlerts = False
XLApp.ScreenUpdating = False
Dim pobjExcelWorkbooks As Excel.Workbooks = XLApp.Workbooks
SourceExcelWorkbook = pobjExcelWorkbooks.Open("source file path")
TargetExcelWorkbook = pobjExcelWorkbooks.Open("target file path")
TargetExcelSheets = TargetExcelWorkbook.Worksheets
SourceExcelSheets = SourceExcelWorkbook.Worksheets
Dim OriginalSheetCount As Integer = TargetExcelSheets.Count
Dim SheetCount As Integer = OriginalSheetCount
Dim SheetsToBeCopiedCount As Integer = SourceExcelSheets.Count
While SheetsToBeCopiedCount > 0
Dim lobjAfterSheet As Object = TargetExcelSheets.Item(SheetCount)
CopyWorkSheet = SourceExcelSheets.Item(1)
CopyWorkSheet.Move(After:=lobjAfterSheet)
SheetCount = SheetCount + 1
TargetExcelWorkbook.Save()
SheetsToBeCopiedCount = SheetsToBeCopiedCount - 1
End While
TargetExcelWorkbook.Save()