使用 Open XML 插入工作表 - “...不可读的内容...”

发布于 2024-10-05 01:35:17 字数 379 浏览 6 评论 0原文

当我按照本教程操作时:

http://msdn.microsoft.com/en-us /library/cc881781.aspx

打开一个 Excel 文档并插入一个空工作表,最终结果是一条消息,告诉您“Excel 在...中发现不可读的内容...是否要恢复该工作簿的内容...” 。如果我恢复所有插入的工作表都是空白的(即使我以编程方式添加内容),

则在将 xlsx 重命名为 .zip 并检查它后,显示工作表已创建并添加了内容。

有人有类似的问题吗?这可能是因为没有在新创建的部件之间创建关系......

When I follow this tutorial:

http://msdn.microsoft.com/en-us/library/cc881781.aspx

to open an Excel document and insert an empty worksheet the final result is a message telling "Excel found unreadable content in ... Do you want to recover the contents of this workbook...". If I recover all the inserted sheets are blank (even if I add content programatically)

After renaming the xlsx to .zip and examining it shows that the worksheets have been created, and content added.

Anyone with any similar problems? It may be something with not creating relationships between newly created parts...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2024-10-12 01:35:17

该错误消息意味着构成 Excel 文档的 XML 不符合 XML 架构并且无效。您可以使用 打开XML SDK 2.0 Productivity Tool 查看问题所在。

我还从您的链接底部复制了代码,并使其像克里斯在评论中所说的那样工作。你的代码看起来像下面这样吗?

// Open the document for editing.
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true)) 
{
    // Add a blank WorksheetPart.
    WorksheetPart newWorksheetPart = 
       spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
    newWorksheetPart.Worksheet = new Worksheet(new SheetData());

    Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
    string relationshipId = 
       spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);
    // Get a unique ID for the new worksheet.
    uint sheetId = 1;
    if (sheets.Elements<Sheet>().Count() > 0)
    {
        sheetId = 
        sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
    }

    // Give the new worksheet a name.
    string sheetName = "Sheet" + sheetId;

    // Append the new worksheet and associate it with the workbook.
    Sheet sheet = new Sheet() 
    { Id = relationshipId, SheetId = sheetId, Name = sheetName };
    sheets.Append(sheet);

    string docName = @"C:\Users\Public\Documents\Sheet7.xlsx";
    InsertWorksheet(docName);
}

// Given a document name, inserts a new worksheet.
public static void InsertWorksheet(string docName)
{
    // Open the document for editing.
    using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
    {
        // Add a blank WorksheetPart.
        WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
        newWorksheetPart.Worksheet = new Worksheet(new SheetData());

        Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
        string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);

        // Get a unique ID for the new worksheet.
        uint sheetId = 1;
        if (sheets.Elements<Sheet>().Count() > 0)
        {
            sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
        }

        // Give the new worksheet a name.
        string sheetName = "Sheet" + sheetId;

        // Append the new worksheet and associate it with the workbook.
        Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
        sheets.Append(sheet);
    }
}

That error message means that the XML that makes up your excel document is not conforming to the XML Schema and is invalid. You can use the Open XML SDK 2.0 Productivity Tool to see where the issue is located.

I also copied the code from the bottom of your link and got it to work like Chris said in his comment. Does your code look like this below?

// Open the document for editing.
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true)) 
{
    // Add a blank WorksheetPart.
    WorksheetPart newWorksheetPart = 
       spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
    newWorksheetPart.Worksheet = new Worksheet(new SheetData());

    Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
    string relationshipId = 
       spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);
    // Get a unique ID for the new worksheet.
    uint sheetId = 1;
    if (sheets.Elements<Sheet>().Count() > 0)
    {
        sheetId = 
        sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
    }

    // Give the new worksheet a name.
    string sheetName = "Sheet" + sheetId;

    // Append the new worksheet and associate it with the workbook.
    Sheet sheet = new Sheet() 
    { Id = relationshipId, SheetId = sheetId, Name = sheetName };
    sheets.Append(sheet);

    string docName = @"C:\Users\Public\Documents\Sheet7.xlsx";
    InsertWorksheet(docName);
}

// Given a document name, inserts a new worksheet.
public static void InsertWorksheet(string docName)
{
    // Open the document for editing.
    using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
    {
        // Add a blank WorksheetPart.
        WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
        newWorksheetPart.Worksheet = new Worksheet(new SheetData());

        Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
        string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);

        // Get a unique ID for the new worksheet.
        uint sheetId = 1;
        if (sheets.Elements<Sheet>().Count() > 0)
        {
            sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
        }

        // Give the new worksheet a name.
        string sheetName = "Sheet" + sheetId;

        // Append the new worksheet and associate it with the workbook.
        Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
        sheets.Append(sheet);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文