Check my blog:
http://spaces.msn.com/sholliday/
9/22/2005
You need to do an XML to XML transformation.
The format of the Excel XML is .... unique. It also has namespaces. So
there isn't a super trivial example.
I have some code below. I think it will work, but it will definately get
you going.
It took me about 2 weeks to figure this out, so post a 'thank you' please,
(and any issues if you run into them)
Ok..
Create an excel file.
3 columns, the first row is the header.
uid,lastname,firstname
Add like 4 more rows of real data ( "123","Smith","John" .. like that)
Save as XML.
I have the C# code, which will show how to apply the XSL to the XML.
There is xsl also. Hopefully, it won't get screwed up
///////////////// START C# code
/// <summary>
/// Summary description for XMLtoXSLTransformWrapper.
/// </summary>
public class XMLtoXSLTransformWrapper
{
string debugMsg=null;
public void DoTranslation(string xmlFile, string xslFile, string
outputFile)
{
try
{
//Create a new XslTransform object.
XslTransform xslt = new XslTransform();
//Load the stylesheet.
xslt.Load(xslFile);
//Create a new XPathDocument and load the XML data to be transformed.
XPathDocument mydata = new XPathDocument(xmlFile);
//Create an XmlTextWriter which outputs to the console.
//XmlWriter writer = new XmlTextWriter(Console.Out);
//Transform the data and send the output to the console.
//xslt.Transform(mydata,null,writer, null);
xslt.Transform (xmlFile, outputFile);
}
catch (Exception ex)
{
debugMsg = ex.Message;
Console.WriteLine (debugMsg);
}
}
public XMLtoXSLTransformWrapper()
{
//
// TODO: Add constructor logic here
//
}
}
}
//END C#
//START XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"
xmlns
="urn:schemas-microsoft-com
ffice
ffice"
xmlns:x="urn:schemas-microsoft-com
ffice:excel"
xmlns:ss="urn:schemas-microsoft-com
ffice:spreadsheet" >
<xsl:template match="/">
<OneWellDefinedDS>
<!--<myRootPosition><xsl:value-of select="position()"/></myRootPosition>-->
<xsl:apply-templates select="ss:Workbook"/>
</OneWellDefinedDS>
</xsl:template>
<xsl:template match="ss:Workbook">
<!--<myWorkbookPosition><xsl:value-of
select="position()"/></myWorkbookPosition>-->
<xsl:apply-templates select="ss:Worksheet"/>
</xsl:template>
<xsl:template match="ss:Worksheet">
<!--<myWorksheetPosition><xsl:value-of
select="position()"/></myWorksheetPosition>-->
<xsl:choose>
<xsl:when test="position()=1">
<xsl:apply-templates select="ss:Table"/>
</xsl:when>
<xsl
therwise>
</xsl
therwise>
</xsl:choose>
</xsl:template>
<xsl:template match="ss:Table">
<!--<myTablePosition><xsl:value-of
select="position()"/></myTablePosition>-->
<xsl:apply-templates select="ss:Row"/>
</xsl:template>
<xsl:template match="ss:Row">
<!--<myRowPosition><xsl:value-of select="position()"/></myRowPosition>-->
<xsl:choose>
<xsl:when test="position()=1">
</xsl:when>
<xsl
therwise>
<customerInfo>
<!--<myPosition><xsl:value-of select="position()"/></myPosition>-->
<xsl:apply-templates select="ss:Cell"/>
</customerInfo>
</xsl
therwise>
</xsl:choose>
</xsl:template>
<xsl:template match="ss:Cell">
<!--<myCellPosition><xsl:value-of
select="$varCurrentAttributePosition"/></myCellPosition>-->
<xsl:choose>
<xsl:when test="position()=1">
<xsl:call-template name="xuid" />
</xsl:when>
<xsl:when test="position()=2">
<xsl:call-template name="xlastname" />
</xsl:when>
<xsl:when test="position()=3">
<xsl:call-template name="xfirstname" />
</xsl:when>
<xsl
therwise>
</xsl
therwise>
</xsl:choose>
</xsl:template>
<xsl:template name="xuid">
<uid><xsl:value-of select="."/></uid>
</xsl:template>
<xsl:template name="xlastname">
<lastname><xsl:value-of select="."/></lastname>
</xsl:template>
<xsl:template name="xfirstname">
<firstname><xsl:value-of select="."/></firstname>
</xsl:template>
</xsl:stylesheet>