You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

含KnownFonts标签的多语言Excel无法用Open-XML SDK加载的问题咨询

Fixing Open-XML SDK's "The specified package is invalid. The main part is missing." Error for Korean Excel Files

Hey Francis, let's break down how to tackle this issue without manually editing the Excel file, and also address whether this counts as an SDK bug.

Option 1: Auto-Remove the Troublesome KnownFonts Tag via SDK

You don't need to manually unzip/rezip the file—you can use the Open-XML SDK's underlying Package API to modify the styles.xml part on the fly before loading the SpreadsheetDocument. Here's a working code snippet:

using (var package = Package.Open("your-problem-file.xlsx", FileMode.Open, FileAccess.ReadWrite))
{
    // Locate the styles.xml part in the package
    var stylesUri = new Uri("/xl/styles.xml", UriKind.Relative);
    var stylesPart = package.GetPart(stylesUri);
    
    if (stylesPart != null)
    {
        using (var stream = stylesPart.GetStream())
        {
            var stylesDoc = XDocument.Load(stream);
            var ns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
            
            // Find and remove the knownFonts element with val="1"
            var knownFontsElement = stylesDoc.Descendants(ns + "knownFonts")
                .FirstOrDefault(e => e.Attribute("val")?.Value == "1");
            
            if (knownFontsElement != null)
            {
                knownFontsElement.Remove();
                
                // Save the modified styles back to the package
                stream.Position = 0;
                stylesDoc.Save(stream);
                stream.SetLength(stream.Position); // Truncate leftover data to avoid corruption
            }
        }
    }
    
    // Now you can load and process the document normally without errors
    using (var spreadsheetDoc = SpreadsheetDocument.Open(package, false))
    {
        // Your existing import/processing logic goes here
    }
}

This approach intercepts the package before the SDK's higher-level parsing kicks in, removes the conflicting element, and lets you proceed with your standard workflow entirely via code.

Option 2: Report This as an SDK Compatibility Bug

From your detailed investigation, this absolutely looks like a bug in the Open-XML SDK. The KnownFonts tag with val="1" added by Korean-language Excel, when paired with Korean font references, is triggering a misleading "main part missing" error—this is a false alarm, as the real issue is the SDK failing to parse the styles correctly.

To report this and help get it fixed:

  • Head to the Open-XML SDK's GitHub repository and create a new Issue
  • Include these details to speed up troubleshooting:
    • A minimal, reproducible Excel file (stripped down to just the problematic styles and font references)
    • Exact SDK versions you tested (2.8.1 and 2.9.0)
    • Full error message and stack trace
    • Your testing results (e.g., removing the tag fixes the issue, local Excel opens the file without problems)

The maintainers will likely prioritize this since it's an interoperability issue with regional Excel versions that you can't control as a developer.


内容的提问来源于stack exchange,提问作者Francis P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:11:05