Pregunta

Cuando sigo este tutorial:

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

para abrir un documento de Excel e insertar una hoja de cálculo vacía, el resultado final es un decir del mensaje "Excel encontró contenido ilegible en ... ¿Quieres recuperar el contenido de este libro ...". Si me recupero todas las hojas insertadas están en blanco (incluso si añado contenido programáticamente)

Después de cambiar el nombre del xlsx a .zip y el examen muestra que las hojas de trabajo se han creado, y el contenido añadido.

Cualquier persona con problemas similares? Puede ser algo que no con la creación de relaciones entre las piezas de nueva creación ...

¿Fue útil?

Solución

Eso significa que el mensaje de error XML que compone el documento de Excel no está cumpliendo con el esquema XML y no es válido. Se puede utilizar el abierto XML SDK 2.0 Herramienta de productividad del para ver dónde se encuentra el problema.

También copiar el código desde el fondo de su enlace y tengo que trabajar como Chris dijo en su comentario. Se ve su código como el de abajo?

// 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);
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top