private void NullifyInternalDataStores() { slwb = null; slws = null; SimpleTheme = null; wbp = null; this.DocumentProperties = null; dictBuiltInNumberingFormat = null; dictBuiltInNumberingFormatHash = null; dictStyleNumberingFormat = null; dictStyleNumberingFormatHash = null; //countStyle = 0; listStyle = null; dictStyleHash = null; //countStyleFont = 0; listStyleFont = null; dictStyleFontHash = null; //countStyleFill = 0; listStyleFill = null; dictStyleFillHash = null; //countStyleBorder = 0; listStyleBorder = null; dictStyleBorderHash = null; //countStyleCellStyle = 0; listStyleCellStyle = null; dictStyleCellStyleHash = null; //countStyleCellStyleFormat = 0; listStyleCellStyleFormat = null; dictStyleCellStyleFormatHash = null; //countStyleDifferentialFormat = 0; listStyleDifferentialFormat = null; dictStyleDifferentialFormatHash = null; //countStyleTableStyle = 0; listStyleTableStyle = null; dictStyleTableStyleHash = null; //countSharedString = 0; listSharedString = null; dictSharedStringHash = null; StylesheetColors = null; TableStylesDefaultTableStyle = null; TableStylesDefaultPivotStyle = null; }
private void OpenExistingSpreadsheet(string SheetNameOnOpen) { xl = SpreadsheetDocument.Open(memstream, true); wbp = xl.WorkbookPart; IsNewSpreadsheet = false; slwb = new SLWorkbook(); this.DocumentProperties = new SLDocumentProperties(); this.LoadDocumentProperties(); LoadBuiltInNumberingFormats(); InitialiseStylesheetWhatNots(SLThemeTypeValues.Office); LoadSharedStringTable(); giWorksheetIdCounter = 0; using (OpenXmlReader oxr = OpenXmlReader.Create(wbp)) { Sheet s; SLSheet sheet; DefinedName dn; SLDefinedName sldn; while (oxr.Read()) { if (oxr.ElementType == typeof(Sheet)) { s = (Sheet)oxr.LoadCurrentElement(); sheet = new SLSheet(s.Name.Value, s.SheetId.Value, s.Id.Value, SLSheetType.Unknown); if (s.State != null) sheet.State = s.State.Value; slwb.Sheets.Add(sheet); if (sheet.SheetId > giWorksheetIdCounter) { giWorksheetIdCounter = (int)sheet.SheetId; } } else if (oxr.ElementType == typeof(DefinedName)) { dn = (DefinedName)oxr.LoadCurrentElement(); sldn = new SLDefinedName(dn.Name.Value); sldn.FromDefinedName(dn); slwb.DefinedNames.Add(sldn); } } } if (wbp.Workbook.WorkbookProperties != null) { slwb.WorkbookProperties.FromWorkbookProperties(wbp.Workbook.WorkbookProperties); } if (wbp.CalculationChainPart != null) { int iCurrentSheetId = 0; SLCalculationCell slcc = new SLCalculationCell(string.Empty); CalculationCell cc; using (OpenXmlReader oxr = OpenXmlReader.Create(wbp.CalculationChainPart)) { while (oxr.Read()) { if (oxr.ElementType == typeof(CalculationCell)) { cc = (CalculationCell)oxr.LoadCurrentElement(); if (cc.SheetId == null) { cc.SheetId = iCurrentSheetId; } else { if (cc.SheetId.Value != iCurrentSheetId) iCurrentSheetId = cc.SheetId.Value; } slcc.FromCalculationCell(cc); slwb.CalculationCells.Add(slcc); } } } } WorksheetPart wsp; foreach (SLSheet sheet in slwb.Sheets) { wsp = (WorksheetPart)wbp.GetPartById(sheet.Id); foreach (TableDefinitionPart tdp in wsp.TableDefinitionParts) { if (tdp.Table.Id != null && !slwb.TableIds.Contains(tdp.Table.Id.Value)) slwb.TableIds.Add(tdp.Table.Id.Value); if (tdp.Table.Name != null && !slwb.TableNames.Contains(tdp.Table.Name.Value)) slwb.TableNames.Add(tdp.Table.Name.Value); } } bool bFound = false; string sRelID = string.Empty; foreach (SLSheet sheet in slwb.Sheets) { bFound = false; foreach (WorksheetPart wspFound in wbp.WorksheetParts) { sRelID = wbp.GetIdOfPart(wspFound); if (sheet.Id.Equals(sRelID, StringComparison.InvariantCultureIgnoreCase)) { sheet.SheetType = SLSheetType.Worksheet; bFound = true; break; } } if (!bFound) { foreach (ChartsheetPart csp in wbp.ChartsheetParts) { sRelID = wbp.GetIdOfPart(csp); if (sheet.Id.Equals(sRelID, StringComparison.InvariantCultureIgnoreCase)) { sheet.SheetType = SLSheetType.Chartsheet; bFound = true; break; } } } if (!bFound) { foreach (DialogsheetPart dsp in wbp.DialogsheetParts) { sRelID = wbp.GetIdOfPart(dsp); if (sheet.Id.Equals(sRelID, StringComparison.InvariantCultureIgnoreCase)) { sheet.SheetType = SLSheetType.DialogSheet; bFound = true; break; } } } if (!bFound) { foreach (MacroSheetPart msp in wbp.MacroSheetParts) { sRelID = wbp.GetIdOfPart(msp); if (sheet.Id.Equals(sRelID, StringComparison.InvariantCultureIgnoreCase)) { sheet.SheetType = SLSheetType.Macrosheet; bFound = true; break; } } } } string sWorksheetName = SLConstants.DefaultFirstSheetName; int i = 1; bool bCannotFind = true; bool bIsLegit = true; if (wbp.WorksheetParts.Count() == 0) { // no worksheets! Apparently an Excel file with only 1 dialog sheet is perfectly legit... // come up with a legit worksheet name that's not already taken... i = 1; bCannotFind = true; while (bCannotFind) { sWorksheetName = string.Format("Sheet{0}", i); bIsLegit = true; foreach (SLSheet sheet in slwb.Sheets) { if (sheet.Name.Equals(sWorksheetName, StringComparison.InvariantCultureIgnoreCase)) { bIsLegit = false; break; } } ++i; if (bIsLegit) bCannotFind = false; } AddWorksheet(sWorksheetName); } else { bFound = false; // there's a given worksheet name if (SheetNameOnOpen.Length > 0) { foreach (SLSheet sheet in slwb.Sheets) { if (sheet.Name.Equals(SheetNameOnOpen, StringComparison.InvariantCultureIgnoreCase) && sheet.SheetType == SLSheetType.Worksheet) { giSelectedWorksheetID = sheet.SheetId; gsSelectedWorksheetName = sheet.Name; gsSelectedWorksheetRelationshipID = sheet.Id; bFound = true; break; } } } if (!bFound) { // we get here either if there's no given worksheet name (bFound is still false), // or there's a given worksheet name but corresponding values weren't found. // The given worksheet name must be that of a worksheet. A chartsheet name is // considered "invalid". // Either way, we use the first available worksheet as the selected worksheet. wsp = wbp.WorksheetParts.First(); sRelID = wbp.GetIdOfPart(wsp); foreach (SLSheet sheet in slwb.Sheets) { if (sheet.Id.Equals(sRelID, StringComparison.InvariantCultureIgnoreCase)) { giSelectedWorksheetID = sheet.SheetId; gsSelectedWorksheetName = sheet.Name; gsSelectedWorksheetRelationshipID = sheet.Id; bFound = true; break; } } } if (bFound) { // A viable worksheet should be found by now. Otherwise, it's probably // a corrupted spreadsheet... LoadSelectedWorksheet(); IsNewWorksheet = false; } else { // why is it not found!?! The file is corrupted somehow... we'll try to recover // by adding a new worksheet and selecting it. Same algorithm as above. i = 1; bCannotFind = true; while (bCannotFind) { sWorksheetName = string.Format("Sheet{0}", i); bIsLegit = true; foreach (SLSheet sheet in slwb.Sheets) { if (sheet.Name.Equals(sWorksheetName, StringComparison.InvariantCultureIgnoreCase)) { bIsLegit = false; break; } } ++i; if (bIsLegit) bCannotFind = false; } AddWorksheet(sWorksheetName); } } }
/// <summary> /// Create a new spreadsheet with a custom theme. /// </summary> /// <param name="ThemeSettings">Custom theme settings.</param> public SLDocument(SLThemeSettings ThemeSettings) { memstream = new MemoryStream(); xl = SpreadsheetDocument.Create(memstream, SpreadsheetDocumentType.Workbook); wbp = xl.AddWorkbookPart(); IsNewSpreadsheet = true; slwb = new SLWorkbook(); this.DocumentProperties = new SLDocumentProperties(); this.DocumentProperties.Created = DateTime.UtcNow.ToString(SLConstants.W3CDTF); LoadBuiltInNumberingFormats(); InitialiseStylesheetWhatNots(ThemeSettings); LoadSharedStringTable(); InitialiseNewSpreadsheet(); }