public static XElement ConvertTableToHtml(SpreadsheetDocument sDoc, SmlToHtmlConverterSettings settings, string tableName) { var rangeXml = SmlDataRetriever.RetrieveTable(sDoc, tableName); var xhtml = SmlToHtmlConverter.ConvertToHtmlInternal(sDoc, settings, rangeXml); return(xhtml); }
public SmlToHtmlConverterSettings(SmlToHtmlConverterSettings htmlConverterSettings) { PageTitle = htmlConverterSettings.PageTitle; CssClassPrefix = htmlConverterSettings.CssClassPrefix; FabricateCssClasses = htmlConverterSettings.FabricateCssClasses; GeneralCss = htmlConverterSettings.GeneralCss; AdditionalCss = htmlConverterSettings.AdditionalCss; }
private static XNode ConvertToHtmlTransform(SpreadsheetDocument sDoc, SmlToHtmlConverterSettings htmlConverterSettings, XNode node) { var element = node as XElement; if (element != null) { return(new XElement(element.Name, element.Attributes(), element.Nodes().Select(n => ConvertToHtmlTransform(sDoc, htmlConverterSettings, n)))); } return(node); }
// *********************************************************************************************************************************** #region PublicApis public static XElement ConvertTableToHtml(SmlDocument smlDoc, SmlToHtmlConverterSettings settings, string tableName) { using (MemoryStream ms = new MemoryStream()) { ms.Write(smlDoc.DocumentByteArray, 0, smlDoc.DocumentByteArray.Length); using (SpreadsheetDocument sDoc = SpreadsheetDocument.Open(ms, false)) { var rangeXml = SmlDataRetriever.RetrieveTable(sDoc, tableName); var xhtml = SmlToHtmlConverter.ConvertToHtmlInternal(sDoc, settings, rangeXml); return(xhtml); } } }
// *********************************************************************************************************************************** private static XElement ConvertToHtmlInternal(SpreadsheetDocument sDoc, SmlToHtmlConverterSettings htmlConverterSettings, XElement rangeXml) { XElement xhtml = (XElement)ConvertToHtmlTransform(sDoc, htmlConverterSettings, rangeXml); ReifyStylesAndClasses(htmlConverterSettings, xhtml); // Note: the xhtml returned by ConvertToHtmlTransform contains objects of type // XEntity. PtOpenXmlUtil.cs define the XEntity class. See // http://blogs.msdn.com/ericwhite/archive/2010/01/21/writing-entity-references-using-linq-to-xml.aspx // for detailed explanation. // // If you further transform the XML tree returned by ConvertToHtmlTransform, you // must do it correctly, or entities will not be serialized properly. return(xhtml); }
public XElement ConvertTableToHtml(string tableName) { SmlToHtmlConverterSettings settings = new SmlToHtmlConverterSettings(); return(SmlToHtmlConverter.ConvertTableToHtml(this, settings, tableName)); }
public XElement ConvertToHtml(SmlToHtmlConverterSettings htmlConverterSettings, string tableName) { return(SmlToHtmlConverter.ConvertTableToHtml(this, htmlConverterSettings, tableName)); }
private static void ReifyStylesAndClasses(SmlToHtmlConverterSettings htmlConverterSettings, XElement xhtml) { if (htmlConverterSettings.FabricateCssClasses) { var usedCssClassNames = new HashSet <string>(); var elementsThatNeedClasses = xhtml .DescendantsAndSelf() .Select(d => new { Element = d, Styles = d.Annotation <Dictionary <string, string> >(), }) .Where(z => z.Styles != null); var augmented = elementsThatNeedClasses .Select(p => new { p.Element, p.Styles, StylesString = p.Element.Name.LocalName + "|" + p.Styles.OrderBy(k => k.Key).Select(s => string.Format("{0}: {1};", s.Key, s.Value)).StringConcatenate(), }) .GroupBy(p => p.StylesString) .ToList(); int classCounter = 1000000; var sb = new StringBuilder(); sb.Append(Environment.NewLine); foreach (var grp in augmented) { string classNameToUse; var firstOne = grp.First(); var styles = firstOne.Styles; if (styles.ContainsKey("PtStyleName")) { classNameToUse = htmlConverterSettings.CssClassPrefix + styles["PtStyleName"]; if (usedCssClassNames.Contains(classNameToUse)) { classNameToUse = htmlConverterSettings.CssClassPrefix + styles["PtStyleName"] + "-" + classCounter.ToString().Substring(1); classCounter++; } } else { classNameToUse = htmlConverterSettings.CssClassPrefix + classCounter.ToString().Substring(1); classCounter++; } usedCssClassNames.Add(classNameToUse); sb.Append(firstOne.Element.Name.LocalName + "." + classNameToUse + " {" + Environment.NewLine); foreach (var st in firstOne.Styles.Where(s => s.Key != "PtStyleName")) { var s = " " + st.Key + ": " + st.Value + ";" + Environment.NewLine; sb.Append(s); } sb.Append("}" + Environment.NewLine); var classAtt = new XAttribute("class", classNameToUse); foreach (var gc in grp) { gc.Element.Add(classAtt); } } var styleValue = htmlConverterSettings.GeneralCss + sb + htmlConverterSettings.AdditionalCss; SetStyleElementValue(xhtml, styleValue); } else { // Previously, the h:style element was not added at this point. However, // at least the General CSS will contain important settings. SetStyleElementValue(xhtml, htmlConverterSettings.GeneralCss + htmlConverterSettings.AdditionalCss); foreach (var d in xhtml.DescendantsAndSelf()) { var style = d.Annotation <Dictionary <string, string> >(); if (style == null) { continue; } var styleValue = style .Where(p => p.Key != "PtStyleName") .OrderBy(p => p.Key) .Select(e => string.Format("{0}: {1};", e.Key, e.Value)) .StringConcatenate(); XAttribute st = new XAttribute("style", styleValue); if (d.Attribute("style") != null) { d.Attribute("style").Value += styleValue; } else { d.Add(st); } } } }