c# - Adding Spreadsheets with open XML SDK -


i'm attempting alter pre-existing office document inserting externally loaded image using open xml sdk 2.5. plan in excel appending worksheet workbook , doing of work there. however, can't seem around 1 error in particular.

my code is:

    public void insert(string filepath)     {         spreadsheetdocument doc = spreadsheetdocument.open(filepath, false);          // add worksheetpart workbookpart.         worksheetpart worksheetpart = doc.workbookpart.addnewpart<worksheetpart>();         worksheetpart.worksheet = new worksheet(new sheetdata());          // add sheets workbook.         sheets sheets = doc.workbookpart.workbook.             appendchild<sheets>(new sheets());          // append new worksheet , associate workbook.         sheet sheet = new sheet()         {             id = doc.workbookpart.             getidofpart(worksheetpart),             sheetid = 1,             name = "mysheet"         };         sheets.append(sheet);          // close document.         doc.close();     } 

line 6 (adding new worksheet part) throws io exception each time try test program. of code taken msdn. same site suggested if i'm getting io exception, may have input wrong filepath, not case. verified members i'm accessing indeed public, i'm bit confused. can tell me error may be? i'm hoping it's simple. first time using c#.

edit: should mention exception message states "cannot open read container", have verified file not read , excel not open.

you passing false iseditable argument when opening file.

spreadsheetdocument.open(filepath, false) 

this makes document read-only. if change true, should work better.

more information:


i first read file content memorystream, , open instead. reduce risk of file corruption.

var memory = new memorystream(); using (var file = file.openread(filepath)) {     file.copyto(memory); } memory.position = 0;  var doc = spreadsheetdocument.open(memory, true);  //...  doc.close();  file.writeallbytes(filepath, memory.toarray()); 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -