Hello.
I'm new to NPOI and if I misunderstand something please correct me. I want to write a test application which opens an existing Excel Workbook (created by Excel 2007 or 2010 in 97-2003 xls Format) and removes the column style of column index 4. The first sheet in the Workbook includes several columns formatted in different value formats like Date and Text and different cell colors.
The only function I've found to set a column style was SetDefaultColumnStyle of ISheet.
Here is the code:
```
IWorkbook wb;
ISheet ws;
using (FileStream file = new FileStream(@"..\..\..\Test\Test_Table.xls", FileMode.Open, FileAccess.Read))
{
wb = new HSSFWorkbook(file);
}
ws = wb.GetSheetAt(0);
ws.SetDefaultColumnStyle(4, wb.CreateCellStyle());
using (FileStream file = new FileStream(@"..\..\..\Test\Test_Table_result.xls", FileMode.Create, FileAccess.Write))
{
wb.Write(file);
}
```
For the column E the call of SetDefaultColumnStyle works as expected. But the Colors of the cells in the other columns change. When I compare the original file with the file created by NPOI some of the cell colors are changed.
The description of SetDefaultColumnStyle says "Sets the default column style for a given column. POI will only apply this style to new cells Added to the sheet.". So this function should have no influence on any cell which already has its own cell style, didn't it? From my Point of view is a bug.
To proof that the color change is not a General Problem I also opened the workbook and saved it without changes. The resulting file is smaller than the original Excel file but the content and the formatting keeps the same. So, the Color change of the cells must have something to do with the call to SetDefaultColumnStyle.
It would be great if someone could tell me if there is another possibility to remove the default column style from a column, which does not affect the existing cell styles of the sheet. Furthermore, I hope that this behavior can be fixed in the next stable release of NPOI.
My testing workbook is attatched.
Best regards
Herbie
Comments: ** Comment from web user: Herbiee **
Hello again.
So, finally I found the root cause of this unpleasant behavior and I want to share with you my findings. It is not a bug it's a feature :).
I've read the Microsoft Specification [MS-XLS] for the XLS format and found, that Microsoft added with Excel 2007 support for more than the 56 colors.
Therefore they defined the XFExt record ([MS-XLS].pdf page 610), which can hold different format information that overwrites the settings of a XF record (each XFExt record references a specific XF record). To be backward compatible an additional record, the XFCRC record ([MS-XLS].pdf page 609), was defined.
The XFCRC record must exist exactly once, if an XLS file keeps any XFExt record. It must include the exact count of XF records in the XLS-file and a checksum over the data portion of all XF records.
When Excel loads an Excel file, it checks if an XFCRC record exists behind the last XF record. If so it checks if the XF record count and the checksum in this record match to the loaded XF records. If the XF record count or the checksum does not match, Excel knows that the last application which processed the Excel file does not support the XFExt records and that this application has done some changes to the XF records. Otherwise the count and the checksum would match. In this case Excel ignores the extended format information in any XFExt record and used the format information of the XF records.
POI and NPOI do not support those new records. So, when I call the CreateCellStyle function NPOI creates an additional XF record and adds it to the workbook. When you save this workbook to a file and open it with Excel, it shows only the colors used in the XF records (which are colors from the old standard color table), since the XFCRC record is not updated.
I've proofed this by adding the XFCRC record to NPOI and execute an update of the record before saving the workbook to a file. The resulting file still shows the correct pastel colors. If I disable the XFCRC record support, the pastel colors are replaced by similar colors of the standard color table of excel.
By the way Excel 2007 and above seem to use the XFExt record by default to set a color or other supported format information. When you set a color this color is set in the XFExt record and the most similar color of the standard color palette is set to the corresponding XF record.
Best regards
Herbiee