nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.65k stars 1.42k forks source link

fixed System.InvalidOperationException: 'The hyperlink for cell A1 references relation rId1, but that didn't exist!' #1370

Open htoyama opened 2 months ago

htoyama commented 2 months ago

NPOI Version

2.7.0

File Type

Upload the Excel File

Book1.xlsx

Reproduce Steps

workbook.CloneSheet("Sheet1");

Issue Description

System.InvalidOperationException: 'The hyperlink for cell A1 references relation rId1, but that didn't exist!' RelationshipのIdは、コピー元のワークシートと同じにしないと例外になる。

diff --git a/ooxml/XSSF/UserModel/XSSFWorkbook.cs b/ooxml/XSSF/UserModel/XSSFWorkbook.cs
index 2940dc72..da0981aa 100644
--- a/ooxml/XSSF/UserModel/XSSFWorkbook.cs
+++ b/ooxml/XSSF/UserModel/XSSFWorkbook.cs
@@ -626,7 +626,7 @@ public ISheet CloneSheet(int sheetNum, String newName)
                     if (pr.TargetMode == TargetMode.External)
                     {
                         clonedSheet.GetPackagePart().AddExternalRelationship
-                            (pr.TargetUri.OriginalString, pr.RelationshipType, null);
+                            (pr.TargetUri.OriginalString, pr.RelationshipType, pr.Id);
                     }
                 }
             }