GetWorksheetPartByName(SpreadsheetDocument document, string sheetName) { IEnumerable <Sheet> sheets = document.WorkbookPart.Workbook.GetFirstChild <Sheets>(). Elements <Sheet>().Where(s => s.Name == sheetName); if (sheets.Count() == 0) { // The specified worksheet does not exist. throw new ApplicationException(String.Format("WorkSheetName {0} does not exist in the file : {1}", sheetName, document.ToString())); } string relationshipId = sheets.First().Id.Value; WorksheetPart worksheetPart = (WorksheetPart) document.WorkbookPart.GetPartById(relationshipId); return(worksheetPart); }
GetFirstWorksheetPart(SpreadsheetDocument document) { Sheets sheets = document.WorkbookPart.Workbook.GetFirstChild <Sheets>(); Sheet s = ((Sheet)sheets.First()); if (s == null) { // The specified worksheet does not exist. throw new ApplicationException(String.Format("No WorkSheet in the file : {0}", document.ToString())); } return((WorksheetPart)document.WorkbookPart.GetPartById(s.Id)); }