Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Table.cs 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using Novacode;
  7. using System.IO.Packaging;
  8. using System.IO;
  9. using System.Reflection;
  10. namespace Novacode
  11. {
  12. /// <summary>
  13. /// Designs\Styles that can be applied to a table.
  14. /// </summary>
  15. public enum TableDesign { TableNormal, TableGrid, LightShading, LightShadingAccent1, LightShadingAccent2, LightShadingAccent3, LightShadingAccent4, LightShadingAccent5, LightShadingAccent6, LightList, LightListAccent1, LightListAccent2, LightListAccent3, LightListAccent4, LightListAccent5, LightListAccent6, LightGrid, LightGridAccent1, LightGridAccent2, LightGridAccent3, LightGridAccent4, LightGridAccent5, LightGridAccent6, MediumShading1, MediumShading1Accent1, MediumShading1Accent2, MediumShading1Accent3, MediumShading1Accent4, MediumShading1Accent5, MediumShading1Accent6, MediumShading2, MediumShading2Accent1, MediumShading2Accent2, MediumShading2Accent3, MediumShading2Accent4, MediumShading2Accent5, MediumShading2Accent6, MediumList1, MediumList1Accent1, MediumList1Accent2, MediumList1Accent3, MediumList1Accent4, MediumList1Accent5, MediumList1Accent6, MediumList2, MediumList2Accent1, MediumList2Accent2, MediumList2Accent3, MediumList2Accent4, MediumList2Accent5, MediumList2Accent6, MediumGrid1, MediumGrid1Accent1, MediumGrid1Accent2, MediumGrid1Accent3, MediumGrid1Accent4, MediumGrid1Accent5, MediumGrid1Accent6, MediumGrid2, MediumGrid2Accent1, MediumGrid2Accent2, MediumGrid2Accent3, MediumGrid2Accent4, MediumGrid2Accent5, MediumGrid2Accent6, MediumGrid3, MediumGrid3Accent1, MediumGrid3Accent2, MediumGrid3Accent3, MediumGrid3Accent4, MediumGrid3Accent5, MediumGrid3Accent6, DarkList, DarkListAccent1, DarkListAccent2, DarkListAccent3, DarkListAccent4, DarkListAccent5, DarkListAccent6, ColorfulShading, ColorfulShadingAccent1, ColorfulShadingAccent2, ColorfulShadingAccent3, ColorfulShadingAccent4, ColorfulShadingAccent5, ColorfulShadingAccent6, ColorfulList, ColorfulListAccent1, ColorfulListAccent2, ColorfulListAccent3, ColorfulListAccent4, ColorfulListAccent5, ColorfulListAccent6, ColorfulGrid, ColorfulGridAccent1, ColorfulGridAccent2, ColorfulGridAccent3, ColorfulGridAccent4, ColorfulGridAccent5, ColorfulGridAccent6};
  16. /// <summary>
  17. /// Represents a Table in a document.
  18. /// </summary>
  19. public class Table
  20. {
  21. private List<Row> rows;
  22. private int rowCount, columnCount;
  23. internal XElement xml;
  24. /// <summary>
  25. /// Returns the number of rows in this table.
  26. /// </summary>
  27. public int RowCount { get { return rowCount; } }
  28. /// <summary>
  29. /// Returns the number of coloumns in this table.
  30. /// </summary>
  31. public int ColumnCount { get { return columnCount; } }
  32. /// <summary>
  33. /// Returns a list of rows in this table.
  34. /// </summary>
  35. public List<Row> Rows { get { return rows; } }
  36. DocX document;
  37. private TableDesign design;
  38. internal Table(DocX document, XElement xml)
  39. {
  40. this.xml = xml;
  41. this.document = document;
  42. XElement properties = xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  43. rows = (from r in xml.Elements(XName.Get("tr", DocX.w.NamespaceName))
  44. select new Row(document, r)).ToList();
  45. rowCount = rows.Count;
  46. if (rows.Count > 0)
  47. if (rows[0].Cells.Count > 0)
  48. columnCount = rows[0].Cells.Count;
  49. XElement style = properties.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
  50. XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
  51. design = (TableDesign)Enum.Parse(typeof(TableDesign), val.Value.Replace("-", string.Empty));
  52. }
  53. /// <summary>
  54. /// The design\style to apply to this table.
  55. /// </summary>
  56. public TableDesign Design
  57. {
  58. get { return design; }
  59. set
  60. {
  61. XElement tblPr = xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));
  62. XElement style = tblPr.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
  63. if (style == null)
  64. {
  65. tblPr.Add(new XElement(XName.Get("tblStyle", DocX.w.NamespaceName)));
  66. style = tblPr.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
  67. }
  68. XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
  69. if(val == null)
  70. {
  71. style.Add(new XAttribute(XName.Get("val", DocX.w.NamespaceName), ""));
  72. val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));
  73. }
  74. design = value;
  75. switch (design)
  76. {
  77. case TableDesign.TableNormal: val.Value = "TableNormal"; break;
  78. case TableDesign.TableGrid: val.Value = "TableGrid"; break;
  79. case TableDesign.LightShading: val.Value = "LightShading"; break;
  80. case TableDesign.LightShadingAccent1: val.Value = "LightShading-Accent1"; break;
  81. case TableDesign.LightShadingAccent2: val.Value = "LightShading-Accent2"; break;
  82. case TableDesign.LightShadingAccent3: val.Value = "LightShading-Accent3"; break;
  83. case TableDesign.LightShadingAccent4: val.Value = "LightShading-Accent4"; break;
  84. case TableDesign.LightShadingAccent5: val.Value = "LightShading-Accent5"; break;
  85. case TableDesign.LightShadingAccent6: val.Value = "LightShading-Accent6"; break;
  86. case TableDesign.LightList: val.Value = "LightList"; break;
  87. case TableDesign.LightListAccent1: val.Value = "LightList-Accent1"; break;
  88. case TableDesign.LightListAccent2: val.Value = "LightList-Accent2"; break;
  89. case TableDesign.LightListAccent3: val.Value = "LightList-Accent3"; break;
  90. case TableDesign.LightListAccent4: val.Value = "LightList-Accent4"; break;
  91. case TableDesign.LightListAccent5: val.Value = "LightList-Accent5"; break;
  92. case TableDesign.LightListAccent6: val.Value = "LightList-Accent6"; break;
  93. case TableDesign.LightGrid: val.Value = "LightGrid"; break;
  94. case TableDesign.LightGridAccent1: val.Value = "LightGrid-Accent1"; break;
  95. case TableDesign.LightGridAccent2: val.Value = "LightGrid-Accent2"; break;
  96. case TableDesign.LightGridAccent3: val.Value = "LightGrid-Accent3"; break;
  97. case TableDesign.LightGridAccent4: val.Value = "LightGrid-Accent4"; break;
  98. case TableDesign.LightGridAccent5: val.Value = "LightGrid-Accent5"; break;
  99. case TableDesign.LightGridAccent6: val.Value = "LightGrid-Accent6"; break;
  100. case TableDesign.MediumShading1: val.Value = "MediumShading1"; break;
  101. case TableDesign.MediumShading1Accent1: val.Value = "MediumShading1-Accent1"; break;
  102. case TableDesign.MediumShading1Accent2: val.Value = "MediumShading1-Accent2"; break;
  103. case TableDesign.MediumShading1Accent3: val.Value = "MediumShading1-Accent3"; break;
  104. case TableDesign.MediumShading1Accent4: val.Value = "MediumShading1-Accent4"; break;
  105. case TableDesign.MediumShading1Accent5: val.Value = "MediumShading1-Accent5"; break;
  106. case TableDesign.MediumShading1Accent6: val.Value = "MediumShading1-Accent6"; break;
  107. case TableDesign.MediumShading2: val.Value = "MediumShading2"; break;
  108. case TableDesign.MediumShading2Accent1: val.Value = "MediumShading2-Accent1"; break;
  109. case TableDesign.MediumShading2Accent2: val.Value = "MediumShading2-Accent2"; break;
  110. case TableDesign.MediumShading2Accent3: val.Value = "MediumShading2-Accent3"; break;
  111. case TableDesign.MediumShading2Accent4: val.Value = "MediumShading2-Accent4"; break;
  112. case TableDesign.MediumShading2Accent5: val.Value = "MediumShading2-Accent5"; break;
  113. case TableDesign.MediumShading2Accent6: val.Value = "MediumShading2-Accent6"; break;
  114. case TableDesign.MediumList1: val.Value = "MediumList1"; break;
  115. case TableDesign.MediumList1Accent1: val.Value = "MediumList1-Accent1"; break;
  116. case TableDesign.MediumList1Accent2: val.Value = "MediumList1-Accent2"; break;
  117. case TableDesign.MediumList1Accent3: val.Value = "MediumList1-Accent3"; break;
  118. case TableDesign.MediumList1Accent4: val.Value = "MediumList1-Accent4"; break;
  119. case TableDesign.MediumList1Accent5: val.Value = "MediumList1-Accent5"; break;
  120. case TableDesign.MediumList1Accent6: val.Value = "MediumList1-Accent6"; break;
  121. case TableDesign.MediumList2: val.Value = "MediumList2"; break;
  122. case TableDesign.MediumList2Accent1: val.Value = "MediumList2-Accent1"; break;
  123. case TableDesign.MediumList2Accent2: val.Value = "MediumList2-Accent2"; break;
  124. case TableDesign.MediumList2Accent3: val.Value = "MediumList2-Accent3"; break;
  125. case TableDesign.MediumList2Accent4: val.Value = "MediumList2-Accent4"; break;
  126. case TableDesign.MediumList2Accent5: val.Value = "MediumList2-Accent5"; break;
  127. case TableDesign.MediumList2Accent6: val.Value = "MediumList2-Accent6"; break;
  128. case TableDesign.MediumGrid1: val.Value = "MediumGrid1"; break;
  129. case TableDesign.MediumGrid1Accent1: val.Value = "MediumGrid1-Accent1"; break;
  130. case TableDesign.MediumGrid1Accent2: val.Value = "MediumGrid1-Accent2"; break;
  131. case TableDesign.MediumGrid1Accent3: val.Value = "MediumGrid1-Accent3"; break;
  132. case TableDesign.MediumGrid1Accent4: val.Value = "MediumGrid1-Accent4"; break;
  133. case TableDesign.MediumGrid1Accent5: val.Value = "MediumGrid1-Accent5"; break;
  134. case TableDesign.MediumGrid1Accent6: val.Value = "MediumGrid1-Accent6"; break;
  135. case TableDesign.MediumGrid2: val.Value = "MediumGrid2"; break;
  136. case TableDesign.MediumGrid2Accent1: val.Value = "MediumGrid2-Accent1"; break;
  137. case TableDesign.MediumGrid2Accent2: val.Value = "MediumGrid2-Accent2"; break;
  138. case TableDesign.MediumGrid2Accent3: val.Value = "MediumGrid2-Accent3"; break;
  139. case TableDesign.MediumGrid2Accent4: val.Value = "MediumGrid2-Accent4"; break;
  140. case TableDesign.MediumGrid2Accent5: val.Value = "MediumGrid2-Accent5"; break;
  141. case TableDesign.MediumGrid2Accent6: val.Value = "MediumGrid2-Accent6"; break;
  142. case TableDesign.MediumGrid3: val.Value = "MediumGrid3"; break;
  143. case TableDesign.MediumGrid3Accent1: val.Value = "MediumGrid3-Accent1"; break;
  144. case TableDesign.MediumGrid3Accent2: val.Value = "MediumGrid3-Accent2"; break;
  145. case TableDesign.MediumGrid3Accent3: val.Value = "MediumGrid3-Accent3"; break;
  146. case TableDesign.MediumGrid3Accent4: val.Value = "MediumGrid3-Accent4"; break;
  147. case TableDesign.MediumGrid3Accent5: val.Value = "MediumGrid3-Accent5"; break;
  148. case TableDesign.MediumGrid3Accent6: val.Value = "MediumGrid3-Accent6"; break;
  149. case TableDesign.DarkList: val.Value = "DarkList"; break;
  150. case TableDesign.DarkListAccent1: val.Value = "DarkList-Accent1"; break;
  151. case TableDesign.DarkListAccent2: val.Value = "DarkList-Accent2"; break;
  152. case TableDesign.DarkListAccent3: val.Value = "DarkList-Accent3"; break;
  153. case TableDesign.DarkListAccent4: val.Value = "DarkList-Accent4"; break;
  154. case TableDesign.DarkListAccent5: val.Value = "DarkList-Accent5"; break;
  155. case TableDesign.DarkListAccent6: val.Value = "DarkList-Accent6"; break;
  156. case TableDesign.ColorfulShading: val.Value = "ColorfulShading"; break;
  157. case TableDesign.ColorfulShadingAccent1: val.Value = "ColorfulShading-Accent1"; break;
  158. case TableDesign.ColorfulShadingAccent2: val.Value = "ColorfulShading-Accent2"; break;
  159. case TableDesign.ColorfulShadingAccent3: val.Value = "ColorfulShading-Accent3"; break;
  160. case TableDesign.ColorfulShadingAccent4: val.Value = "ColorfulShading-Accent4"; break;
  161. case TableDesign.ColorfulShadingAccent5: val.Value = "ColorfulShading-Accent5"; break;
  162. case TableDesign.ColorfulShadingAccent6: val.Value = "ColorfulShading-Accent6"; break;
  163. case TableDesign.ColorfulList: val.Value = "ColorfulList"; break;
  164. case TableDesign.ColorfulListAccent1: val.Value = "ColorfulList-Accent1"; break;
  165. case TableDesign.ColorfulListAccent2: val.Value = "ColorfulList-Accent2"; break;
  166. case TableDesign.ColorfulListAccent3: val.Value = "ColorfulList-Accent3"; break;
  167. case TableDesign.ColorfulListAccent4: val.Value = "ColorfulList-Accent4"; break;
  168. case TableDesign.ColorfulListAccent5: val.Value = "ColorfulList-Accent5"; break;
  169. case TableDesign.ColorfulListAccent6: val.Value = "ColorfulList-Accent6"; break;
  170. case TableDesign.ColorfulGrid: val.Value = "ColorfulGrid"; break;
  171. case TableDesign.ColorfulGridAccent1: val.Value = "ColorfulGrid-Accent1"; break;
  172. case TableDesign.ColorfulGridAccent2: val.Value = "ColorfulGrid-Accent2"; break;
  173. case TableDesign.ColorfulGridAccent3: val.Value = "ColorfulGrid-Accent3"; break;
  174. case TableDesign.ColorfulGridAccent4: val.Value = "ColorfulGrid-Accent4"; break;
  175. case TableDesign.ColorfulGridAccent5: val.Value = "ColorfulGrid-Accent5"; break;
  176. case TableDesign.ColorfulGridAccent6: val.Value = "ColorfulGrid-Accent6"; break;
  177. default: break;
  178. }
  179. XDocument style_doc;
  180. PackagePart word_styles = document.package.GetPart(new Uri("/word/styles.xml", UriKind.Relative));
  181. using (TextReader tr = new StreamReader(word_styles.GetStream()))
  182. style_doc = XDocument.Load(tr);
  183. var tableStyle =
  184. (
  185. from e in style_doc.Descendants()
  186. let styleId = e.Attribute(XName.Get("styleId", DocX.w.NamespaceName))
  187. where (styleId != null && styleId.Value == val.Value)
  188. select e
  189. ).FirstOrDefault();
  190. if (tableStyle == null)
  191. {
  192. Assembly _assembly = Assembly.GetExecutingAssembly();
  193. TextReader _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("Novacode.styles.xml"));
  194. XDocument external_style_doc = XDocument.Load(_textStreamReader);
  195. var styleElement =
  196. (
  197. from e in external_style_doc.Descendants()
  198. let styleId = e.Attribute(XName.Get("styleId", DocX.w.NamespaceName))
  199. where (styleId != null && styleId.Value == val.Value)
  200. select e
  201. ).First();
  202. style_doc.Element(XName.Get("styles", DocX.w.NamespaceName)).Add(styleElement);
  203. using (TextWriter tw = new StreamWriter(word_styles.GetStream(FileMode.Create)))
  204. style_doc.Save(tw, SaveOptions.None);
  205. }
  206. }
  207. }
  208. /// <summary>
  209. /// Insert a row at the end of this table.
  210. /// </summary>
  211. /// <example>
  212. /// <code>
  213. /// // Load a document.
  214. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  215. /// {
  216. /// // Get the first table in this document.
  217. /// Table table = document.Tables[0];
  218. ///
  219. /// // Insert a new row at the end of this table.
  220. /// Row row = table.InsertRow();
  221. ///
  222. /// // Loop through each cell in this new row.
  223. /// foreach (Cell c in row.Cells)
  224. /// {
  225. /// // Set the text of each new cell to "Hello".
  226. /// c.Paragraph.InsertText("Hello", false);
  227. /// }
  228. ///
  229. /// // Save the document to a new file.
  230. /// document.SaveAs(@"C:\Example\Test2.docx");
  231. /// }// Release this document from memory.
  232. /// </code>
  233. /// </example>
  234. /// <returns>A new row.</returns>
  235. public Row InsertRow()
  236. {
  237. return InsertRow(rows.Count);
  238. }
  239. /// <summary>
  240. /// Insert a column to the right of a Table.
  241. /// </summary>
  242. /// <example>
  243. /// <code>
  244. /// // Load a document.
  245. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  246. /// {
  247. /// // Get the first Table in this document.
  248. /// Table table = document.Tables[0];
  249. ///
  250. /// // Insert a new column to this right of this table.
  251. /// table.InsertColumn();
  252. ///
  253. /// // Set the new coloumns text to "Row no."
  254. /// table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
  255. ///
  256. /// // Loop through each row in the table.
  257. /// for (int i = 1; i &lt; table.Rows.Count; i++)
  258. /// {
  259. /// // The current row.
  260. /// Row row = table.Rows[i];
  261. ///
  262. /// // The cell in this row that belongs to the new coloumn.
  263. /// Cell cell = row.Cells[table.ColumnCount - 1];
  264. ///
  265. /// // The Paragraph that this cell houses.
  266. /// Paragraph p = cell.Paragraph;
  267. ///
  268. /// // Insert this rows index.
  269. /// p.InsertText(i.ToString(), false);
  270. /// }
  271. ///
  272. /// document.Save();
  273. /// }// Release this document from memory.
  274. /// </code>
  275. /// </example>
  276. public void InsertColumn()
  277. {
  278. InsertColumn(columnCount);
  279. }
  280. /// <summary>
  281. /// Remove the last row from this Table.
  282. /// </summary>
  283. /// <example>
  284. /// Remove the last row from a Table.
  285. /// <code>
  286. /// // Load a document.
  287. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  288. /// {
  289. /// // Get the first table in this document.
  290. /// Table table = document.Tables[0];
  291. ///
  292. /// // Remove the last row from this table.
  293. /// table.RemoveRow();
  294. ///
  295. /// // Save the document.
  296. /// document.Save();
  297. /// }// Release this document from memory.
  298. /// </code>
  299. /// </example>
  300. public void RemoveRow()
  301. {
  302. RemoveRow(rowCount - 1);
  303. }
  304. /// <summary>
  305. /// Remove a row from this Table.
  306. /// </summary>
  307. /// <param name="index">The row to remove.</param>
  308. /// <example>
  309. /// Remove the first row from a Table.
  310. /// <code>
  311. /// // Load a document.
  312. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  313. /// {
  314. /// // Get the first table in this document.
  315. /// Table table = document.Tables[0];
  316. ///
  317. /// // Remove the first row from this table.
  318. /// table.RemoveRow(0);
  319. ///
  320. /// // Save the document.
  321. /// document.Save();
  322. /// }// Release this document from memory.
  323. /// </code>
  324. /// </example>
  325. public void RemoveRow(int index)
  326. {
  327. if (index < 0 || index > rows.Count)
  328. throw new IndexOutOfRangeException();
  329. rows[index].xml.Remove();
  330. }
  331. /// <summary>
  332. /// Remove the last column for this Table.
  333. /// </summary>
  334. /// <example>
  335. /// Remove the last column from a Table.
  336. /// <code>
  337. /// // Load a document.
  338. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  339. /// {
  340. /// // Get the first table in this document.
  341. /// Table table = document.Tables[0];
  342. ///
  343. /// // Remove the last column from this table.
  344. /// table.RemoveColumn();
  345. ///
  346. /// // Save the document.
  347. /// document.Save();
  348. /// }// Release this document from memory.
  349. /// </code>
  350. /// </example>
  351. public void RemoveColumn()
  352. {
  353. RemoveColumn(columnCount - 1);
  354. }
  355. /// <summary>
  356. /// Remove a coloumn from this Table.
  357. /// </summary>
  358. /// <param name="index">The coloumn to remove.</param>
  359. /// <example>
  360. /// Remove the first column from a Table.
  361. /// <code>
  362. /// // Load a document.
  363. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  364. /// {
  365. /// // Get the first table in this document.
  366. /// Table table = document.Tables[0];
  367. ///
  368. /// // Remove the first column from this table.
  369. /// table.RemoveColumn(0);
  370. ///
  371. /// // Save the document.
  372. /// document.Save();
  373. /// }// Release this document from memory.
  374. /// </code>
  375. /// </example>
  376. public void RemoveColumn(int index)
  377. {
  378. if (index < 0 || index > columnCount - 1)
  379. throw new IndexOutOfRangeException();
  380. foreach (Row r in rows)
  381. r.Cells[index].xml.Remove();
  382. }
  383. /// <summary>
  384. /// Insert a row into this table.
  385. /// </summary>
  386. /// <example>
  387. /// <code>
  388. /// // Load a document.
  389. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  390. /// {
  391. /// // Get the first table in this document.
  392. /// Table table = document.Tables[0];
  393. ///
  394. /// // Insert a new row at index 1 in this table.
  395. /// Row row = table.InsertRow(1);
  396. ///
  397. /// // Loop through each cell in this new row.
  398. /// foreach (Cell c in row.Cells)
  399. /// {
  400. /// // Set the text of each new cell to "Hello".
  401. /// c.Paragraph.InsertText("Hello", false);
  402. /// }
  403. ///
  404. /// // Save the document to a new file.
  405. /// document.SaveAs(@"C:\Example\Test2.docx");
  406. /// }// Release this document from memory.
  407. /// </code>
  408. /// </example>
  409. /// <param name="index">Index to insert row at.</param>
  410. /// <returns>A new Row</returns>
  411. public Row InsertRow(int index)
  412. {
  413. if (index < 0 || index > rows.Count)
  414. throw new IndexOutOfRangeException();
  415. List<XElement> content = new List<XElement>();
  416. foreach (Cell c in rows[0].Cells)
  417. content.Add(new XElement(XName.Get("tc", DocX.w.NamespaceName), new XElement(XName.Get("p", DocX.w.NamespaceName))));
  418. XElement e = new XElement(XName.Get("tr", DocX.w.NamespaceName), content);
  419. Row newRow = new Row(document, e);
  420. XElement rowXml;
  421. if (index == rows.Count)
  422. {
  423. rowXml = rows.Last().xml;
  424. rowXml.AddAfterSelf(newRow.xml);
  425. }
  426. else
  427. {
  428. rowXml = rows[index].xml;
  429. rowXml.AddBeforeSelf(newRow.xml);
  430. }
  431. rows.Insert(index, newRow);
  432. rowCount = rows.Count;
  433. return newRow;
  434. }
  435. /// <summary>
  436. /// Insert a column into a table.
  437. /// </summary>
  438. /// <param name="index">The index to insert the column at.</param>
  439. /// <example>
  440. /// Insert a column to the left of a table.
  441. /// <code>
  442. /// // Load a document.
  443. /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
  444. /// {
  445. /// // Get the first Table in this document.
  446. /// Table table = document.Tables[0];
  447. ///
  448. /// // Insert a new column to this left of this table.
  449. /// table.InsertColumn(0);
  450. ///
  451. /// // Set the new coloumns text to "Row no."
  452. /// table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
  453. ///
  454. /// // Loop through each row in the table.
  455. /// for (int i = 1; i &lt; table.Rows.Count; i++)
  456. /// {
  457. /// // The current row.
  458. /// Row row = table.Rows[i];
  459. ///
  460. /// // The cell in this row that belongs to the new coloumn.
  461. /// Cell cell = row.Cells[table.ColumnCount - 1];
  462. ///
  463. /// // The Paragraph that this cell houses.
  464. /// Paragraph p = cell.Paragraph;
  465. ///
  466. /// // Insert this rows index.
  467. /// p.InsertText(i.ToString(), false);
  468. /// }
  469. ///
  470. /// document.Save();
  471. /// }// Release this document from memory.
  472. /// </code>
  473. /// </example>
  474. public void InsertColumn(int index)
  475. {
  476. if (rows.Count > 0)
  477. {
  478. foreach (Row r in rows)
  479. {
  480. if(columnCount == index)
  481. r.Cells[index - 1].xml.AddAfterSelf(new XElement(XName.Get("tc", DocX.w.NamespaceName), new XElement(XName.Get("p", DocX.w.NamespaceName))));
  482. else
  483. r.Cells[index].xml.AddBeforeSelf(new XElement(XName.Get("tc", DocX.w.NamespaceName), new XElement(XName.Get("p", DocX.w.NamespaceName))));
  484. }
  485. rows = (from r in xml.Elements(XName.Get("tr", DocX.w.NamespaceName))
  486. select new Row(document, r)).ToList();
  487. rowCount = rows.Count;
  488. if (rows.Count > 0)
  489. if (rows[0].Cells.Count > 0)
  490. columnCount = rows[0].Cells.Count;
  491. }
  492. }
  493. }
  494. /// <summary>
  495. /// Represents a single row in a Table.
  496. /// </summary>
  497. public class Row
  498. {
  499. DocX document;
  500. internal XElement xml;
  501. private List<Cell> cells;
  502. /// <summary>
  503. /// A list of Cells in this Row.
  504. /// </summary>
  505. public List<Cell> Cells { get { return cells; } }
  506. internal Row(DocX document, XElement xml)
  507. {
  508. this.document = document;
  509. this.xml = xml;
  510. cells = (from c in xml.Elements(XName.Get("tc", DocX.w.NamespaceName))
  511. select new Cell(document, c)).ToList();
  512. }
  513. }
  514. public class Cell
  515. {
  516. private Paragraph p;
  517. private DocX document;
  518. internal XElement xml;
  519. public Paragraph Paragraph
  520. {
  521. get { return p; }
  522. set { p = value; }
  523. }
  524. internal Cell(DocX document, XElement xml)
  525. {
  526. this.document = document;
  527. this.xml = xml;
  528. XElement properties = xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
  529. p = new Paragraph(document, 0, xml.Element(XName.Get("p", DocX.w.NamespaceName)));
  530. }
  531. }
  532. }