Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

exception on loading : An item with the same key has already been added. #1121

Closed
thenik opened this issue Jul 19, 2023 · 9 comments
Closed
Labels
bug file_error file format generation/writing issue
Milestone

Comments

@thenik
Copy link

thenik commented Jul 19, 2023

Hello
Got some issue with the next file:

optax_price_xiaomi_obnov_1707 (2).xlsx

any way how to "try catch" such an error and to read this file till the end?

@thenik thenik changed the title excelption on loading : An item with the same key has already been added. exception on loading : An item with the same key has already been added. Jul 19, 2023
@tonyqus
Copy link
Member

tonyqus commented Jul 19, 2023

which version of NPOI are you using?

And please show me the code to reproduce the issue.

@thenik
Copy link
Author

thenik commented Jul 21, 2023

I used 2.5.6
image

but I tested with 2.6.1
it has the same Error

@tonyqus
Copy link
Member

tonyqus commented Jul 21, 2023

Can you share the code to reproduce the issue?

@karakasa
Copy link
Contributor

karakasa commented Jul 27, 2023

Can you share the code to reproduce the issue?

A simple loading file will raise the exception.

const string path = @"d:\whatever\optax_price_xiaomi_obnov_1707.2.xlsx";
var wb = new XSSFWorkbook(path);

As for the file, there're duplicated entries in xl/drawings/_rels/drawing1.xml.rels referencing the same file.

<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image4.jpeg"/>
...
<Relationship Id="rId91" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image4.jpeg"/>

I think it's because of image reuse (i.e. copy & paste inside Excel) as put in the cell of E10 & E133.

I implemented a quick fix in NPOI.OpenXml4Net.Core/OPC/PackageRelationshipCollection.cs, did a round-trip test and witnessed no side effects so far. Maybe someone, who is more familar with OOXML, can explain if anything different is gonna happen.

https://github.com/nissl-lab/npoi/blob/fd8103d1eeae6483f941073d10f09303bbfb57df/openxml4Net/OPC/PackageRelationshipCollection.cs#L249C8-L249C8

        if (targetMode == TargetMode.Internal)
        {
            var uriOriginalString = targetUri.OriginalString;

            if (!internalRelationshipsByTargetName.ContainsKey(uriOriginalString))
            {
                internalRelationshipsByTargetName.Add(uriOriginalString, rel);
            }
        }

p.s. I understand ContainsKey+Add is suboptimal to this[key] performance-wise but I don't know if it's better to keep the first rel or the last rel, while lower dotnet targets don't have a TryAdd extension.

@tonyqus
Copy link
Member

tonyqus commented Jul 27, 2023

I notice this file is generated by LibreOffice. I resave the file with Microsoft Office and I don't see a duplicate key anymore

Here is the drawing1.xml.rels from the MIcrosoft Office resaved file.
drawing1.xml.rels.txt

In this case, I don't think it is a bug. The file provided is not well-generated.

@thenik
Copy link
Author

thenik commented Jul 27, 2023

This file I get for a supplier via loading from a website url.
I cannot open it and resave if in Microsoft Office as you suggested.
From my point of view is a bug because I can open this file in Microsoft Office and there is no any exception
but when I open file via c# code using NPOI it calls exception, so it is a bug.

@karakasa
Copy link
Contributor

karakasa commented Jul 27, 2023

I suppose that NPOI should have a policy on whether to fix issues relating to malformed but Excel-readable files. The answer is probably yes.

@tonyqus tonyqus added bug file_error file format generation/writing issue and removed need-investigation labels Jul 27, 2023
@tonyqus
Copy link
Member

tonyqus commented Jul 27, 2023

Fair enough. I'll fix this issue and see if we can suppress the exception without breaking anything

@tonyqus tonyqus added this to the NPOI 2.7.0 milestone Jul 27, 2023
@thenik
Copy link
Author

thenik commented Jul 27, 2023

wow, many thanks! you are the best!

@tonyqus tonyqus modified the milestones: NPOI 2.7.0, NPOI 2.6.2 Aug 23, 2023
tonyqus added a commit that referenced this issue Aug 23, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug file_error file format generation/writing issue
Projects
None yet
Development

No branches or pull requests

3 participants