Updating Excel Header makes the excel document unreadable

Topics: Developer Forum
Apr 23, 2010 at 3:55 PM


    I tried to update the Excel header and it worked the first time. When I try to update the same document for the second time it does not throw an error during code execution but makes the excel document unreadable.

I get "Excel found unreadable content in Book1.xlsx. Do you want to recover the contents of this workbook ? If you trust the source of this workbook Click Yes." error.

The following is the code I was trying :

 SPFile oFile = oItem.File;
                    if(oFile.CheckOutStatus == SPFile.SPCheckOutStatus.None)

                    MemoryStream fileStream = new MemoryStream();
                    fileStream.Write(oFile.OpenBinary(), 0, Convert.ToInt32(oFile.TotalLength));

                      using (ExcelPackage xlPackage = new ExcelPackage(fileStream))

                        ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];

                        worksheet.HeaderFooter.oddHeader.CenteredText = "Tinned Goods Sales 2";
                        // add the page number to the footer plus the total number of pages
                        worksheet.HeaderFooter.oddFooter.RightAlignedText = string.Format("Page {0} of {1}", ExcelHeaderFooter.PageNumber,ExcelHeaderFooter.NumberOfPages);


Any ideas ?

Thanks in Advance