NPOI Excel set predifined color theme

30 views Asked by At

I am creating an Excel file with NPOI and simply want to set a global color theme. Like we can do manually in Excel, for example set the theme to "Blue" instead of the default "Office" :

Excel layout color theme

XSSFWorkbook as a GetTheme() which returns null by default.

I tried

var src = workbook.GetStylesSource();
src.EnsureThemesTable();

but when opening the file Excel says there is an issue.

I tried opening an existing file with a (predifined) theme (GetTheme() was not null) and set it to my new file but nothing changed when I open the file :

var otherTheme = existingWorkbook.GetTheme();
var src = workbook.GetStylesSource();
src.SetTheme(otherTheme);

I also tried to modify it but still nothing changes :

var theme = src.GetTheme();
var accent = theme.GetThemeColor(4);
accent.Theme = 2;
var color = Color.BlueViolet;
accent.SetRgb(new byte[] { color.R, color.G, color.B });

I found built in table styles but not sure what it is exactly or how to use it since there is juste a getter :

var src = workbook.GetStylesSource();
var tableStyle = src.GetTableStyle(nameof(XSSFBuiltinTableStyleEnum.PivotStyleLight27));

I don't understand why it is so difficult to do something so simple. I couldn't find anything on the web after searching for three hours, and couldn't find a proper documentation.

0

There are 0 answers