private static void WriteLine(this LineTable lta, MWord.Range range, int fontsize = 12) { var oTable = range.Tables.Add(range.Bookmarks["\\endofdoc"].Range, lta.RowCount + 1, lta.ColCount + 1); for (var r = 0; r < lta.RowCount + 1; r++) { for (var c = 0; c < lta.ColCount + 1; c++) { var cellRange = oTable.Cell(r + 1, c + 1).Range; cellRange.Text = lta.Content[r, c]; cellRange.OMaths.Add(cellRange); cellRange.OMaths[1].BuildUp(); cellRange.OMaths[1].Range.Font.Size = fontsize; } foreach (var merge in lta.CellRange2Merge) { var cell1 = oTable.Cell(merge[0, 0] + 1, merge[0, 1] + 1); var cell2 = oTable.Cell(merge[1, 0] + 1, merge[1, 1] + 1); cell1.Merge(cell2); } oTable.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitContent); oTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleOutset; oTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle; } }
private static void CondenseTable(LineTable table) { var cMax = table.LineBoxes.Max(x => x.Col_Max); var rMax = table.LineBoxes.Max(x => x.Row_Max); //var keep_col_single = table.LineBoxes.Where(x => x.Column_Span == 1).Select(x => x.Column_Min); //var keep_row_single = table.LineBoxes.Where(x => x.Row_Span == 1).Select(x => x.Row_Min); //var keep_col_mult = table.LineBoxes.Where(x => x.Column_Span > 1).Where(x => !keep_col_single.Any(c => c >= x.Column_Min || c <= x.Column_Max)).Select(x => x.Column_Min); //var keep_row_mult = table.LineBoxes.Where(x => x.Row_Span > 1).Where(x => !keep_row_single.Any(c => c >= x.Row_Min || c <= x.Row_Max)).Select(x => x.Row_Min); //var keep_col = keep_col_mult.Concat(keep_col_single).Distinct().ToList(); //var keep_row = keep_row_mult.Concat(keep_row_single).Distinct().ToList(); var keep_col = table.LineBoxes.SelectMany(x => new[] { x.Col_Max }).Distinct(); var keep_row = table.LineBoxes.SelectMany(x => new[] { x.Row_Max }).Distinct(); var remove_col = Enumerable.Range(0, cMax + 1).Except(keep_col).ToList(); var remove_row = Enumerable.Range(0, rMax + 1).Except(keep_row).ToList(); table.LineBoxes.ForEach(x => { x.Col_Min -= remove_col.Count(y => y < x.Col_Min); x.Col_Max -= remove_col.Count(y => y < x.Col_Max); x.Row_Min -= remove_row.Count(y => y < x.Row_Min); x.Row_Max -= remove_row.Count(y => y < x.Row_Max); }); }
public static TableInfo FindDataRowsAndColumns(LineTable table) { var rMax = table.LineBoxes.Max(x => x.Row_Max); var cMax = table.LineBoxes.Max(x => x.Col_Max); var cols = Enumerable.Range(0, cMax + 1).Select(c => new ColInfo { Col = c, Cells = table.LineBoxes.Where(x => c >= x.Col_Min && c <= x.Col_Max).OrderBy(x => x.Row_Min).ToList() }).ToList(); var rows = Enumerable.Range(0, rMax + 1).Select(r => new RowInfo { Row = r, Cells = table.LineBoxes.Where(x => r >= x.Row_Min && r <= x.Row_Max).OrderBy(x => x.Col_Min).ToList() }).ToList(); // var rowMatches = rows.Select((row, i) => new { row, matchingRows = rows.Skip(i).Where(r2 => IsSimilarRow(row, r2)).ToList() }).ToList(); // Add row values foreach (var row in rows) { row.Values = row.Cells.Select(x => new ValueInfo { Cell = x, Cols = cols.Where(y => y.Cells.Contains(x)).ToList() }).ToList(); } // Identify Data Rows var rowClusters = rows.Select(x => new List <RowInfo>() { x }).ToList(); for (var i = 0; i < rowClusters.Count; i++) { for (var j = i + 1; j < rowClusters.Count; j++) { if (rowClusters[i].Any(x => rowClusters[j].Any(y => IsSimilarRow(x, y)))) { rowClusters[i].AddRange(rowClusters[j]); rowClusters.RemoveAt(j); j--; } } } var topCluster = rowClusters.OrderByDescending(x => x.Count).FirstOrDefault(); if (topCluster.Count > 1) { topCluster.ForEach(x => x.IsDataRow = true); topCluster.ForEach(x => x.Cells.ForEach(y => y.IsDataCell = true)); } return(new TableInfo() { Cols = cols, Rows = rows }); }
public static string ToClipboard_HtmlTableFormat(this LineTable table) { var sb = new StringBuilder(); sb.Append("<table>"); var cMax = table.LineBoxes.Max(x => x.Col_Max); var rMax = table.LineBoxes.Max(x => x.Row_Max); // Reverse Rows for (var r = rMax; r >= 0; r--) { sb.Append("<tr>"); for (var c = 0; c <= cMax; c++) { var v = table.LineBoxes.Where(x => x.Col_Min <= c && x.Col_Max >= c && x.Row_Min <= r && x.Row_Max >= r).OrderBy(x => x.Col_Min).ThenBy(x => x.Row_Min).FirstOrDefault(); // Skip if in span if (v?.Col_Min != c || v?.Row_Max != r) { continue; } var span = $"{(v?.Row_Span > 1 ? $"rowspan={v.Row_Span}" : "")} {(v?.Col_Span > 1 ? $"colspan={v.Col_Span}" : "")}"; // Force excel text format sb.Append($"<td {span} style='mso-number-format:\"\\@\"'>"); sb.Append(v?.CellText ?? ""); sb.Append("</td>"); } sb.Append("</tr>"); } sb.Append("</table>"); var html = sb.ToString(); return(html); }
public ITable <T> Table <T>() where T : ITableRow { ITable <T> table = null; IConnector connector = new SqlServerConnector(ConnectionString); if (typeof(T) == typeof(Station)) { table = new StationTable(connector) as ITable <T>; } else if (typeof(T) == typeof(Line)) { table = new LineTable(connector) as ITable <T>; } else if (typeof(T) == typeof(StationLine)) { table = new StationLineTable(connector) as ITable <T>; } else if (typeof(T) == typeof(MetroTransfer)) { table = new MetroTransferTable(connector) as ITable <T>; } return(table); }
public static TableData ExtractTable(LineTable table, TablePattern pattern) { var tableInfo = TableDataRowFinder.FindDataRowsAndColumns(table); var columnPatterns = AssignColumns(pattern, tableInfo); if (columnPatterns.Any(x => x.Key.IsRequired && x.Value == null)) { return(null); } var columns = columnPatterns.Select(x => new TableDataColumn() { Name = x.Key.Name, SourceBounds = x.Value?.Cells.Where(c => c.Col_Span == 1).Select(c => c.Box.Bounds).Where(b => b != new Bounds()).UnionBounds() ?? new Bounds(), }).ToList(); var rows = tableInfo.Rows.Select(row => { var rowOut = new TableDataRow() { Values = columnPatterns.Select(col => { var v = row.Values.Where(x => x.Cols.Contains(col.Value)).FirstOrDefault(); if (v == null) { return(null); } // Ensure value matches pattern if (!col.Key.ValueMatchRegex.IsMatch(v.Cell.CellText)) { return(null); } var column = columns.Where(c => c.Name == col.Key.Name).FirstOrDefault(); return(new TableDataValue() { Column = column, Value = v.Cell.CellText, MergeId = v.Cell.Col_Span > 1 || v.Cell.Row_Span > 1 ? "" + v.Cell.CellId : null as string, SourceBounds = v.Cell.Box.Bounds, FontHeight = v.Cell.Box.Texts.Average(x => x.FontHeight), }); }).Where(x => x != null).ToList() }; if (!columnPatterns.Keys.Where(x => x.IsRequired).All(x => rowOut.Values.Any(y => y.Column.Name == x.Name))) { return(null); } return(rowOut); }).Where(x => x != null).ToList(); // columns.ForEach(col => col.SourceBounds = rows.SelectMany(x => x.Values).Where(v => v.Column == col).Select(x => x.SourceBounds).UnionBounds()); // columns.ForEach(col => col.SourceHeaderText = rows.SelectMany(x => x.Values).Where(v => v.Column == col).Select(x => x.SourceBounds).UnionBounds()); return(new TableData() { SourceBounds = table.Bounds, // SourceBounds = rows.SelectMany(x => x.Values.Select(v => v.SourceBounds)).UnionBounds(), SourceBounds_Cropped = rows.SelectMany(x => x.Values.Select(v => v.SourceBounds)).UnionBounds(), TableName = pattern.Name, Rows = rows, Columns = columns, }); }