It appears that each time a workbook is saved a empty NumFmt (NumberingFormat) element is appended in the stylesheet. Once this is done enough times, it will start stepping on the built-in formats. I was noticing this with the percent format which I belive is #10.
The excel file attached has one cell, formatted as a percentage. After 9 saves, the formatting is lost.
In my practical application the stream was being passed around and many classes were opening a workbook from the stream and saving back to the stream.
This was the latest version, 77207
byte[] calculatedExcelStreamBytes = File.ReadAllBytes("c:\\LoseFormatTest.xlsx");
using (MemoryStream calculatedExcelStream = new MemoryStream())
{
calculatedExcelStream.Write(calculatedExcelStreamBytes, 0, calculatedExcelStreamBytes.Length);
XLWorkbook b = new XLWorkbook(calculatedExcelStream);
b.Save(); // Each save will increase the number of formats written, looking to step on built-in formats
b.Save();
b.Save();
b.Save();
b.Save();
b.Save();
b.Save();
b.Save();
b.Save(); // At this point we are stepping on the percent format
b.Save();
b.Save();
b.Save();
b.Save();
using (FileStream fs = new FileStream("C:\\LoseFormatTest.Modify.xlsx", FileMode.Create, FileAccess.Write))
{
calculatedExcelStream.WriteTo(fs);
fs.Flush();
fs.Close();
}
}
Comments: ** Comment from web user: FeelingPhoggy **
I have put together a relatively simple application that illustrates this issue. Attached is the full project and the original Excel workbook and the resulting workbook.
Essentially, what this mimics is a way that I was looking to use an Excel workbook as a simple data repository for a process that needs to be run monthly. The "MonthlyTotals" worksheet keeps the number of records and the total dollar amount of those records for up to 6 months. Once the worksheet has 6 months of data and new data is to be added, the 3rd column (oldest month) is dropped and the other 5 columns moved over to the left with the new month put in the 8th column.
As ISOLDOUT found previously, it works just fine the first few times that it is ran, but once the workbook is saved many times, the formatting gets completely out of whack. In fact, after just saving the workbook 5 times, the dates on the "HeaderPage" worksheet are not formatted like they were in the original.