@@ -38,6 +38,7 @@ public class ExcelChartStyleManager : XmlHelper
3838 internal readonly ExcelChart _chart ;
3939 private readonly ExcelThemeManager _theme ;
4040 private static bool _hasLoadedLibraryFiles = false ;
41+ private readonly object _syncRoot = new object ( ) ;
4142 internal ExcelChartStyleManager ( XmlNamespaceManager nameSpaceManager , ExcelChart chart ) : base ( nameSpaceManager )
4243 {
4344 _chart = chart ;
@@ -72,11 +73,11 @@ internal ExcelChartStyleManager(XmlNamespaceManager nameSpaceManager, ExcelChart
7273 /// A library where chart styles can be loaded for easier access.
7374 /// EPPlus loads most buildin styles into this collection.
7475 /// </summary>
75- public static Dictionary < int , ExcelChartStyleLibraryItem > StyleLibrary = new Dictionary < int , ExcelChartStyleLibraryItem > ( ) ;
76+ public Dictionary < int , ExcelChartStyleLibraryItem > StyleLibrary = new Dictionary < int , ExcelChartStyleLibraryItem > ( ) ;
7677 /// <summary>
7778 /// A library where chart color styles can be loaded for easier access
7879 /// </summary>
79- public static Dictionary < int , ExcelChartStyleLibraryItem > ColorsLibrary = new Dictionary < int , ExcelChartStyleLibraryItem > ( ) ;
80+ public Dictionary < int , ExcelChartStyleLibraryItem > ColorsLibrary = new Dictionary < int , ExcelChartStyleLibraryItem > ( ) ;
8081 /// <summary>
8182 /// Creates an empty style and color for chart, ready to be customized
8283 /// </summary>
@@ -112,7 +113,7 @@ private void CreateColorXml(ZipPackage p)
112113 /// Loads the default chart style library from the internal resource library.
113114 /// Loads styles, colors and the default theme.
114115 /// </summary>
115- public static void LoadStyles ( )
116+ public void LoadStyles ( )
116117 {
117118 var assembly = Assembly . GetExecutingAssembly ( ) ;
118119 var defaultStyleLibrary = assembly . GetManifestResourceStream ( "OfficeOpenXml.resources.DefaultChartStyles.ecs" ) ;
@@ -124,7 +125,7 @@ public static void LoadStyles()
124125 /// </summary>
125126 /// <param name="directory">Load all *.ecs files from the directory</param>
126127 /// <param name="clearLibrary">If true, clear the library before load.</param>
127- public static void LoadStyles ( DirectoryInfo directory , bool clearLibrary = true )
128+ public void LoadStyles ( DirectoryInfo directory , bool clearLibrary = true )
128129 {
129130 if ( clearLibrary )
130131 {
@@ -140,7 +141,7 @@ public static void LoadStyles(DirectoryInfo directory, bool clearLibrary=true)
140141 /// </summary>
141142 /// <param name="ecsFile">The file to load</param>
142143 /// <param name="clearLibrary">If true, clear the library before load.</param>
143- public static void LoadStyles ( FileInfo ecsFile , bool clearLibrary = true )
144+ public void LoadStyles ( FileInfo ecsFile , bool clearLibrary = true )
144145 {
145146 using ( var fs = ecsFile . Open ( FileMode . Open , FileAccess . Read , FileShare . Read ) )
146147 {
@@ -152,91 +153,91 @@ public static void LoadStyles(FileInfo ecsFile, bool clearLibrary = true)
152153 /// </summary>
153154 /// <param name="stream">The stream to load</param>
154155 /// <param name="clearLibrary">If true, clear the library before load.</param>
155- public static void LoadStyles ( Stream stream , bool clearLibrary = true )
156+ public void LoadStyles ( Stream stream , bool clearLibrary = true )
156157 {
157158 LoadStyles ( stream , clearLibrary , "The stream" ) ;
158159 }
159- private static void LoadStyles ( Stream stream , bool clearLibrary , string filename )
160+ private void LoadStyles ( Stream stream , bool clearLibrary , string filename )
160161 {
161- if ( clearLibrary )
162- {
163- StyleLibrary . Clear ( ) ;
164- }
165- try
166- {
167- using ( stream )
162+ if ( clearLibrary )
168163 {
169- var zipStream = new ZipInputStream ( stream ) ;
170- ZipEntry entry ;
171- while ( ( entry = zipStream . GetNextEntry ( ) ) != null )
164+ StyleLibrary . Clear ( ) ;
165+ }
166+ try
167+ {
168+ using ( stream )
172169 {
173- if ( entry . IsDirectory || ! entry . FileName . EndsWith ( ".xml" ) || entry . UncompressedSize <= 0 ) continue ;
174-
175- var name = new FileInfo ( entry . FileName ) . Name ;
176- int id = 0 ;
177- try
170+ var zipStream = new ZipInputStream ( stream ) ;
171+ ZipEntry entry ;
172+ while ( ( entry = zipStream . GetNextEntry ( ) ) != null )
178173 {
179- if ( name . StartsWith ( "colors" , StringComparison . InvariantCultureIgnoreCase ) )
174+ if ( entry . IsDirectory || ! entry . FileName . EndsWith ( ".xml" ) || entry . UncompressedSize <= 0 ) continue ;
175+
176+ var name = new FileInfo ( entry . FileName ) . Name ;
177+ int id = 0 ;
178+ try
180179 {
181- id = int . Parse ( name . Substring ( 6 , name . Length - 10 ) ) ;
182- if ( ColorsLibrary . ContainsKey ( id ) ) continue ;
180+ if ( name . StartsWith ( "colors" , StringComparison . InvariantCultureIgnoreCase ) )
181+ {
182+ id = int . Parse ( name . Substring ( 6 , name . Length - 10 ) ) ;
183+ if ( ColorsLibrary . ContainsKey ( id ) ) continue ;
184+ }
185+ else if ( name . StartsWith ( "style" , StringComparison . InvariantCultureIgnoreCase ) )
186+ {
187+ id = int . Parse ( name . Substring ( 5 , name . Length - 9 ) ) ;
188+ if ( StyleLibrary . ContainsKey ( id ) ) continue ;
189+ }
190+ else if ( name . Equals ( "defaulttheme.xml" , StringComparison . InvariantCultureIgnoreCase ) )
191+ {
192+ var themeXml = UncompressEntry ( zipStream , entry ) ;
193+ ExcelThemeManager . _defaultTheme = themeXml ;
194+ continue ;
195+ }
196+ else
197+ {
198+ if ( name . StartsWith ( "defaulttheme" , StringComparison . InvariantCultureIgnoreCase ) == false )
199+ {
200+ throw ( new InvalidDataException ( $ "{ filename } contains a the file { entry . FileName } , with an invalid filename. Please make sure files in the library are named Colors[id].xml or style[id].xml, where [id] is replaced by the id to access the style in the library") ) ;
201+ }
202+ }
183203 }
184- else if ( name . StartsWith ( "style" , StringComparison . InvariantCultureIgnoreCase ) )
204+ catch
185205 {
186- id = int . Parse ( name . Substring ( 5 , name . Length - 9 ) ) ;
187- if ( StyleLibrary . ContainsKey ( id ) ) continue ;
206+ throw ( new InvalidDataException ( $ "{ filename } contains a the file { entry . FileName } , with an invalid filename. Please make sure files in the library are named Colors[id].xml or style[id].xml, where [id] is replaced by the id to access the style in the library") ) ;
188207 }
189- else if ( name . Equals ( "defaulttheme.xml" , StringComparison . InvariantCultureIgnoreCase ) )
208+
209+
210+ //Extract and set
211+ var uncompressedContent = UncompressEntry ( zipStream , entry ) ;
212+ var item = new ExcelChartStyleLibraryItem ( ) { Id = id , XmlString = uncompressedContent } ;
213+ if ( name [ 0 ] == 'c' ) //Colors
190214 {
191- var themeXml = UncompressEntry ( zipStream , entry ) ;
192- ExcelThemeManager . _defaultTheme = themeXml ;
193- continue ;
215+ if ( ColorsLibrary . ContainsKey ( item . Id ) == false )
216+ {
217+ ColorsLibrary . Add ( item . Id , item ) ;
218+ }
194219 }
195220 else
196221 {
197- if ( name . StartsWith ( "defaulttheme" , StringComparison . InvariantCultureIgnoreCase ) == false )
222+ if ( StyleLibrary . ContainsKey ( item . Id ) == false )
198223 {
199- throw ( new InvalidDataException ( $ " { filename } contains a the file { entry . FileName } , with an invalid filename. Please make sure files in the library are named Colors[id].xml or style[id].xml, where [id] is replaced by the id to access the style in the library" ) ) ;
224+ StyleLibrary . Add ( item . Id , item ) ;
200225 }
201226 }
202227 }
203- catch
204- {
205- throw ( new InvalidDataException ( $ "{ filename } contains a the file { entry . FileName } , with an invalid filename. Please make sure files in the library are named Colors[id].xml or style[id].xml, where [id] is replaced by the id to access the style in the library") ) ;
206- }
207-
208-
209- //Extract and set
210- var uncompressedContent = UncompressEntry ( zipStream , entry ) ;
211- var item = new ExcelChartStyleLibraryItem ( ) { Id = id , XmlString = uncompressedContent } ;
212- if ( name [ 0 ] == 'c' ) //Colors
213- {
214- if ( ColorsLibrary . ContainsKey ( item . Id ) == false )
215- {
216- ColorsLibrary . Add ( item . Id , item ) ;
217- }
218- }
219- else
220- {
221- if ( StyleLibrary . ContainsKey ( item . Id ) == false )
222- {
223- StyleLibrary . Add ( item . Id , item ) ;
224- }
225- }
226228 }
227229 }
228- }
229- catch ( Exception ex )
230- {
231- if ( ex is InvalidDataException )
232- {
233- throw ;
234- }
235- else
230+ catch ( Exception ex )
236231 {
237- throw ( new InvalidDataException ( $ "{ filename } has an invalid format.", ex ) ) ;
232+ if ( ex is InvalidDataException )
233+ {
234+ throw ;
235+ }
236+ else
237+ {
238+ throw ( new InvalidDataException ( $ "{ filename } has an invalid format.", ex ) ) ;
239+ }
238240 }
239- }
240241 }
241242
242243 private static string UncompressEntry ( ZipInputStream zipStream , ZipEntry entry )
0 commit comments