public static SimpleTable HtmlToTable(string html) { var table = new SimpleTable(); var widths = new double[100]; var doc = new HtmlDocument(); doc.LoadHtml(html); var hnTable = doc.DocumentNode.SelectSingleNode("//table"); var trNodes = hnTable.SelectNodes("tr"); var maxCellCount = 0; foreach (var tr in trNodes) { var row = new TR(); var cellCount = 0; var iHeight = RdlUtils.ConvertUnit(tr.GetAttributeValue("height", "")); var tdNodes = tr.SelectNodes("th|td"); foreach (var td in tdNodes) { var colSpan = td.GetAttributeValue("colspan", 1); var rowSpan = td.GetAttributeValue("rowspan", 1); var tdHeight = RdlUtils.ConvertUnit(td.GetAttributeValue("height", "")); var tdWidth = RdlUtils.ConvertUnit(td.GetAttributeValue("width", "")); if (tdWidth== 0) { tdWidth = 21; } if (rowSpan == 1) { if (tdHeight > iHeight) { iHeight = tdHeight; } } if (colSpan == 1) { if (tdWidth > widths[cellCount]) { widths[cellCount] = tdWidth; } } var text = td.InnerText.Trim().Replace(" ", " "); var font=new Font(new FontFamily("宋体") ,0.75F,FontStyle.Regular); var size=System.Windows.Forms.TextRenderer.MeasureText(text, font); //var ww=Common.Report.Rdlc.Enums.MeasureTools.UnitToMillimeters(new Unit(size.Width, UnitType.Pixel) ); //if (ww > tdWidth) //{ // var hh = Common.Report.Rdlc.Enums.MeasureTools.UnitToMillimeters(new Unit(size.Height, UnitType.Pixel)); // tdHeight = Math.Ceiling(ww / tdWidth) * hh; // if (tdHeight > iHeight) // { // iHeight = tdHeight; // } //} var cell = new TD(colSpan, rowSpan, text) { Tag = td }; row.Cells.Add(cell); cellCount += colSpan; if (maxCellCount < cellCount) { maxCellCount = cellCount; } } iHeight = (iHeight == 0 ? 6 : iHeight); row.Height = iHeight; table.Rows.Add(row); } var columns = new List<double>(); for (var i = 0; i < maxCellCount; i++) { columns.Add(widths[i]); } table.Columns = columns; return table; }
public void Computer() { var columnCount = this.Columns.Count; var maxRowCellCount = columnCount; var rowCount = this.Rows.Count; var emptyTds = new List<Point>(); var rowIndex = 0; foreach (var tr in Rows) { tr.RowIndex = rowIndex; var computed = false; //当前单元格的位置 var currCellPos = 0; var tempRow = new TR(); for (int i = 0; i < maxRowCellCount; i++) { var cell = new TD(); if (emptyTds.Contains(new Point(i, rowIndex))) { cell.ShouldEmpty = true; } else { cell.ShouldEmpty = false; if (!computed) { currCellPos = i; computed = true; } } cell.HasValue = false; tempRow.Cells.Add(cell); } foreach (var td in tr.Cells) { if (td == null) { continue; } td.RowIndex = rowIndex; var colSpan = td.ColSpan; var rowSpan = td.RowSpan; if (rowSpan > 1 || colSpan > 1) { for (int y = rowIndex; y < rowIndex + rowSpan; y++) { for (int x = currCellPos; x < currCellPos + colSpan; x++) { //本身不算在内 if (y == rowIndex && x == currCellPos) continue; //计算占位空TextBox if (y == rowIndex) { tempRow.Cells[x].ShouldEmpty = true; } emptyTds.Add(new Point { X = x, Y = y }); } } } if (rowIndex > 0) { for (int i = 0; i < tempRow.Cells.Count; i++) { var c = tempRow.Cells[i]; if (c.ShouldEmpty == false && c.HasValue == false) { tempRow.Cells[i] = td; td.ColumnIndex = i; break; } } } else { td.ColumnIndex = currCellPos; } currCellPos += colSpan; } rowIndex++; } for (int i = 0; i < this.Columns.Count;i++ ) { if (this.Columns[i] == 0) { this.Columns[i] = 21; } } this.Computed = true; }