I'm attempting to alter a pre-existing office document by inserting an externally loaded image using open XML SDK 2.5. I plan to do this in excel by appending a worksheet that to the workbook and then doing all of the work there. However, I can't seem to get around one error in particular.
My code is:
public void insert(String filepath)
{
SpreadsheetDocument doc = SpreadsheetDocument.Open(filepath, false);
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = doc.WorkbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = doc.WorkbookPart.Workbook.
AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
Id = doc.WorkbookPart.
GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "mySheet"
};
sheets.Append(sheet);
// Close the document.
doc.Close();
}
Line 6 (adding a new worksheet part) throws an IO exception each time I try to test the program. Most of this code was taken from the MSDN. The very same site suggested that if I'm getting an IO exception, I may have input the wrong filepath, but that is definitely not the case. I also verified that the members I'm accessing are indeed public, so I'm a bit confused. Can anyone tell me what this error may be? I'm hoping it's something simple. This is my first time using C#.
EDIT: I should mention the exception message also states "cannot open a read only container", but I have verified that the file is NOT read only and excel is NOT open.