无效的超链接:格式错误的URI作为超链接嵌入到文档中 [英] Invalid Hyperlink: Malformed URI is embedded as a hyperlink in the document

查看:1146
本文介绍了无效的超链接:格式错误的URI作为超链接嵌入到文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用OpenXml命名空间.我正在使用它来读取Excel文件中的XML.这在某些excel文件中可以正常使用,但在其他文件上却出现运行时错误,提示

I'm using the OpenXml namespace in my application. I'm using this to read the XML within an Excel file. This works fine with certain excel files but on others I get a run time error saying

无效的超链接:格式错误的URI作为超链接嵌入到文档中.

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

我在下一行获得运行时间

I get the run time on the following line

using (var spreadsheet = 
      DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(filePathCopy, true))

我不确定为什么它适用于某些Excel文件,而不适用于其他Excel文件.

I'm not sure why it works for some Excel files and doesn't work on others.

推荐答案

解决方案来自Eric White的
  • 从Nuget导入OpenXmlPowerTools并使用它.

    1. Import OpenXmlPowerTools from Nuget and use it.

    using OpenXmlPowerTools;
    

    功能OpenXmlPowerTools.UriFixer需要此功能,除非您要从链接复制功能.

    This is needed for the function OpenXmlPowerTools.UriFixer, unless you want to copy the function from the link.


    添加FixUri()函数以使用新定义的URI处理损坏的URI.

    Add the FixUri() Function to handle the broken URI's with a new defined URI.

    private static Uri FixUri(string brokenUri)
    {
        return new Uri("http://broken-link/");
    }
    


    添加代码以打开文档,如果发生异常,它将修复URI并重新打开已修复的文档.

    Add code to open the document, if the exception occurs it fixes the URI's and re-opens the fixed document.

    WordprocessingDocument wDoc;
    try
    {
        using (wDoc = WordprocessingDocument.Open(newFileName, true))
        {
            //Try do something
        }
    }
    catch (OpenXmlPackageException e)
    {
        if (e.ToString().Contains("Invalid Hyperlink"))
        {
            using (FileStream fs = new FileStream(newFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                //Fix problematic URI's
                OpenXmlPowerTools.UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri));
            }
            using (wDoc = WordprocessingDocument.Open(newFileName, true))
            {
                //Do something without error
            }
        }
    }
    

  • 这篇关于无效的超链接:格式错误的URI作为超链接嵌入到文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆