Styles

Topics: User Forum
Jan 26, 2007 at 11:17 AM
Greetings
You mention that it is possible to " load your own style.xml file". I have made a XML file called style.xml which contains some different styles but I dont know how to use them from the c# code. How do i do that?
Mar 5, 2007 at 2:47 PM
I may be wrong here, but it would actually be a matter of trading out files. If the original project (unzipped) has a styles.xml file, then you would but need to switch it out. The switch would then appear automatic if the names in your style sheet still agree with those in the data xml.

I don't think excelpackage has real control over this as it is a wrapper. You will have to look at the Microsoft website for the system.io.package library in order to see how to deal with individual files within the hierarchy structure.
Jan 16, 2008 at 12:39 PM
Look at the StylesXml member of the ExcelWorkbook class (I think it's workbook). It allows you to assign an XmlDocument which will be used as the styles.xml. Be warned that you will get warnings when you try and open the file if there are styles that are not used.
Apr 11, 2008 at 1:55 AM
Perhaps I was using it wrong, but when I tried to read in and use an existing style.xml file, I would always get an error on save. I had to add the code below to the setter of the StylesXml property in ExcelWorkbook.cs (after assigning _xmlStyles = value;)

if (!_xlPackage.Package.PartExists(StylesUri))
{
// create a new styles part and add to the package
PackagePart partSyles = _xlPackage.Package.CreatePart(StylesUri, @"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");

// save it to the package
StreamWriter streamStyles = new StreamWriter(partSyles.GetStream(FileMode.Create, FileAccess.Write));
_xmlStyles.Save(streamStyles);
streamStyles.Close();
_xlPackage.Package.Flush();

// create the relationship between the workbook and the new shared strings part
_xlPackage.Workbook.Part.CreateRelationship(StylesUri, TargetMode.Internal, ExcelPackage.schemaRelationships + "/styles");
_xlPackage.Package.Flush();
}