Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Invalid Hyperlink: Malformed URI is embedded as a hyperlink in the document. #249

Open
BenKmann opened this issue Mar 29, 2017 · 11 comments · May be fixed by #1619
Open

Invalid Hyperlink: Malformed URI is embedded as a hyperlink in the document. #249

BenKmann opened this issue Mar 29, 2017 · 11 comments · May be fixed by #1619

Comments

@BenKmann
Copy link

I get the Exception Invalid Hyperlink: Malformed URI is embedded as a hyperlink in the document.
at

   bei DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Load()
   bei DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(String path, Boolean readWriteMode)
   bei DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable, OpenSettings openSettings)
   bei ClosedXML.Excel.XLWorkbook.LoadSheets(String fileName) in C:\Git\ClosedXML\ClosedXML\Excel\XLWorkbook_Load.cs:Zeile 44.
   bei ClosedXML.Excel.XLWorkbook..ctor(String file) in C:\Git\ClosedXML\ClosedXML\Excel\XLWorkbook.cs:Zeile 682.

I googled a little bit and apparently it is a common problem. You can't open a xlsx with a malformed Uri :(. Here is a Solution to the Problem http://ericwhite.com/blog/handling-invalid-hyperlinks-openxmlpackageexception-in-the-open-xml-sdk/ .
I looked in the sheet1.xml.rels and found the malformed Hyperlink, it has as Target:
" mailto:test.test@test-test.com;%2009.06.2015%20gibt%20ww%20Stahl ".

Maybe you can Implement the Solution from the Link or they already implemented a fitting overload as announced in the blog-post.

@igitur
Copy link
Member

igitur commented Mar 29, 2017

Try taking it up with the OpenXML team first. I think this should be handled closer to the source. Tag the issue here, please.

@igitur
Copy link
Member

igitur commented Mar 30, 2017

It's already been logged with them. Please see dotnet/Open-XML-SDK#38

@BenKmann
Copy link
Author

Wow thanks for linking and all :). Great work!

@santerinogelainen
Copy link

OpenXML v2.12.0+ introduced a workaround using OpenSettings.RelationshipErrorRewriter with pull request Add a relationship rewriter to help sanitize malformed URIs. Are there any plans to add/implement this workaround to ClosedXML?

@igitur
Copy link
Member

igitur commented Feb 6, 2021

I have limited capacity, but we welcome pull requests.

@igitur igitur linked a pull request Feb 21, 2021 that will close this issue
@hieuxlu
Copy link

hieuxlu commented Apr 2, 2021

I had an issue like this and it turns out upgrading OpenXML to 2.12.x and write the fix ourselves is very easy. It's just a couple of LOCs to rewrite the input stream.

            var settings = new OpenSettings
            {
                RelationshipErrorHandlerFactory = package =>
                {
                    return new UriRelationShipErrorHandler();
                },
            };


            var outputStream = new MemoryStream();
            stream.CopyTo(outputStream);
            stream.Position = 0;

            using (var doc = SpreadsheetDocument.Open(outputStream, true, settings))
            {
                doc.Save();
            }
            outputStream.Position = 0;
            // At this point, outputStream is ready for use by ClosedXML
            using (var workbook = new XLWorkbook(outStream))
            

@igitur Could we just accept OpenSettings as a constructor argument of ClosedXML's XLWorkbook? Does that makes solving future similar problems easier?

@igitur
Copy link
Member

igitur commented Apr 2, 2021

@hieuxlu See #1619

@TrueBytes
Copy link

I had an issue like this and it turns out upgrading OpenXML to 2.12.x and write the fix ourselves is very easy. It's just a couple of LOCs to rewrite the input stream.

            var settings = new OpenSettings
            {
                RelationshipErrorHandlerFactory = package =>
                {
                    return new UriRelationShipErrorHandler();
                },
            };


            var outputStream = new MemoryStream();
            stream.CopyTo(outputStream);
            stream.Position = 0;

            using (var doc = SpreadsheetDocument.Open(outputStream, true, settings))
            {
                doc.Save();
            }
            outputStream.Position = 0;
            // At this point, outputStream is ready for use by ClosedXML
            using (var workbook = new XLWorkbook(outStream))
            

@igitur Could we just accept OpenSettings as a constructor argument of ClosedXML's XLWorkbook? Does that makes solving future similar problems easier?

@hieuxlu ,
workbook.Save() is not saving any changes performed in the worksheet in the ClosedXML.
When I used "outputStream is ready for use by ClosedXML" code-shared by you.
WorkBook.SaveAs(filename) is working however I want to achieve the modifications in the same workbook without creating the new workbook.

@RajdeepRay0411
Copy link

RajdeepRay0411 commented Jul 27, 2021

Hello @igitur, @hieuxlu, and @santerinogelainen, I am also facing the same exception though I am using version 2.13.0 of DocumentFormat.OpenXml Package. Can you please tell me the ETA when this issue will be resolved in the package?

@RajdeepRay0411
Copy link

Hello @TrueBytes, Can you please help me with what you are passing and executing in the "UriRelationShipErrorHandler()" method.

@mrsric
Copy link

mrsric commented Oct 27, 2021

Hello @RajdeepRay0411 , following @hieuxlu tip you need to create a class that overrides Rewrite:

using DocumentFormat.OpenXml.Packaging;
using System;

public class UriRelationshipErrorHandler : RelationshipErrorHandler
{
    public override string Rewrite(Uri partUri, string id, string uri)
    {
        return "http://link-invalido";
    }
}

Example of use:

        var openSettings = new OpenSettings()
        {
            RelationshipErrorHandlerFactory = package =>
            {
                return new UriRelationshipErrorHandler();
            }
        };

        using (var outputStream = File.Open(excelPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            using (var xls = SpreadsheetDocument.Open(outputStream, true, openSettings))
            {
                xls.Save();
            }

            var workbook = new XLWorkbook(outputStream);
           .......

        }

Refereces:

https://stackoverflow.com/questions/29970814/openxml-excel-throw-error-in-any-word-after-mail-address

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants