OpenXML sdk Modify a sheet in my Excel document -


i create empty template in excel. open template , edit document not know how change existing sheet. that's code:

 using (spreadsheetdocument xl = spreadsheetdocument.open(filename, true))  {  workbookpart wbp = xl.workbookpart;  workbookpart workbook = xl.workbookpart;  // worksheet required name.  // used match id required sheet data  // because sheet class , sheetdata class aren't  // linked each other directly.  sheet s = null;  if (wbp.workbook.sheets.elements().count(nm => nm.name == sheetname) == 0)  {  // no such sheet name  xl.close();  return;  }  else  {  s = (sheet)wbp.workbook.sheets.elements().where(nm => nm.name == sheetname).first();  } 

worksheetpart wsp = (worksheetpart)xl.workbookpart.getpartbyid(s.id.value); worksheet worksheet = new worksheet(); sheetdata sd = new sheetdata(); //sheetdata sd = (sheetdata)wsp.worksheet.getfirstchild(); stylesheet stylesheet = workbook.workbookstylespart.stylesheet; //sheetdata sheetdata = new sheetdata(); //build formatted header style uint32value headerfontindex = util.createfont( stylesheet, "arial", 10, true, system.drawing.color.red);

//build formatted date style uint32value datefontindex = util.createfont( stylesheet, "arial", 8, true, system.drawing.color.black);

//set background color style uint32value headerfillindex = util.createfill( stylesheet, system.drawing.color.black);

//create cell style combining font/background uint32value headerstyleindex = util.createcellformat( stylesheet, headerfontindex, headerfillindex, null); /* * create set of basic cell styles specific formats... * if controlling table can create styles need, * set of code still intended generic. */ _numberstyleid = util.createcellformat(stylesheet, null, null, uint32value.fromuint32(3)); _doublestyleid = util.createcellformat(stylesheet, null, null, uint32value.fromuint32(4)); _datestyleid = util.createcellformat(stylesheet, null, null, uint32value.fromuint32(14)); _textstyleid = util.createcellformat(stylesheet, headerfontindex, headerfillindex, null); _percentagestyleid = util.createcellformat(stylesheet, null, null, uint32value.fromuint32(9));

util.addnumber(xl, sheetname, (uint32)3, "e", "27", _numberstyleid); util.addnumber(xl, sheetname, (uint32)3, "f", "3.6", _doublestyleid); util.addnumber(xl, sheetname, (uint32)5, "l", "5", _percentagestyleid); util.addtext(xl, sheetname, (uint32)5, "m", "dario", _textstyleid); util.adddate(xl, sheetname, (uint32)3, "j", datetime.now, _datestyleid); util.addimage(xl, sheetname, imagepath, "smile", "smile", 30, 30); util.mergecells(xl, sheetname, "d12", "f12"); //util.deletevaluecell(spreadsheet, sheetname, "f", (uint32)8);

txtcelltext.text = util.getcellvalue(xl, sheetname, (uint32)5, "m");

double number = util.getcelldoublevalue(xl, sheetname, (uint32)3, "e"); double numberd = util.getcelldoublevalue(xl, sheetname, (uint32)3, "f"); datetime datee = util.getcelldatetimevalue(xl, sheetname, (uint32)3, "j");

//txtdoublecell.text = util.getcellvalue(spreadsheet, sheetname, (uint32)3, "p"); txtpercentualecell.text = util.getcellvalue(xl, sheetname, (uint32)5, "l");

string date = util.getcellvalue(xl, sheetname, (uint32)3, "j"); double dated = convert.todouble(date); datetime datetime = datetime.fromoadate(dated); txtdatecell.text = datetime.toshortdatestring();

//worksheet.append(sd); /* columns columns = new columns(); columns.append(util.createcolumndata(10, 10, 40));

worksheet.append(columns); */ sheetprotection sheetprotection1 = new sheetprotection() { sheet = true, objects = true, scenarios = true, selectlockedcells = true, selectunlockedcells = true }; worksheet.append(sheetprotection1); wsp.worksheet = worksheet; wsp.worksheet.save();

xl.workbookpart.workbook.save(); xl.close();

thanks!

update

i try doesn't work.i'm using yet method (getworksheetpart() ) want existing worksheet, edit , save new document modified.

 using (spreadsheetdocument xl = spreadsheetdocument.open(filename, true)) {      workbookpart wbp = xl.workbookpart;

worksheetpart worksheetpart = util.getworksheetpart(wbp, sheetname); sheetprotection sheetprotection1 = new sheetprotection() { sheet = true, objects = true, scenarios = true, selectlockedcells = true, selectunlockedcells = true }; worksheetpart.worksheet.append(sheetprotection1); worksheetpart.worksheet.save(); </pre></code>

i build document corrupt. why?

to existing worksheet edit use code part:

        public static worksheetpart getworksheetpart(workbookpart workbookpart, string sheetname)         {             string relid = workbookpart.workbook.descendants<sheet>()                                  .where(s => sheetname.equals(s.name))                                  .first()                                  .id;              return (worksheetpart)workbookpart.getpartbyid(relid);         } 

just make sure sheetname exists searching or exceptions. use reference worksheetpart perform edits want , @ end call worksheetpart.worksheet.save();

edit

the sheetprotection element needs inserted after sheetdata element. try line when appending: worksheetpart.worksheet.descendants<sheetdata>().first().insertafterself(sheetprotection1);


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -