Skip to content

Commit

Permalink
- Excel Bug Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DraviaVemal committed Apr 16, 2024
1 parent 497f93c commit c5578c2
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 113 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<LangVersion>5.0</LangVersion>
<OutputPath>..\bin\</OutputPath>
<Authors>DraviaVemal</Authors>
<Version>2.6.6-Alpha</Version>
<Version>2.6.7</Version>
<Company>DraviaVemal</Company>
<Copyright>Copyright (c) 2023 DraviaVemal
Permission is hereby granted, free of charge, to any person obtaining a copy of this
Expand Down
22 changes: 17 additions & 5 deletions Global/Parts/CoreProperties.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) DraviaVemal. Licensed under the MIT License. See License in the project root.
using System;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
namespace OpenXMLOffice.Global_2007
{
/// <summary>
Expand All @@ -12,14 +14,10 @@ public class CoreProperties
/// <summary>
///
/// </summary>
public static void AddCoreProperties(Stream stream, CorePropertiesModel corePropertiesModel = null)
public static void AddCoreProperties(Stream stream, CorePropertiesModel corePropertiesModel)
{
try
{
if (corePropertiesModel == null)
{
corePropertiesModel = new CorePropertiesModel();
}
string timeStamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
using (XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8))
{
Expand Down Expand Up @@ -71,5 +69,19 @@ public static void AddCoreProperties(Stream stream, CorePropertiesModel coreProp
Console.WriteLine("Core Property Error" + ex.Message);
}
}
/// <summary>
///
/// </summary>
public static void UpdateModifiedDetails(Stream stream, CorePropertiesModel corePropertiesModel = null)
{
XDocument doc = XDocument.Load(stream);
stream.Position = 0;
stream.SetLength(0);
XElement lastModifiedByElement = doc.Descendants().First(e => e.Name.LocalName == "lastModifiedBy");
XElement modifiedElement = doc.Descendants().First(e => e.Name.LocalName == "modified");
lastModifiedByElement.SetValue(corePropertiesModel.creator);
modifiedElement.SetValue(DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"));
doc.Save(stream);
}
}
}
6 changes: 5 additions & 1 deletion Presentation/Internal/PresentationCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ private void InitialisePresentation(PresentationProperties powerPointProperties)
if (presentationDocument.CoreFilePropertiesPart == null)
{
presentationDocument.AddCoreFilePropertiesPart();
CoreProperties.AddCoreProperties(presentationDocument.CoreFilePropertiesPart.GetStream(FileMode.OpenOrCreate, FileAccess.ReadWrite));
CoreProperties.AddCoreProperties(presentationDocument.CoreFilePropertiesPart.GetStream(FileMode.OpenOrCreate, FileAccess.ReadWrite), powerPointProperties.coreProperties);
}
else
{
CoreProperties.UpdateModifiedDetails(presentationDocument.CoreFilePropertiesPart.GetStream(FileMode.OpenOrCreate, FileAccess.ReadWrite), powerPointProperties.coreProperties);
}
PresentationPart presentationPart = presentationDocument.PresentationPart ?? presentationDocument.AddPresentationPart();
if (presentationPart.Presentation == null)
Expand Down
4 changes: 4 additions & 0 deletions Presentation/Models/PowerPointModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class PresentationProperties
/// Gets or sets the theme of the presentation.
/// </summary>
public ThemePallet theme = new ThemePallet();
/// <summary>
/// Add Meta Data Details to File
/// </summary>
public CorePropertiesModel coreProperties = new CorePropertiesModel();
}
/// <summary>
/// Represents the settings of a presentation.
Expand Down
6 changes: 3 additions & 3 deletions Spreadsheet/Internal/Spreadsheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace OpenXMLOffice.Spreadsheet_2007
{
internal class Spreadsheet : SpreadsheetCore
{
internal Spreadsheet(Excel excel, SpreadsheetProperties spreadsheetProperties) : base(excel, spreadsheetProperties) { }
internal Spreadsheet(Excel excel, string filePath, bool isEditable, SpreadsheetProperties spreadsheetProperties) : base(excel, filePath, isEditable, spreadsheetProperties) { }
internal Spreadsheet(Excel excel, Stream stream, bool isEditable, SpreadsheetProperties spreadsheetProperties) : base(excel, stream, isEditable, spreadsheetProperties) { }
internal Spreadsheet(Excel excel, ExcelProperties spreadsheetProperties) : base(excel, spreadsheetProperties) { }
internal Spreadsheet(Excel excel, string filePath, bool isEditable, ExcelProperties spreadsheetProperties) : base(excel, filePath, isEditable, spreadsheetProperties) { }
internal Spreadsheet(Excel excel, Stream stream, bool isEditable, ExcelProperties spreadsheetProperties) : base(excel, stream, isEditable, spreadsheetProperties) { }
internal Worksheet AddSheet(string sheetName)
{
sheetName = sheetName ?? string.Format("Sheet{0}", GetMaxSheetId() + 1);
Expand Down
26 changes: 15 additions & 11 deletions Spreadsheet/Internal/SpreadsheetCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ internal class SpreadsheetCore
{
internal readonly Excel excel;
internal readonly SpreadsheetDocument spreadsheetDocument;
internal readonly SpreadsheetInfo spreadsheetInfo = new SpreadsheetInfo();
internal readonly SpreadsheetProperties spreadsheetProperties;
internal readonly ExcelInfo spreadsheetInfo = new ExcelInfo();
internal readonly ExcelProperties spreadsheetProperties;
private readonly StylesService stylesService = new StylesService();
private readonly ShareStringService shareStringService = new ShareStringService();
internal SpreadsheetCore(Excel excel, SpreadsheetProperties spreadsheetProperties = null)
internal SpreadsheetCore(Excel excel, ExcelProperties spreadsheetProperties = null)
{
this.excel = excel;
this.spreadsheetProperties = spreadsheetProperties ?? new SpreadsheetProperties();
this.spreadsheetProperties = spreadsheetProperties ?? new ExcelProperties();
MemoryStream memoryStream = new MemoryStream();
spreadsheetDocument = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook, true);
InitialiseSpreadsheet(this.spreadsheetProperties);
}
internal SpreadsheetCore(Excel excel, string filePath, bool isEditable, SpreadsheetProperties spreadsheetProperties = null)
internal SpreadsheetCore(Excel excel, string filePath, bool isEditable, ExcelProperties spreadsheetProperties = null)
{
this.excel = excel;
this.spreadsheetProperties = spreadsheetProperties ?? new SpreadsheetProperties();
this.spreadsheetProperties = spreadsheetProperties ?? new ExcelProperties();
FileStream reader = new FileStream(filePath, FileMode.Open);
MemoryStream memoryStream = new MemoryStream();
reader.CopyTo(memoryStream);
Expand All @@ -50,10 +50,10 @@ internal SpreadsheetCore(Excel excel, string filePath, bool isEditable, Spreadsh
}
ReadDataFromFile();
}
internal SpreadsheetCore(Excel excel, Stream stream, bool isEditable, SpreadsheetProperties spreadsheetProperties = null)
internal SpreadsheetCore(Excel excel, Stream stream, bool isEditable, ExcelProperties spreadsheetProperties = null)
{
this.excel = excel;
this.spreadsheetProperties = spreadsheetProperties ?? new SpreadsheetProperties();
this.spreadsheetProperties = spreadsheetProperties ?? new ExcelProperties();
spreadsheetDocument = SpreadsheetDocument.Open(stream, isEditable, new OpenSettings()
{
AutoSave = true
Expand Down Expand Up @@ -190,12 +190,16 @@ private void InitialiseStyle()
/// <summary>
/// Common Spreadsheet perparation process used by all constructor
/// </summary>
private void InitialiseSpreadsheet(SpreadsheetProperties SpreadsheetProperties)
private void InitialiseSpreadsheet(ExcelProperties excelProperties)
{
if (spreadsheetDocument.CoreFilePropertiesPart == null)
{
spreadsheetDocument.AddCoreFilePropertiesPart();
G.CoreProperties.AddCoreProperties(spreadsheetDocument.CoreFilePropertiesPart.GetStream(FileMode.OpenOrCreate, FileAccess.ReadWrite));
G.CoreProperties.AddCoreProperties(spreadsheetDocument.CoreFilePropertiesPart.GetStream(FileMode.OpenOrCreate, FileAccess.ReadWrite), excelProperties.coreProperties);
}
else
{
G.CoreProperties.UpdateModifiedDetails(spreadsheetDocument.CoreFilePropertiesPart.GetStream(FileMode.OpenOrCreate, FileAccess.ReadWrite), excelProperties.coreProperties);
}
if (GetWorkbookPart().Workbook == null)
{
Expand All @@ -211,7 +215,7 @@ private void InitialiseSpreadsheet(SpreadsheetProperties SpreadsheetProperties)
{
GetWorkbookPart().AddNewPart<ThemePart>(GetNextSpreadSheetRelationId());
}
G.Theme theme = new G.Theme(SpreadsheetProperties.theme);
G.Theme theme = new G.Theme(excelProperties.theme);
GetWorkbookPart().ThemePart.Theme = theme.GetTheme();
InitialiseStyle();
GetWorkbookPart().Workbook.Save();
Expand Down
12 changes: 8 additions & 4 deletions Spreadsheet/Models/2007/SpreadsheetModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@ namespace OpenXMLOffice.Spreadsheet_2007
/// <summary>
/// Represents the properties of a column in a worksheet.
/// </summary>
public class SpreadsheetProperties
public class ExcelProperties
{
/// <summary>
/// Spreadsheet settings
/// </summary>
public SpreadsheetSettings settings = new SpreadsheetSettings();
public ExcelSettings settings = new ExcelSettings();
/// <summary>
/// Spreadsheet theme settings
/// </summary>
public ThemePallet theme = new ThemePallet();
/// <summary>
/// Add Meta Data Details to File
/// </summary>
public CorePropertiesModel coreProperties = new CorePropertiesModel();
}
/// <summary>
/// Represents the settings of a spreadsheet.
/// </summary>
public class SpreadsheetSettings
public class ExcelSettings
{
}
internal class SpreadsheetInfo
internal class ExcelInfo
{
public bool isEditable = true;
public bool isExistingFile = false;
Expand Down
70 changes: 42 additions & 28 deletions Spreadsheet/Models/2007/StylesModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public enum PatternTypeValues
/// Solid Pattern Type
/// </summary>
SOLID
}/// <summary>
/// Font Scheme values
/// </summary>
}
/// <summary>
/// Font Scheme values
/// </summary>
public enum SchemeValues
{
/// <summary>
Expand Down Expand Up @@ -103,7 +104,7 @@ public enum StyleValues
/// <summary>
///
/// </summary>
public enum StyleColorTypeValues
public enum ColorSettingTypeValues
{
/// <summary>
///
Expand All @@ -126,36 +127,38 @@ public class BorderSetting
/// <summary>
/// Gets or sets the color of the border.
/// </summary>
public string color = "64";
public ColorSetting BorderColor { get; set; }
/// <summary>
/// Gets or sets the style of the border.
/// </summary>
public StyleValues style = StyleValues.NONE;
public StyleValues Style { get; set; }
/// <summary>
///
/// </summary>
public BorderSetting()
{
BorderColor = new ColorSetting()
{
ColorSettingTypeValues = ColorSettingTypeValues.INDEXED,
Value = "64"
};
Style = StyleValues.NONE;
}
}
/// <summary>
/// Represents the border style of a cell in a worksheet.
/// </summary>
public class BorderStyle
{
/// <summary>
/// Initializes a new instance of the <see cref="BorderStyle"/> class.
/// Gets or sets the ID of the border style.
/// </summary>
public BorderStyle()
{
Bottom = new BorderSetting();
Left = new BorderSetting();
Right = new BorderSetting();
Top = new BorderSetting();
}
public uint Id { get; set; }
/// <summary>
/// Bottom border style
/// </summary>
public BorderSetting Bottom { get; set; }
/// <summary>
/// Gets or sets the ID of the border style.
/// </summary>
public uint Id { get; set; }
/// <summary>
/// Left border style
/// </summary>
public BorderSetting Left { get; set; }
Expand All @@ -167,6 +170,16 @@ public BorderStyle()
/// Top border style
/// </summary>
public BorderSetting Top { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="BorderStyle"/> class.
/// </summary>
public BorderStyle()
{
Bottom = new BorderSetting();
Left = new BorderSetting();
Right = new BorderSetting();
Top = new BorderSetting();
}
}
/// <summary>
/// Represents the style of a cell in a worksheet.
Expand Down Expand Up @@ -236,9 +249,9 @@ public class CellStyleSetting
/// <summary>
/// Gets or sets the text color of the cell. default is 000000
/// </summary>
public StyleColor textColor = new StyleColor()
public ColorSetting textColor = new ColorSetting()
{
StyleColorTypeValues = StyleColorTypeValues.RGB,
ColorSettingTypeValues = ColorSettingTypeValues.RGB,
Value = "000000"
};
/// <summary>
Expand Down Expand Up @@ -336,11 +349,11 @@ public FillStyle()
/// <summary>
/// Gets or sets the background color of the cell.
/// </summary>
public StyleColor BackgroundColor { get; set; }
public ColorSetting BackgroundColor { get; set; }
/// <summary>
/// Gets or sets the foreground color of the cell.
/// </summary>
public StyleColor ForegroundColor { get; set; }
public ColorSetting ForegroundColor { get; set; }
/// <summary>
/// Fill style ID
/// </summary>
Expand All @@ -353,22 +366,23 @@ public FillStyle()
/// <summary>
///
/// </summary>
public class StyleColor
public class ColorSetting
{
/// <summary>
///
/// </summary>
public StyleColorTypeValues StyleColorTypeValues { get; set; }
public ColorSettingTypeValues ColorSettingTypeValues { get; set; }
/// <summary>
///
/// </summary>
public string Value { get; set; }
/// <summary>
///
/// </summary>
public StyleColor()
public ColorSetting()
{
StyleColorTypeValues = StyleColorTypeValues.THEME;
ColorSettingTypeValues = ColorSettingTypeValues.THEME;
Value = "1";
}
}
/// <summary>
Expand All @@ -381,7 +395,7 @@ public class FontStyle
/// </summary>
public FontStyle()
{
Color = new StyleColor() { Value = "1" };
Color = new ColorSetting() { Value = "1" };
Family = 2;
Size = 11;
Name = "Calibri";
Expand All @@ -394,7 +408,7 @@ public FontStyle()
/// <summary>
/// Gets or sets the color of the font. default is accent1
/// </summary>
public StyleColor Color { get; set; }
public ColorSetting Color { get; set; }
/// <summary>
/// Gets or sets the font family of the font.
/// </summary>
Expand Down
Loading

0 comments on commit c5578c2

Please sign in to comment.