Getting an "Excel Found Unreadable Content" error when creating copies of worksheets RRS feed

  • Question

  • I need to generate an excel file that will have a varying number of worksheets (I need to generate 1 worksheet for every active product my company is currently selling at the moment).  I have a prebuilt excel file that I use as a template.  I open this template, create a copy of the file, then duplicate the pre-built worksheet for every available product. 

    Everything works fine up to this point, however when I go to open my newly generated excel file, I get the following error:


    "Excel found unreadable content in '<My excel file name>'.  Do you want to recover its contents of this workbook?  If you trust the source of this workbook, click yes"



    After clicking "Yes", I get a 2nd dialog box:


    "Excel was able to open the file by repairing or removing unreadable content.  Repaired Records: Worksheet properties from /xl/workbook.xml part (Workbook)".


    The strange thing about this error is I'm able to successfully create up to 40 copies of a worksheet with no problem.  After 40 copies however, I receive this message.  Once I open my file, review the error message, and then manually resave the excel file, the error goes away.

    Here's my code for creating a copy of a worksheet.  I call this code from within a for loop:

    public void CopyWorkSheet(string newSheetName)
          //Add cloned sheet and all associated parts to workbook
          WorksheetPart clonedSheet = Workbook.AddPart((WorksheetPart) (Workbook.GetPartById(Sheet.Id)));
          //There should only be one sheet that has focus
          //Add new sheet to main workbook part
          Sheets sheets = Workbook.Workbook.GetFirstChild<Sheets>();
          Sheet copiedSheet = new Sheet
                        Name = newSheetName,  
                        Id = Workbook.GetIdOfPart(clonedSheet),
                        SheetId = (uint) sheets.ChildElements.Count + 1
          //Save Changes

    Any idea why this may be happening?


    Thursday, August 11, 2011 9:37 PM


  • I found and corrected my issue. 

    After converting my xlsx file to xml, I realized I had duplicate sheetId's within my worksheet.  My pre-built template worksheet had a sheetId of 41.  My code assumed this would have a sheetId of 1, so when creating copies of this worksheet template, it assigned sheetId's of 2, 3, 4, 5, etc.  When it hit sheetId=41, this caused the Id to no longer be unique, and thus the error.


    • Proposed as answer by Bruce Song Tuesday, August 16, 2011 10:54 AM
    • Marked as answer by Bruce Song Wednesday, August 17, 2011 3:47 AM
    Friday, August 12, 2011 3:36 PM