[研究][ASP.NET]匯入.xlsx出錯:無效的 URI: 無法剖析主機名稱。
2022-11-24, 2024-01-22 更新
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#
********************************************************************************
Elmah 通知信內容
********************************************************************************
nerated: Wed, 23 Nov 2022 08:19:14 GMT
System.Web.HttpUnhandledException (0x80004005): 已發生類型 'System.Web.HttpUnhandledException' 的例外狀況。 ---> DocumentFormat.OpenXml.Packaging.OpenXmlPackageException: A malformed URI was found in the document.
Please provide a OpenSettings.RelationshipErrorRewriter to handle these errors while opening a package.
---> System.UriFormatException: 無效的 URI: 無法剖析主機名稱。
於 System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
於 MS.Internal.IO.Packaging.InternalRelationshipCollection.ProcessRelationshipAttributes(XmlCompatibilityReader reader)
於 MS.Internal.IO.Packaging.InternalRelationshipCollection.ParseRelationshipPart(PackagePart part)
於 MS.Internal.IO.Packaging.InternalRelationshipCollection..ctor(Package package, PackagePart part)
於 System.IO.Packaging.PackagePart.EnsureRelationships()
於 System.IO.Packaging.PackagePart.GetRelationshipsHelper(String filterString)
於 DocumentFormat.OpenXml.Packaging.OpenXmlPart.Load(OpenXmlPackage openXmlPackage, OpenXmlPart parent, Uri uriTarget, String id, Dictionary`2 loadedParts)
於 DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.LoadReferencedPartsAndRelationships(OpenXmlPackage openXmlPackage, OpenXmlPart sourcePart, RelationshipCollection relationshipCollection, Dictionary`2 loadedParts)
於 DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.LoadReferencedPartsAndRelationships(OpenXmlPackage openXmlPackage, OpenXmlPart sourcePart, RelationshipCollection relationshipCollection, Dictionary`2 loadedParts)
於 DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Load(Package package)
--- 內部例外狀況堆疊追蹤的結尾 ---
於 DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Load(Package package)
於 DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable, OpenSettings openSettings)
於 ClosedXML.Excel.XLWorkbook.LoadSheets(String fileName)
於 (略)_ClosedXML_v2_Click(Object sender, EventArgs e) 於 (略).aspx.cs: 行 2216
於 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
於 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
於 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
於 System.Web.UI.Page.HandleError(Exception e)
於 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
於 System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
於 System.Web.UI.Page.ProcessRequest()
於 System.Web.UI.Page.ProcessRequest(HttpContext context)
於 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
於 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
於 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
********************************************************************************
解決
開發機上測試
A malformed uri was found in the document.
在文件中發現格式錯誤的 uri。
開啟出事的 .xlsx 檔案,發現某「儲存格」內容是藍色、底線、有@,也就是被 Excel 當成超連結了,可能敝人使用的 ClosedXML 0.96.0 無法開啟有這種「儲存格」的 .xlsx,修改欄位內容,結果藍色、底線一值沒有自動消失,檢視格式是「通用格式」,最後從別的文字「儲存格」複製貼過來蓋掉,這次檔案匯入成功。
結論:匯入元件無法開啟有這種「儲存格」內容和格式的 .xlsx,會直接 Exception。
(完)