private static TableBody ParseTableBody( XElement tableBodyTag, BaseStyle tablestyle, IDictionary <string, BaseStyle> classes) { var attrClass = tableBodyTag.Attributes("class").FirstOrDefault(); BaseStyle tbodyStyle; if (attrClass != null) { var tbodyClasses = GetStylesByClasses(classes, attrClass); tbodyStyle = StyleFactory.MergeStyles(tablestyle, tbodyClasses); } else { tbodyStyle = tablestyle; } var rows = tableBodyTag.Elements("tr").Select(tr => ParseBodyRow(tr, tbodyStyle, classes)); var tbody = new TableBody { Style = tbodyStyle, Rows = rows.ToArray() }; return(tbody); }
private static TableRow ParseBodyRow( XElement tableBodyRowTag, BaseStyle tbodyStyle, IDictionary <string, BaseStyle> classes) { var attrClass = tableBodyRowTag.Attributes("class").FirstOrDefault(); BaseStyle rowStyle; if (attrClass != null) { var tbodyClasses = GetStylesByClasses(classes, attrClass); rowStyle = StyleFactory.MergeStyles(tbodyStyle, tbodyClasses); } else { rowStyle = tbodyStyle; } var cells = tableBodyRowTag.Elements("td").Select(td => ParseCell(td, rowStyle, classes)); var tableRow = new TableRow { Style = rowStyle, Cells = cells.ToArray() }; return(tableRow); }
private static IEnumerable <Table> ParseTables(IEnumerable <XElement> tableTags, IDictionary <string, BaseStyle> classes) { var tableIndex = 0; foreach (var tableTag in tableTags) { var tableStyle = StyleFactory.DefaultStyle; var attrClass = tableTag.Attributes("class").FirstOrDefault(); if (attrClass != null) { var tableClasses = GetStylesByClasses(classes, attrClass); tableStyle = StyleFactory.MergeStyles(tableClasses); } var attrTitle = tableTag.Attributes("title").FirstOrDefault(); var tableName = attrTitle != null ? attrTitle.Value : tableIndex.ToString(CultureInfo.InvariantCulture); TableBody tableBody; var tableBodyTag = tableTag.Elements("tbody").FirstOrDefault(); if (tableBodyTag != null) { tableBody = ParseTableBody(tableBodyTag, tableStyle, classes); } else { throw new FormatException("Can't find 'tbody' tag."); } var tableHeadTag = tableTag.Elements("thead").FirstOrDefault(); var tableHead = tableHeadTag != null?ParseTableHead(tableHeadTag, tableStyle, classes) : null; var table = new Table { Body = tableBody, Head = tableHead, Style = tableStyle, Name = tableName }; tableIndex++; yield return(table); } }
private static TableCell ParseCell( XElement cellTag, BaseStyle rowStyle, IEnumerable <KeyValuePair <string, BaseStyle> > classes) { BaseStyle cellStyle; var attrClass = cellTag.Attributes("class").FirstOrDefault(); if (attrClass != null) { var cellClasses = GetStylesByClasses(classes, attrClass); cellStyle = StyleFactory.MergeStyles(rowStyle, cellClasses); } else { cellStyle = rowStyle; } string formula = null; var attrFormula = cellTag.Attributes("formula").FirstOrDefault(); if (attrFormula != null) { formula = attrFormula.Value; } string numberFormat = null; var attrNumberFormat = cellTag.Attributes("number-format").FirstOrDefault(); if (attrNumberFormat != null) { numberFormat = attrNumberFormat.Value; } string title = null; var attrTitle = cellTag.Attributes("title").FirstOrDefault(); if (attrTitle != null) { title = attrTitle.Value; } int colspan = 0; var attrColspan = cellTag.Attributes("colspan").FirstOrDefault(); if (attrColspan != null) { int.TryParse(attrColspan.Value, out colspan); } int rowspan = 0; var attrRowspan = cellTag.Attributes("rowspan").FirstOrDefault(); if (attrRowspan != null) { int.TryParse(attrRowspan.Value, out rowspan); } var cell = new TableCell { Style = new CellStyle(cellStyle, formula, numberFormat, title, colspan, rowspan), Value = ProcessString(cellTag) }; return(cell); }