简体   繁体   English

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

[英]Inserting WorkSheet with Open XML - “…unreadable content…”

When I follow this tutorial: 当我按照本教程进行操作时:

http://msdn.microsoft.com/en-us/library/cc881781.aspx 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...". 打开Excel文档并插入一个空的工作表,最终结果是一条消息,提示“ Excel在...中发现了不可读的内容?您要恢复此工作簿的内容...”。 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. 将xlsx重命名为.zip并进行检查后,表明已创建工作表并添加了内容。

Anyone with any similar problems? 任何有类似问题的人吗? It may be something with not creating relationships between newly created parts... 可能是在新创建的零件之间未建立关系的情况下...

That error message means that the XML that makes up your excel document is not conforming to the XML Schema and is invalid. 该错误消息表示组成您的excel文档的XML不符合XML Schema,并且无效。 You can use the Open XML SDK 2.0 Productivity Tool to see where the issue is located. 您可以使用Open XML SDK 2.0生产率工具查看问题所在。

I also copied the code from the bottom of your link and got it to work like Chris said in his comment. 我还从链接底部复制了代码,并使它像Chris在他的评论中所说的那样起作用。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM