0

I have some XML and I need to convert them to a Microsoft Office Document(2007). What's the best and fastest way to do it? I can do it using C# or Java. I've seen that spire. I can do it but it's quite expensive, is there an alternative? Does Microsoft offer something?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
Thiago Valle
  • 449
  • 1
  • 6
  • 20

1 Answers1

4

You could use XSLT - And there's a nice sample here on Christian Nagel's OneNotes.

Taking this XML

    <?xml version="1.0" encoding="utf-8" ?>
    <Courses>
     <Course Number="MS-2524">
      <Title>XML Web Services Programming</Title>
     </Course>
     <Course Number="MS-2124">
      <Title>C# Programming</Title>
     </Course>
     <Course Number="NET2">
      <Title>.NET 2.0 Early Adapter</Title>
     </Course>
    </Courses>

And using this XML Style sheet:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
 <xsl:output method="xml" indent="yes" />
 <xsl:template match="/">
  <xsl:processing-instruction name="mso-application">
   <xsl:text>progid="Word.Document"</xsl:text>
  </xsl:processing-instruction>
  <w:wordDocument>
   <w:body>
     <xsl:apply-templates select="Courses/Course" />
   </w:body>
  </w:wordDocument>
 </xsl:template>
 <xsl:template match="Course">
    <w:p>
     <w:r>
      <w:t>
       <xsl:value-of select="@Number" />, <xsl:value-of select="Title"/>
      </w:t>
     </w:r>
    </w:p>
 </xsl:template>
</xsl:stylesheet>

Can generate this MS Word Doc for 2003.

<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
  <w:body>
    <w:p>
      <w:r>
        <w:t>MS-2524, XML Web Services Programming</w:t>
      </w:r>
    </w:p>
    <w:p>
      <w:r>
        <w:t>MS-2124, C# Programming</w:t>
      </w:r>
    </w:p>
    <w:p>
      <w:r>
        <w:t>NET2, .NET 2.0 Early Adapter</w:t>
      </w:r>
    </w:p>
  </w:body>
</w:wordDocument>

To do this in code see this answer: https://stackoverflow.com/a/34095/30225

What you need to do either use an equivalent for Office 2007 docx or just generate the 2003 doc and let people open it in 2007.

Community
  • 1
  • 1
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216