Error while opening a file with multiple worksheets

Sep 28, 2010 at 11:07 AM


I have created a Excel 2007 template workbook that has two sheets named Rates and Frequency. In my program I create a Excel 2007 file from the template and then proceed to work on the file.

My code snippet is as follows:


strfiledest = "Frequency_UI_NDPL_" + abtsUtil.FormatDate(txtdate.Text) + "test.xlsx"; 

string strfilesource = "TemplateSheet.xlsx";





FileInfo TheFile = new FileInfo(strCurrentDir + strfilesource); 

if (TheFile.Exists) 


File.Copy(strCurrentDir + strfilesource, strCurrentDir + strfiledest, true); 




throw new FileNotFoundException(); 



catch (FileNotFoundException ex) 



catch (Exception ex) 



using (var package = SpreadsheetDocumentWrapper.Open(strCurrentDir + strfiledest)) 


//Making of UI Data Sheet 

var part1 = package.WorkbookPart.WorksheetParts["UI DATA"]; 

var part2 = package.WorkbookPart.WorksheetParts["FREQ AND UI RATE"]; 


//Put UI data in Excel Sheet. Also populate some data in Freq & UI Rate Sheet 

//populating For and Run Dates in UI Data 

string datetime = DateTime.Now.ToShortDateString(); 
part1.Worksheet.SetCellValue(2, 2, txtdate.Text);

part1.Worksheet.SetCellValue(2, 8,


Now the workbook gets generated without any issue, but when I try to open the workbook, I get an error stating
Excel found unreadable content in the <file name>. Do you want to recover the contents of the workbook? If you trust the source Click Yes.
When I click on Yes, the workbook opens without the second sheet.
What is the wrong step that I am taking to open this workbook?
Sep 28, 2010 at 11:40 AM

Your code looks OK, although it is difficult to be sure without knowing exactly what your utility methods are doing. You appear to be using an old (deprecated) overload of the SetCellValue() method, although this would not be the cause of the problem you have reported.

The error that you are seeing tells you that Excel has found something in the processed output file that is not OpenXML compliant. It is not possible to diagnose the exact cause of your problem using this information alone - you need to examine the output file and determine exactly where it is deficient. You could do this using the OpenXML SDK Tool, which includes a validator utility that will pinpoint the location of the bad data and should give you a clue to why it is occuring. If the validator doesn't find the bad data (which sometimes happens) you can find it manually using the comparison utility in the same Tool. By comparing your processed file to a copy of your original template file, you will see exactly what has changed when your code executed.

Although your code is very simple, you may wish to simplify it further by commenting out lines that write to the package until the data corruption no longer occurs. This should tell you the line that is causing the problem and may help me to advise you futher.

Good luck,