Я пытаюсь перевести XML-данные в Excel, используя шаблон xslt.Но я получаю только заголовки, а не данные.Пожалуйста, ознакомьтесь с приведенными ниже шаблонами xml data и xslt и моим кодом C #.
example.xml
<?xml version="1.0"?>
<projects>
<project>
<name>Shock</name>
<language>Ruby</language>
<owner>Brian May</owner>
<state>New</state>
<Date>31/10/2008 0:00:00</Date>
</project>
<project>
<name>Shock1</name>
<language>Ruby1</language>
<owner>Brian May1</owner>
<state>New1</state>
<Date>30/10/2008 0:00:00</Date>
</project>
</projects>
test.xsl
<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Excel.Sheet"?>
<xsl:stylesheet version="1.0"
xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<xsl:template match="/">
<Workbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom" />
<Borders />
<Font />
<Interior />
<NumberFormat />
<Protection />
</Style>
<Style ss:ID="s21">
<Font ss:Size="22" ss:Bold="1" />
</Style>
<Style ss:ID="s22">
<Font ss:Size="14" ss:Bold="1" />
</Style>
<Style ss:ID="s23">
<Font ss:Size="12" ss:Bold="1" />
</Style>
<Style ss:ID="s24">
<Font ss:Size="10" ss:Bold="1" />
</Style>
</Styles>
<Worksheet ss:Name="Page1">
<Table>
<xsl:call-template name="XMLToXSL" />
</Table>
</Worksheet>
</Workbook>
</xsl:template>
<xsl:template name="XMLToXSL">
<Row ss:Index="1" >
<Cell>
<Data ss:Type="String">name</Data>
</Cell>
<Cell>
<Data ss:Type="String">language</Data>
</Cell>
<Cell>
<Data ss:Type="String">owner</Data>
</Cell>
<Cell>
<Data ss:Type="String">state</Data>
</Cell>
<Cell>
<Data ss:Type="String">Date</Data>
</Cell>
</Row>
<xsl:for-each select="/projects">
<xsl:for-each select="/project">
<Row ss:Index="1">
<Cell ss:Index="2">
<Data ss:Type="String">
<xsl:value-of select="name" />
</Data>
</Cell>
<Cell ss:Index="2">
<Data ss:Type="String">
<xsl:value-of select="language" />
</Data>
</Cell>
<Cell ss:Index="2">
<Data ss:Type="String">
<xsl:value-of select="owner" />
</Data>
</Cell>
<Cell ss:Index="2">
<Data ss:Type="String">
<xsl:value-of select="state" />
</Data>
</Cell>
<Cell ss:Index="2">
<Data ss:Type="String">
<xsl:value-of select="Date" />
</Data>
</Cell>
</Row>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template match="DataList">
</xsl:template>
</xsl:stylesheet>
C # code:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"C:\Xmls\example.xml");
XslCompiledTransform xct = new XslCompiledTransform();
xct.Load(@"C:\Templates\Test.xsl");
XmlTextWriter writer = new XmlTextWriter(@"C:\Outputs\output.xls", null);
writer.WriteProcessingInstruction("xml", "version='1.0'");
xct.Transform(xdoc, null, writer);
writer.Close();
Excel output.xls:
| name | language | owner | state | Date |
-----------------------------------------------------
Но мой ожидаемый результат должен быть следующим:
| name | language | owner | state | Date |
------------------------------------------------------------------
| Shock | Ruby | Brian May | New | 31/10/2008 0:00:00 |
------------------------------------------------------------------
| Shock1 | Ruby1 | Brian May1| New1 | 30/10/2008 0:00:00 |
------------------------------------------------------------------
В настоящее время это печать одних только заголовков в Excel, Какчтобы получить данные в Excel по строкам.