From 35d87391d4cccfba3ba1a02847eda7ded2827e17 Mon Sep 17 00:00:00 2001 From: Przemyslaw Klys Date: Sun, 22 Sep 2024 11:07:36 +0200 Subject: [PATCH] Remove obsolete stuff --- OfficeIMO.Excel/ExcelDocument.cs | 25 +++++++------------- OfficeIMO.Word/WordComment.PrivateMethods.cs | 20 +++++++--------- OfficeIMO.Word/WordCustomProperties.cs | 12 +--------- OfficeIMO.Word/WordDocument.cs | 2 +- 4 files changed, 20 insertions(+), 39 deletions(-) diff --git a/OfficeIMO.Excel/ExcelDocument.cs b/OfficeIMO.Excel/ExcelDocument.cs index 7f68ea68..2b934d0d 100644 --- a/OfficeIMO.Excel/ExcelDocument.cs +++ b/OfficeIMO.Excel/ExcelDocument.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.IO.Packaging; @@ -16,15 +16,12 @@ public List Sheets { List listExcel = new List(); if (_spreadSheetDocument.WorkbookPart.Workbook.Sheets != null) { var elements = _spreadSheetDocument.WorkbookPart.Workbook.Sheets.OfType().ToList(); - - - foreach (Sheet s in elements) { - ExcelSheet excelSheet = new ExcelSheet(this, _spreadSheetDocument, s); - id.Add(s.SheetId); - listExcel.Add(excelSheet); - } + foreach (Sheet s in elements) { + ExcelSheet excelSheet = new ExcelSheet(this, _spreadSheetDocument, s); + id.Add(s.SheetId); + listExcel.Add(excelSheet); } - + } return listExcel; } @@ -41,7 +38,6 @@ public static ExcelDocument Create(string filePath) { // Create a spreadsheet document by supplying the filepath. // By default, AutoSave = true, Editable = true, and Type = xlsx. SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook); - document._spreadSheetDocument = spreadSheetDocument; // Add a WorkbookPart to the document. @@ -73,9 +69,6 @@ public static ExcelDocument Load(string filePath, bool readOnly = false, bool au document._spreadSheetDocument = spreadSheetDocument; //// Add a WorkbookPart to the document. - //WorkbookPart workbookpart = spreadSheetDocument.AddWorkbookPart(); - //workbookpart.Workbook = new Workbook(); - document._workBookPart = document._spreadSheetDocument.WorkbookPart; return document; @@ -84,7 +77,7 @@ public static ExcelDocument Load(string filePath, bool readOnly = false, bool au public static ExcelDocument Create(string filePath, string workSheetName) { ExcelDocument excelDocument = Create(filePath); excelDocument.AddWorkSheet(workSheetName); - return excelDocument; + return excelDocument; } public ExcelSheet AddWorkSheet(string workSheetName = "") { @@ -92,7 +85,7 @@ public ExcelSheet AddWorkSheet(string workSheetName = "") { return excelSheet; } - + public void Open(string filePath = "", bool openExcel = true) { if (filePath == "") { filePath = this.FilePath; @@ -101,7 +94,7 @@ public void Open(string filePath = "", bool openExcel = true) { } public void Close() { - this._spreadSheetDocument.Close(); + this._spreadSheetDocument.Dispose(); } public void Save(string filePath, bool openExcel) { diff --git a/OfficeIMO.Word/WordComment.PrivateMethods.cs b/OfficeIMO.Word/WordComment.PrivateMethods.cs index 6d13aba5..b22a0374 100644 --- a/OfficeIMO.Word/WordComment.PrivateMethods.cs +++ b/OfficeIMO.Word/WordComment.PrivateMethods.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -38,18 +38,16 @@ internal static string GetNewId(WordDocument document, Comments comments) { internal static Comments GetCommentsPart(WordDocument document) { Comments comments = null; - if (document._wordprocessingDocument.MainDocumentPart.GetPartsCountOfType() > 0) { - comments = document._wordprocessingDocument.MainDocumentPart.WordprocessingCommentsPart.Comments; - - //if (comments.HasChildren) { - // Obtain an unused ID. - //id = (comments.Descendants().Select(e => int.Parse(e.Id.Value)).Max() + 1).ToString(); - //} + if (document._wordprocessingDocument.MainDocumentPart != null && document._wordprocessingDocument.MainDocumentPart.GetPartsOfType().Any()) { + if (document._wordprocessingDocument.MainDocumentPart.WordprocessingCommentsPart != null) + comments = document._wordprocessingDocument.MainDocumentPart.WordprocessingCommentsPart.Comments; } else { // No WordprocessingCommentsPart part exists, so add one to the package. - WordprocessingCommentsPart commentPart = document._wordprocessingDocument.MainDocumentPart.AddNewPart(); - commentPart.Comments = new Comments(); - comments = commentPart.Comments; + if (document._wordprocessingDocument.MainDocumentPart != null) { + WordprocessingCommentsPart commentPart = document._wordprocessingDocument.MainDocumentPart.AddNewPart(); + commentPart.Comments = new Comments(); + comments = commentPart.Comments; + } } return comments; diff --git a/OfficeIMO.Word/WordCustomProperties.cs b/OfficeIMO.Word/WordCustomProperties.cs index b341a50b..54ad95f7 100644 --- a/OfficeIMO.Word/WordCustomProperties.cs +++ b/OfficeIMO.Word/WordCustomProperties.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -138,7 +138,6 @@ public CustomDocumentProperty Add(string name, object value, PropertyTypes prope } private void Add(string name, CustomDocumentProperty newProp) { - string returnValue = null; if (_customProperties != null) { // This will trigger an exception if the property's Name // property is null, but if that happens, the property is damaged, @@ -148,10 +147,7 @@ private void Add(string name, CustomDocumentProperty newProp) { // Does the property exist? If so, get the return value, // and then delete the property. if (prop != null) { - //returnValue = prop.InnerText; prop.Remove(); - - //_customProperties.RemoveChild(prop); } // Append the new property, and @@ -162,14 +158,8 @@ private void Add(string name, CustomDocumentProperty newProp) { foreach (CustomDocumentProperty item in _customProperties) { item.PropertyId = pid++; } - - //this._wordprocessingDocument.CustomFilePropertiesPart.Properties.Save(); _customProperties.Save(); - - //this._wordprocessingDocument.CustomFilePropertiesPart.Properties = _customProperties; } - - // return returnValue; } private void CreateCustomProperty(WordDocument document) { diff --git a/OfficeIMO.Word/WordDocument.cs b/OfficeIMO.Word/WordDocument.cs index 53d2f83e..de25d101 100644 --- a/OfficeIMO.Word/WordDocument.cs +++ b/OfficeIMO.Word/WordDocument.cs @@ -895,7 +895,7 @@ public void Dispose() { if (this._wordprocessingDocument != null) { try { - this._wordprocessingDocument.Close(); + this._wordprocessingDocument.Dispose(); } catch { // ignored }