using C1.Win.C1FlexGrid; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using VEPROMS.CSLA.Library; using Volian.Controls.Library; namespace XMLConvert { public partial class frmConvertXML { private static void LoadTable2(XmlNode xn, StepLookup mySteps, int level) { XmlNode xn2 = xn.SelectSingleNode("tr/td/table"); if (xn2 != null) xn = xn2; VlnFlexGrid fg = _MyFlexGrid; fg.Font = new Font("Arial", 11, FontStyle.Regular, GraphicsUnit.Point); fg.Clear(); fg.Rows.Count = 1; fg.Cols.Count = 1; int rows = 0; fg.MergedRanges.Clear(); foreach (XmlNode xr in xn.ChildNodes) { if (xr.Name == "tr") { ++rows; AddTableRow(xr, fg, rows); } } fg.MyBorders = new VlnBorders(GridLinePattern.Single, fg.Rows.Count, fg.Cols.Count); fg.BringToFront(); fg.Invalidate(); Application.DoEvents(); fg.MakeRTFcells(); //Well, Can I save the table using (Step step = MakeCSLAStep(mySteps, mySteps.Count, null, "table", 20008, E_FromType.Table)) { Grid.MakeGrid(step.MyContent, fg.GetXMLData(), ""); } } public static VlnFlexGrid _MyFlexGrid = null; private static void AddTableRow(XmlNode xr, VlnFlexGrid fg, int rows) { if (rows > fg.Rows.Count) fg.Rows.Count = rows; int cols = 0; foreach (XmlNode xc in xr.ChildNodes) { ++cols; //if (xc.InnerText.Contains("RC-V200")) // Console.WriteLine(xc.InnerText); //if (xc.InnerText.Contains("RC-V121")) // Console.WriteLine(xc.InnerText); //if (xc.InnerXml.Contains("AB 137") || xc.InnerXml.Contains("3013N01")) // Console.WriteLine("here"); CellRange cr2 = GetMyMergedRange(fg, rows - 1, cols - 1); //Console.WriteLine("Check {0}", cr2); while (cr2.c1 != cols - 1 || cr2.r1 != rows - 1) { cols++; cr2 = GetMyMergedRange(fg, rows - 1, cols - 1); } AddMergedCells(fg, rows, cols, xc); //ShowMergedCells(fg); if (xc.Name == "td") { AddTableColumn(xc, fg, rows, cols); } } } private static void ShowMergedCells(VlnFlexGrid fg) { for (int r = 0; r < fg.Rows.Count; r++) { for (int c = 0; c < fg.Cols.Count; c++) { CellRange cr3 = GetMyMergedRange(fg, r, c); if (fg.MergedRanges.Contains(cr3)) Console.WriteLine("*** cr3 r={0},c={1},rng={2}", r, c, cr3); } } } private static int GetSpan(string span) { int retval = int.Parse("0" + (span ?? "")); if (retval == 0) return 0; return retval - 1; } private static void AddMergedCells(VlnFlexGrid fg, int rows, int cols, XmlNode xc) { string colspan = GetAttribute(xc, "colspan"); string rowspan = GetAttribute(xc, "rowspan"); if (colspan != null || rowspan != null) { //AddMergedRanges int r1 = rows; int c1 = cols; int r2 = r1 + GetSpan(rowspan); if (r2 > fg.Rows.Count) fg.Rows.Count = r2; int c2 = c1 + GetSpan(colspan); if (c2 > fg.Cols.Count) fg.Cols.Count = c2; CellRange cr = new CellRange(); cr.r1 = r1 - 1; cr.r2 = r2 - 1; cr.c1 = c1 - 1; cr.c2 = c2 - 1; fg.MergedRanges.Add(cr); //Console.WriteLine("Merged {0}", cr); } } private static CellRange GetMyMergedRange(VlnFlexGrid fg, int r, int c) { foreach (CellRange cr in fg.MergedRanges) { if (cr.r1 <= r && cr.r2 >= r && cr.c1 <= c && cr.c2 >= c) return cr; } return fg.GetMergedRange(r, c); } private static Regex regNumber = new Regex("^[0-9]+$", RegexOptions.Compiled); private static void AddTableColumn(XmlNode xc, VlnFlexGrid fg, int rows, int cols) { //Console.WriteLine("Rows {0}, Cols {1}", rows, cols); if (cols > fg.Cols.Count) fg.Cols.Count = cols; string width = GetAttribute(xc, "width"); if (width != null && width != "" && regNumber.IsMatch(width)) { if (width.EndsWith("%")) fg.Cols[cols - 1].Width = (int)(int.Parse(width.Replace("%", "")) * 96 * 6.5F / 100); else fg.Cols[cols - 1].Width = int.Parse(width) * 96 / 72; } StringBuilder sb = new StringBuilder(); string prefix = ""; foreach (XmlNode xn in xc.ChildNodes) { if (xn.Name == "p") { sb.Append(prefix + xn.InnerText); } if (xn.Name == "ul") { foreach (XmlNode xn2 in xn.ChildNodes) { if (xn2.Name == "li") { sb.Append(prefix + "*" + xn.InnerText); } if (xn2.Name == "p") { sb.Append(prefix + xn.InnerText); } } } } if (width != null && width != "" && !regNumber.IsMatch(width)) { Console.WriteLine("*** width is not a number {0}, rows {1}, cols {2}, Text='{3}'", width, rows, cols, xc.OuterXml); } fg[rows - 1, cols - 1] = sb.ToString(); } } }