Inserimento foglio di lavoro con Open XML - “... contenuto illeggibile ...”
-
29-09-2019 - |
Domanda
Quando ho seguire questo tutorial:
http://msdn.microsoft.com/en-us/library/ cc881781.aspx
per aprire un documento Excel e inserire un foglio di lavoro vuoto il risultato finale è un messaggio che dice "Excel trovato contenuto illeggibile in ... Do si desidera recuperare il contenuto di questo lavoro ...". Se mi riprendo tutti i fogli inseriti sono vuoti (anche se posso aggiungere contenuti programatically)
Dopo aver rinominato il xlsx in .zip ed esaminando dimostra che sono stati creati i fogli di lavoro, e contenuto aggiunto.
Chiunque con problemi simili? Potrebbe essere qualcosa di non creare relazioni tra le parti di nuova creazione ...
Soluzione
Ciò significa che il messaggio di errore XML che compone il documento Excel non è conforme allo schema XML e non è valido. È possibile utilizzare il Open XML SDK 2.0 Produttività strumento per vedere dove il problema si trova.
Inoltre ho copiato il codice dal fondo del tuo link e ottenuto di lavorare come Chris ha detto nel suo commento. Il vostro sguardo codice come questo qui sotto?
// 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);
}
}