XLS格式:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>ReportName</th>
<th>Date</th>
</tr>
<tr>
<td>
<xsl:value-of select="ValulationReport/ClientName"/>
</td>
<td>
<xsl:value-of select="ValulationReport/ReportDate"/>
</td>
</tr>
</table>
<p>Please see the attached file.Thanks</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XML文件:
转换代码:
public void Transform(string inXml, string inXsl, string outHtml)
{
XmlReader xslReader = null;
Stream str = null;
try
{
// Do the transform
FileInfo f = new FileInfo(inXsl);
if (f.Exists == false)
{
throw new Exception(string.Format("Disclaimer template {0} doesn't exist.", inXsl));
}
xslReader = new XmlTextReader(inXsl);
XslTransform transform = new XslTransform();
transform.Load(xslReader);
XPathDocument pathDoc = new XPathDocument(inXml);
StreamWriter writer = new StreamWriter(outHtml, false, new ASCIIEncoding());
transform.Transform(pathDoc, null, writer);
writer.Flush();
writer.Close();
}
catch (Exception ex)
{
string errorMes = string.Format("Exception occurred as transforming email body xsl file to html file and error message is \"{0}\"", ex.Message);
ourLogger.Error(errorMes);
throw new DocComposerGenerateReportException(errorMes, ex);
}
finally
{
if (xslReader != null)
xslReader.Close();
if (str != null)
str.Close();
}
}