gpt4 book ai didi

c# - OpenXML Excel : throw error in any word after mail address

转载 作者:太空狗 更新时间:2023-10-29 22:36:51 25 4
gpt4 key购买 nike

我使用 OpenXml 读取 Excel 文件。一切正常,但如果电子表格包含一个单元格,其中有一个地址邮件,然后是一个空格和另一个单词,例如:

abc@abc.com abc

它在打开电子表格时立即抛出异常:

var _doc = SpreadsheetDocument.Open(_filePath, false); 

异常(exception):

DocumentFormat.OpenXml.Packaging.OpenXmlPackageException
Additional information:
Invalid Hyperlink: Malformed URI is embedded as a hyperlink in the document.

最佳答案

OpenXml 论坛上有一个与此问题相关的未解决问题:Malformed Hyperlink causes exception

在博文中,他们谈到了在 Word 文档中遇到格式错误的“mailto:”超链接的问题。

他们在这里提出了一个变通办法:Workaround for malformed hyperlink exception

解决方法本质上是一个小型控制台应用程序,它可以找到无效的 URL 并将其替换为硬编码值;这是他们进行替换的样本中的代码片段;您可以扩充此代码以尝试更正传递的 brokenUri:

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

我遇到的问题实际上是 Excel 文档(就像您一样),它与格式错误的 http URL 有关;我惊喜地发现他们的代码与我的 Excel 文件一起工作得很好。

这是整个变通源代码,以防万一这些链接之一在将来消失:

 void Main(string[] args)
{
var fileName = @"C:\temp\corrupt.xlsx";
var newFileName = @"c:\temp\Fixed.xlsx";
var newFileInfo = new FileInfo(newFileName);

if (newFileInfo.Exists)
newFileInfo.Delete();

File.Copy(fileName, newFileName);

WordprocessingDocument wDoc;
try
{
using (wDoc = WordprocessingDocument.Open(newFileName, true))
{
ProcessDocument(wDoc);
}
}
catch (OpenXmlPackageException e)
{
e.Dump();
if (e.ToString().Contains("The specified package is not valid."))
{
using (FileStream fs = new FileStream(newFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri));
}
}
}
}

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

private static void ProcessDocument(WordprocessingDocument wDoc)
{
var elementCount = wDoc.MainDocumentPart.Document.Descendants().Count();
Console.WriteLine(elementCount);
}
}

public static class UriFixer
{
public static void FixInvalidUri(Stream fs, Func<string, Uri> invalidUriHandler)
{
XNamespace relNs = "http://schemas.openxmlformats.org/package/2006/relationships";
using (ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Update))
{
foreach (var entry in za.Entries.ToList())
{
if (!entry.Name.EndsWith(".rels"))
continue;
bool replaceEntry = false;
XDocument entryXDoc = null;
using (var entryStream = entry.Open())
{
try
{
entryXDoc = XDocument.Load(entryStream);
if (entryXDoc.Root != null && entryXDoc.Root.Name.Namespace == relNs)
{
var urisToCheck = entryXDoc
.Descendants(relNs + "Relationship")
.Where(r => r.Attribute("TargetMode") != null && (string)r.Attribute("TargetMode") == "External");
foreach (var rel in urisToCheck)
{
var target = (string)rel.Attribute("Target");
if (target != null)
{
try
{
Uri uri = new Uri(target);
}
catch (UriFormatException)
{
Uri newUri = invalidUriHandler(target);
rel.Attribute("Target").Value = newUri.ToString();
replaceEntry = true;
}
}
}
}
}
catch (XmlException)
{
continue;
}
}
if (replaceEntry)
{
var fullName = entry.FullName;
entry.Delete();
var newEntry = za.CreateEntry(fullName);
using (StreamWriter writer = new StreamWriter(newEntry.Open()))
using (XmlWriter xmlWriter = XmlWriter.Create(writer))
{
entryXDoc.WriteTo(xmlWriter);
}
}
}
}
}

关于c# - OpenXML Excel : throw error in any word after mail address,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29970814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com