ImportTable.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. using FastReport.Matrix;
  2. using FastReport.Table;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Xml;
  6. namespace FastReport.Import.RDL
  7. {
  8. // Represents the RDL tables import
  9. public partial class RDLImport
  10. {
  11. private void LoadTableColumn(XmlNode tableColumnNode, TableColumn column)
  12. {
  13. XmlNodeList nodeList = tableColumnNode.ChildNodes;
  14. foreach (XmlNode node in nodeList)
  15. {
  16. if (node.Name == "Width")
  17. {
  18. column.Width = UnitsConverter.SizeToPixels(node.InnerText);
  19. }
  20. else if (node.Name == "Visibility")
  21. {
  22. LoadVisibility(node);
  23. }
  24. }
  25. }
  26. private void LoadTableColumns(XmlNode tableColumnsNode)
  27. {
  28. if (tableColumnsNode != null)
  29. {
  30. XmlNodeList nodeList = tableColumnsNode.ChildNodes;
  31. foreach (XmlNode node in nodeList)
  32. {
  33. if (node.Name == "TableColumn" || node.Name == "TablixColumn")
  34. {
  35. if (component is TableObject)
  36. {
  37. TableColumn column = new TableColumn();
  38. (component as TableObject).Columns.Add(column);
  39. LoadTableColumn(node, column);
  40. }
  41. }
  42. }
  43. }
  44. }
  45. private void LoadTableCell(XmlNode tableCellNode, ref int col)
  46. {
  47. int row = (component as TableObject).RowCount - 1;
  48. XmlNodeList nodeList = tableCellNode.ChildNodes;
  49. foreach (XmlNode node in nodeList)
  50. {
  51. if (node.Name == "ReportItems" || node.Name == "CellContents")
  52. {
  53. Base tempParent = parent;
  54. ComponentBase tempComponent = component;
  55. parent = (component as TableObject).GetCellData(col, row).Cell;
  56. LoadReportItems(node);
  57. component = tempComponent;
  58. parent = tempParent;
  59. }
  60. else if (node.Name == "ColSpan")
  61. {
  62. int colSpan = Convert.ToInt32(node.InnerText);
  63. (component as TableObject).GetCellData(col, row).Cell.ColSpan = colSpan;
  64. col += colSpan - 1;
  65. }
  66. }
  67. }
  68. private void LoadTableCells(XmlNode tableCellsNode)
  69. {
  70. int col = 0;
  71. XmlNodeList nodeList = tableCellsNode.ChildNodes;
  72. foreach (XmlNode node in nodeList)
  73. {
  74. if (node.Name == "TableCell" || node.Name == "TablixCell")
  75. {
  76. LoadTableCell(node, ref col);
  77. col++;
  78. }
  79. }
  80. }
  81. private void LoadTableRow(XmlNode tableRowNode, TableRow row)
  82. {
  83. XmlNodeList nodeList = tableRowNode.ChildNodes;
  84. foreach (XmlNode node in nodeList)
  85. {
  86. if (node.Name == "TableCells" || node.Name == "TablixCells")
  87. {
  88. LoadTableCells(node);
  89. }
  90. else if (node.Name == "Height")
  91. {
  92. row.Height = UnitsConverter.SizeToPixels(node.InnerText);
  93. }
  94. else if (node.Name == "Visibility")
  95. {
  96. LoadVisibility(node);
  97. }
  98. }
  99. }
  100. private void LoadTableRows(XmlNode tableRowsNode)
  101. {
  102. XmlNodeList nodeList = tableRowsNode.ChildNodes;
  103. foreach (XmlNode node in nodeList)
  104. {
  105. if (node.Name == "TableRow" || node.Name == "TablixRow")
  106. {
  107. if (component is TableObject)
  108. {
  109. TableRow row = new TableRow();
  110. (component as TableObject).Rows.Add(row);
  111. LoadTableRow(node, row);
  112. }
  113. }
  114. }
  115. }
  116. private void LoadHeader(XmlNode headerNode)
  117. {
  118. if (headerNode != null)
  119. {
  120. XmlNodeList nodeList = headerNode.ChildNodes;
  121. foreach (XmlNode node in nodeList)
  122. {
  123. if (node.Name == "TableRows" || node.Name == "TablixRows")
  124. {
  125. LoadTableRows(node);
  126. }
  127. }
  128. }
  129. }
  130. private void LoadTableGroup(XmlNode tableGroupNode)
  131. {
  132. XmlNodeList nodeList = tableGroupNode.ChildNodes;
  133. foreach (XmlNode node in nodeList)
  134. {
  135. if (node.Name == "Header")
  136. {
  137. LoadHeader(node);
  138. }
  139. else if (node.Name == "Footer")
  140. {
  141. LoadFooter(node);
  142. }
  143. else if (node.Name == "Visibility")
  144. {
  145. LoadVisibility(node);
  146. }
  147. }
  148. }
  149. private void LoadTableGroups(XmlNode tableGroupsNode)
  150. {
  151. XmlNodeList nodeList = tableGroupsNode.ChildNodes;
  152. foreach (XmlNode node in nodeList)
  153. {
  154. if (node.Name == "TableGroup")
  155. {
  156. LoadTableGroup(node);
  157. }
  158. }
  159. }
  160. private void LoadDetails(XmlNode detailsNode)
  161. {
  162. if (detailsNode != null)
  163. {
  164. XmlNodeList nodeList = detailsNode.ChildNodes;
  165. foreach (XmlNode node in nodeList)
  166. {
  167. if (node.Name == "TableRows")
  168. {
  169. LoadTableRows(node);
  170. }
  171. else if (node.Name == "Visibility")
  172. {
  173. LoadVisibility(node);
  174. }
  175. }
  176. }
  177. }
  178. private void LoadFooter(XmlNode footerNode)
  179. {
  180. if (footerNode != null)
  181. {
  182. XmlNodeList nodeList = footerNode.ChildNodes;
  183. foreach (XmlNode node in nodeList)
  184. {
  185. if (node.Name == "TableRows")
  186. {
  187. LoadTableRows(node);
  188. }
  189. }
  190. }
  191. }
  192. private void LoadCorner(XmlNode cornerNode)
  193. {
  194. if (cornerNode != null)
  195. {
  196. XmlNodeList nodeList = cornerNode.ChildNodes;
  197. foreach (XmlNode node in nodeList)
  198. {
  199. if (node.Name == "ReportItems")
  200. {
  201. //LoadReportItems(node);
  202. }
  203. }
  204. }
  205. }
  206. private void LoadDynamicColumns(XmlNode dynamicColumnsNode, List<XmlNode> dynamicColumns)
  207. {
  208. XmlNodeList nodeList = dynamicColumnsNode.ChildNodes;
  209. foreach (XmlNode node in nodeList)
  210. {
  211. if (node.Name == "Subtotal")
  212. {
  213. XmlNodeList subtotalNodeList = node.ChildNodes;
  214. foreach (XmlNode subtotalNode in subtotalNodeList)
  215. {
  216. if (subtotalNode.Name == "ReportItems")
  217. {
  218. dynamicColumns.Add(subtotalNode.Clone());
  219. }
  220. }
  221. }
  222. else if (node.Name == "ReportItems")
  223. {
  224. dynamicColumns.Add(node.Clone());
  225. }
  226. else if (node.Name == "Visibility")
  227. {
  228. LoadVisibility(node);
  229. }
  230. }
  231. }
  232. private XmlNode LoadStaticColumn(XmlNode staticColumnNode)
  233. {
  234. XmlNode staticColumn = null;
  235. XmlNodeList nodeList = staticColumnNode.ChildNodes;
  236. foreach (XmlNode node in nodeList)
  237. {
  238. if (node.Name == "ReportItems")
  239. {
  240. staticColumn = node.Clone();
  241. }
  242. }
  243. return staticColumn;
  244. }
  245. private void LoadStaticColumns(XmlNode staticColumnsNode, List<XmlNode> staticColumns)
  246. {
  247. XmlNodeList nodeList = staticColumnsNode.ChildNodes;
  248. foreach (XmlNode node in nodeList)
  249. {
  250. if (node.Name == "StaticColumn")
  251. {
  252. staticColumns.Add(LoadStaticColumn(node));
  253. }
  254. }
  255. }
  256. private float LoadColumnGrouping(XmlNode columnGroupingNode, List<XmlNode> dynamicColumns, List<XmlNode> staticColumns)
  257. {
  258. float cornerHeight = 0.8f * Utils.Units.Centimeters;
  259. XmlNodeList nodeList = columnGroupingNode.ChildNodes;
  260. foreach (XmlNode node in nodeList)
  261. {
  262. if (node.Name == "Height")
  263. {
  264. cornerHeight = UnitsConverter.SizeToPixels(node.InnerText);
  265. }
  266. else if (node.Name == "DynamicColumns")
  267. {
  268. LoadDynamicColumns(node, dynamicColumns);
  269. }
  270. else if (node.Name == "StaticColumns")
  271. {
  272. LoadStaticColumns(node, staticColumns);
  273. }
  274. }
  275. return cornerHeight;
  276. }
  277. private float LoadColumnGroupings(XmlNode columnGroupingsNode, List<XmlNode> dynamicColumns, List<XmlNode> staticColumns)
  278. {
  279. float cornerHeight = 0.8f * Utils.Units.Centimeters;
  280. if (columnGroupingsNode != null)
  281. {
  282. XmlNodeList nodeList = columnGroupingsNode.ChildNodes;
  283. foreach (XmlNode node in nodeList)
  284. {
  285. if (node.Name == "ColumnGrouping")
  286. {
  287. if (component is MatrixObject)
  288. {
  289. cornerHeight = LoadColumnGrouping(node, dynamicColumns, staticColumns);
  290. }
  291. }
  292. }
  293. }
  294. return cornerHeight;
  295. }
  296. private void LoadDynamicRows(XmlNode dynamicRowsNode, List<XmlNode> dynamicRows)
  297. {
  298. XmlNodeList nodeList = dynamicRowsNode.ChildNodes;
  299. foreach (XmlNode node in nodeList)
  300. {
  301. if (node.Name == "Subtotal")
  302. {
  303. XmlNodeList subtotalNodeList = node.ChildNodes;
  304. foreach (XmlNode subtotalNode in subtotalNodeList)
  305. {
  306. if (subtotalNode.Name == "ReportItems")
  307. {
  308. dynamicRows.Add(subtotalNode.Clone());
  309. }
  310. }
  311. }
  312. else if (node.Name == "ReportItems")
  313. {
  314. dynamicRows.Add(node.Clone());
  315. }
  316. else if (node.Name == "Visibility")
  317. {
  318. LoadVisibility(node);
  319. }
  320. }
  321. }
  322. private XmlNode LoadStaticRow(XmlNode staticRowNode)
  323. {
  324. XmlNode staticRow = null;
  325. XmlNodeList nodeList = staticRowNode.ChildNodes;
  326. foreach (XmlNode node in nodeList)
  327. {
  328. if (node.Name == "ReportItems")
  329. {
  330. staticRow = node.Clone();
  331. }
  332. }
  333. return staticRow;
  334. }
  335. private void LoadStaticRows(XmlNode staticRowsNode, List<XmlNode> staticRows)
  336. {
  337. XmlNodeList nodeList = staticRowsNode.ChildNodes;
  338. foreach (XmlNode node in nodeList)
  339. {
  340. if (node.Name == "StaticRow")
  341. {
  342. staticRows.Add(LoadStaticRow(node));
  343. }
  344. }
  345. }
  346. private float LoadRowGrouping(XmlNode rowGroupingNode, List<XmlNode> dynamicRows, List<XmlNode> staticRows)
  347. {
  348. float cornerWidth = 2.5f * Utils.Units.Centimeters;
  349. XmlNodeList nodeList = rowGroupingNode.ChildNodes;
  350. foreach (XmlNode node in nodeList)
  351. {
  352. if (node.Name == "Width")
  353. {
  354. cornerWidth = UnitsConverter.SizeToPixels(node.InnerText);
  355. }
  356. else if (node.Name == "DynamicRows")
  357. {
  358. LoadDynamicRows(node, dynamicRows);
  359. }
  360. else if (node.Name == "StaticRows")
  361. {
  362. LoadStaticRows(node, staticRows);
  363. }
  364. }
  365. return cornerWidth;
  366. }
  367. private float LoadRowGroupings(XmlNode rowGroupingsNode, List<XmlNode> dynamicRows, List<XmlNode> staticRows)
  368. {
  369. float cornerWidth = 2.5f * Utils.Units.Centimeters;
  370. if (rowGroupingsNode != null)
  371. {
  372. XmlNodeList nodeList = rowGroupingsNode.ChildNodes;
  373. foreach (XmlNode node in nodeList)
  374. {
  375. if (node.Name == "RowGrouping")
  376. {
  377. if (component is MatrixObject)
  378. {
  379. cornerWidth = LoadRowGrouping(node, dynamicRows, staticRows);
  380. }
  381. }
  382. }
  383. }
  384. return cornerWidth;
  385. }
  386. private void LoadMatrixCell(XmlNode matrixCellNode, MatrixCellDescriptor cell, int col)
  387. {
  388. int row = (component as MatrixObject).RowCount - 1;
  389. XmlNodeList nodeList = matrixCellNode.ChildNodes;
  390. foreach (XmlNode node in nodeList)
  391. {
  392. if (node.Name == "ReportItems")
  393. {
  394. }
  395. }
  396. }
  397. private void LoadMatrixCells(XmlNode matrixCellsNode)
  398. {
  399. int col = 0;
  400. XmlNodeList nodeList = matrixCellsNode.ChildNodes;
  401. foreach (XmlNode node in nodeList)
  402. {
  403. if (node.Name == "MatrixCell")
  404. {
  405. if (component is MatrixObject)
  406. {
  407. MatrixCellDescriptor cell = new MatrixCellDescriptor();
  408. (component as MatrixObject).Data.Cells.Add(cell);
  409. LoadMatrixCell(node, cell, col);
  410. col++;
  411. }
  412. }
  413. }
  414. }
  415. private float LoadMatrixRow(XmlNode matrixRowNode, MatrixHeaderDescriptor row)
  416. {
  417. float rowHeight = 0.8f * Utils.Units.Centimeters;
  418. XmlNodeList nodeList = matrixRowNode.ChildNodes;
  419. foreach (XmlNode node in nodeList)
  420. {
  421. if (node.Name == "Height")
  422. {
  423. rowHeight = UnitsConverter.SizeToPixels(node.InnerText);
  424. }
  425. else if (node.Name == "MatrixCells")
  426. {
  427. LoadMatrixCells(node);
  428. }
  429. }
  430. return rowHeight;
  431. }
  432. private float LoadMatrixRows(XmlNode matrixRowsNode)
  433. {
  434. float rowHeight = 0.8f * Utils.Units.Centimeters;
  435. if (matrixRowsNode != null)
  436. {
  437. XmlNodeList nodeList = matrixRowsNode.ChildNodes;
  438. foreach (XmlNode node in nodeList)
  439. {
  440. if (node.Name == "MatrixRow")
  441. {
  442. if (component is MatrixObject)
  443. {
  444. MatrixHeaderDescriptor row = new MatrixHeaderDescriptor();
  445. (component as MatrixObject).Data.Rows.Add(row);
  446. rowHeight = LoadMatrixRow(node, row);
  447. }
  448. }
  449. }
  450. }
  451. return rowHeight;
  452. }
  453. private float LoadMatrixColumn(XmlNode matrixColumnNode, MatrixHeaderDescriptor column)
  454. {
  455. float columnWidth = 2.5f * Utils.Units.Centimeters;
  456. XmlNodeList nodeList = matrixColumnNode.ChildNodes;
  457. foreach (XmlNode node in nodeList)
  458. {
  459. if (node.Name == "Width")
  460. {
  461. columnWidth = UnitsConverter.SizeToPixels(node.InnerText);
  462. }
  463. }
  464. return columnWidth;
  465. }
  466. private float LoadMatrixColumns(XmlNode matrixColumnsNode)
  467. {
  468. float columnWidth = 2.5f * Utils.Units.Centimeters;
  469. if (matrixColumnsNode != null)
  470. {
  471. XmlNodeList nodeList = matrixColumnsNode.ChildNodes;
  472. foreach (XmlNode node in nodeList)
  473. {
  474. if (node.Name == "MatrixColumn")
  475. {
  476. if (component is MatrixObject)
  477. {
  478. MatrixHeaderDescriptor column = new MatrixHeaderDescriptor();
  479. (component as MatrixObject).Data.Columns.Add(column);
  480. columnWidth = LoadMatrixColumn(node, column);
  481. }
  482. }
  483. }
  484. }
  485. return columnWidth;
  486. }
  487. private void LoadTable(XmlNode tableNode)
  488. {
  489. component = ComponentsFactory.CreateTableObject(tableNode.Attributes["Name"].Value, parent);
  490. XmlNodeList nodeList = tableNode.ChildNodes;
  491. LoadReportItem(nodeList);
  492. XmlNode tableColumnsNode = null;
  493. XmlNode headerNode = null;
  494. XmlNode detailsNode = null;
  495. XmlNode footerNode = null;
  496. XmlNode tableRowsNode = null;
  497. foreach (XmlNode node in nodeList)
  498. {
  499. if (node.Name == "TableColumns")
  500. {
  501. tableColumnsNode = node.Clone();
  502. }
  503. else if (node.Name == "Header")
  504. {
  505. headerNode = node.Clone();
  506. }
  507. else if (node.Name == "Details")
  508. {
  509. detailsNode = node.Clone();
  510. }
  511. else if (node.Name == "Footer")
  512. {
  513. footerNode = node.Clone();
  514. }
  515. else if (node.Name == "TablixBody")
  516. {
  517. if (node.HasChildNodes)
  518. foreach (XmlNode bodyChild in node.ChildNodes)
  519. if (bodyChild.Name == "TablixColumns")
  520. {
  521. tableColumnsNode = bodyChild.Clone();
  522. }
  523. else if (bodyChild.Name == "TablixRows")
  524. {
  525. tableRowsNode = node.Clone();
  526. }
  527. }
  528. }
  529. LoadTableColumns(tableColumnsNode);
  530. LoadHeader(headerNode != null ? headerNode : tableRowsNode);
  531. LoadDetails(detailsNode);
  532. LoadFooter(footerNode);
  533. (component as TableObject).CreateUniqueNames();
  534. }
  535. private bool IsTablixMatrix(XmlNode node)
  536. {
  537. if (node.HasChildNodes)
  538. foreach (XmlNode tablixItem in node.ChildNodes)
  539. {
  540. if (tablixItem.Name == "TablixCorner")
  541. return true;
  542. }
  543. return false;
  544. }
  545. private void LoadMatrix(XmlNode matrixNode)
  546. {
  547. component = ComponentsFactory.CreateMatrixObject(matrixNode.Attributes["Name"].Value, parent);
  548. MatrixObject matrix = component as MatrixObject;
  549. matrix.AutoSize = false;
  550. XmlNodeList nodeList = matrixNode.ChildNodes;
  551. LoadReportItem(nodeList);
  552. //XmlNode cornerNode = null;
  553. XmlNode columnGroupingsNode = null;
  554. XmlNode rowGroupingsNode = null;
  555. XmlNode matrixRowsNode = null;
  556. XmlNode matrixColumnsNode = null;
  557. foreach (XmlNode node in nodeList)
  558. {
  559. //if (node.Name == "Corner")
  560. //{
  561. // cornerNode = node.Clone();
  562. //}
  563. /*else */
  564. if (node.Name == "ColumnGroupings")
  565. {
  566. columnGroupingsNode = node.Clone();
  567. }
  568. else if (node.Name == "RowGroupings")
  569. {
  570. rowGroupingsNode = node.Clone();
  571. }
  572. else if (node.Name == "MatrixColumns")
  573. {
  574. matrixColumnsNode = node.Clone();
  575. }
  576. else if (node.Name == "MatrixRows")
  577. {
  578. matrixRowsNode = node.Clone();
  579. }
  580. }
  581. //LoadCorner(cornerNode);
  582. List<XmlNode> dynamicColumns = new List<XmlNode>();
  583. List<XmlNode> staticColumns = new List<XmlNode>();
  584. LoadColumnGroupings(columnGroupingsNode, dynamicColumns, staticColumns);
  585. List<XmlNode> dynamicRows = new List<XmlNode>();
  586. List<XmlNode> staticRows = new List<XmlNode>();
  587. LoadRowGroupings(rowGroupingsNode, dynamicRows, staticRows);
  588. float columnWidth = LoadMatrixColumns(matrixColumnsNode);
  589. float rowHeight = LoadMatrixRows(matrixRowsNode);
  590. matrix.CreateUniqueNames();
  591. matrix.BuildTemplate();
  592. for (int i = 1; i < matrix.Columns.Count; i++)
  593. {
  594. matrix.Columns[i].Width = columnWidth;
  595. }
  596. for (int i = 1; i < matrix.Rows.Count; i++)
  597. {
  598. matrix.Rows[i].Height = rowHeight;
  599. }
  600. for (int i = 0; i < matrix.Columns.Count; i++)
  601. {
  602. for (int j = 0; j < matrix.Rows.Count; j++)
  603. {
  604. matrix.GetCellData(i, j).Cell.Text = "";
  605. }
  606. }
  607. for (int i = 0; i < dynamicColumns.Count; i++)
  608. {
  609. Base tempParent = parent;
  610. ComponentBase tempComponent = component;
  611. parent = matrix.GetCellData(i + 1, 0).Cell;
  612. LoadReportItems(dynamicColumns[i]);
  613. component = tempComponent;
  614. parent = tempParent;
  615. }
  616. for (int i = 0; i < dynamicRows.Count; i++)
  617. {
  618. Base tempParent = parent;
  619. ComponentBase tempComponent = component;
  620. parent = matrix.GetCellData(0, i + 1).Cell;
  621. LoadReportItems(dynamicRows[i]);
  622. component = tempComponent;
  623. parent = tempParent;
  624. }
  625. }
  626. }
  627. }