c# - How to add style to an exisiting table in docx using openXML SDK2.0 -


i working on project, involves creating data sheet. have managed create tables in docx file following code provided msdn, when try apply style (eg. border set none, shading, cell width, etc.) table created, wont append on it.

so assume have use table.removeallchildren append new style. if so, entire table removed , have re-create table. there must better way of doing it. please advise.

the following sample code iihave, 1 cell table. shaded dark-blue, trying remove table border, set cell width , modify text.

  public void docheadertableval(string filelocation, string replacingval, int tablenum, int rownum, int cellnum) //replace value     {         try         {             using (var doc = wordprocessingdocument.open(filelocation, true))             {                 // find first table in document.                 table table =                     doc.maindocumentpart.document.body.elements<table>().elementat(tablenum);                   tableproperties props = new tableproperties(                     new tableborders(                         new topborder                         {                             val = new enumvalue<bordervalues>(bordervalues.none),                             //size = 10                         },                         new bottomborder                         {                             val = new enumvalue<bordervalues>(bordervalues.none),                             //size = 10                         },                         new leftborder                         {                             val = new enumvalue<bordervalues>(bordervalues.none),                             //size = 10                         },                         new rightborder                         {                             val = new enumvalue<bordervalues>(bordervalues.none),                             //size = 10                         },                         new insidehorizontalborder                         {                             val = new enumvalue<bordervalues>(bordervalues.none),                             //size = 10                         },                         new insideverticalborder                         {                             val = new enumvalue<bordervalues>(bordervalues.none),                             //size = 10                          }                         )                     );                   table.appendchild<tableproperties>(props);                  tablerow row = table.elements<tablerow>().elementat(rownum);                   tablecell cell = row.elements<tablecell>().elementat(cellnum);                  cell.removeallchildren();                  //cell.tablecellproperties.tablecellwidth.width = "2500";                    //finding first run , first text replace passed-in text.                 paragraph p = new paragraph() { rsidparagraphaddition = "004f7104", rsidparagraphproperties = "008f2986", rsidrunadditiondefault = "004f7104" };                 paragraphproperties ppr = new paragraphproperties();                 justification just1 = new justification() { val = justificationvalues.center };                 ppr.append(just1);                 run r = new run();                 runproperties rprop = new runproperties(); //making instance of run property                 runfonts rfont = new runfonts();                 fontsize size = new fontsize();                 color color = new color() { val = "#ffffff"};                 bold bld = new bold();                 italic italic = new italic();                 rfont.ascii = "arial black";                  size.val = new stringvalue("32");                 rprop.append(rfont, size, color, bld, italic);                 r.prependchild<runproperties>(rprop);                   text t = new text(replacingval);                 r.append(t);                 p.append(ppr);                 p.append(r);                   //back ground shading title cell;                  cell.appendchild(new tablecellproperties(new shading { val = shadingpatternvalues.clear, color = "auto", fill = "##17365d" }));                  cell.append(p);               }         }         catch         {             messagebox.show("can not replace table value due file open error.");         }  } 


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -