Aspose cells for .Net , Style - Serialize?

Hello
We are using Aspose cells for .Net . We have a requirement to load the cell Style properties into a database and be able to query them later. We are thinking about Serializing the Style object into XML format and loading into database. Is it possible to do so ? Please let us know.

Thanks,
POP Displays USA, Inc.

@Thahseen,

Thanks for using Aspose APIs.

Not all properties of Style objects are required by you. A complete Style object with default properties can be created using Workbook.CreateStyle(), so there is no need to save the default properties. You only need to save those properties which are needed by you.

e.g.

  • Style.Name
  • Style.Pattern
  • Style.ForegroundColor
  • Style.BackgroundColor
  • Style.Font.Name
  • Style.Font.Size

etc.

It means, you should save only the properties of your interest.

However, if you want to serialize the entire Style object, then we have researched a bit and found, there is XmlSerializer class which serializes the .NET object into XML. There might be other serializing objects or classes in .NET that serialize the .NET objects.

Please research on internet how to serialize any .NET object.

Hello shakeel ,
We tried XmlSerializer, but we got the following error while trying to Serialize Style object.

System.InvalidOperationException: There was an error reflecting type ‘Aspose.Cells.Style’. —> System.InvalidOperationException: Cannot serialize member ‘Aspose.Cells.Style.BackgroundThemeColor’ of type ‘Aspose.Cells.ThemeColor’, see inner exception for more details. —> System.InvalidOperationException: Aspose.Cells.ThemeColor cannot be serialized because it does not have a parameterless constructor.

Please let us know, if you find anything.

Thanks.

@Thahseen

Thanks for considering Aspose APIs.

Please spare us some time, we will look into your issue and update you asap.

@Thahseen,

Thanks for considering Aspose APIs.

We are afraid, it is not possible for us to support this feature. However, as a workaround, you should write xml and parse xml by yourself.

For example, Style object may look like this in xml.

Please change the extension of your file e.g. Sample.xlsx to Sample.xlsx.zip and extract its contents.

<Style ss:ID="S90">
   <ss:Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1" />
   <ss:Borders>
    <ss:Border ss:Position="Left" ss:Color="#999999" ss:LineStyle="Continuous" ss:Weight="1" />
    <ss:Border ss:Position="Top" ss:Color="#999999" ss:LineStyle="Continuous" ss:Weight="1" />
    <ss:Border ss:Position="Right" ss:Color="#999999" ss:LineStyle="Continuous" ss:Weight="1" />
   </ss:Borders>
   <ss:Font ss:Bold="1" ss:FontName="Arial" ss:Size="10" />
   <ss:fill ss:Color="#FFFF99" ss:Pattern="Solid" />
</Style>

@Thahseen

You can write the similar code as we have shown here for your needs.

C#

Workbook wb = new Workbook(@"D:\Filetemp\book1.xlsx");

wb.Settings.UpdateLinksType = (UpdateLinksType.Never);
Style style = wb.Worksheets[0].Cells["A1"].GetStyle();
MemoryStream ms = new MemoryStream();

XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8);
writer.WriteStartElement("Style");
if (style.IsModified(StyleModifyFlag.CellShading))
{
	writer.WriteStartElement("Fill");
	writer.WriteAttributeString("Pattern", style.Pattern.ToString());
	writer.WriteAttributeString("Color", style.ForegroundColor.ToArgb().ToString("X"));
	writer.WriteEndElement();
}
writer.WriteEndElement();
writer.Flush();
ms.Flush();
Console.WriteLine(Encoding.UTF8.GetString(ms.GetBuffer(), 3,(int)ms.Length - 3));

Thanks Shakeel for your help!