如何将XML转换为MS Doc?

时间:2012-05-09 03:04:13

标签: java .net xml docx

我有一些XML,我需要将它们转换为Microsoft Office Document(2007)。什么是最好和最快的方法?我可以使用C#或Java来完成它。我见过那个尖顶。我能做到,但它很贵,还有其他选择吗?微软是否提供了一些东西?

1 个答案:

答案 0 :(得分:4)

你可以使用XSLT - 在Christian Nagel的OneNotes上有一个很好的样本here

采用此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>

使用此XML样式表:

<?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>

可以为2003年生成此MS Word文档。

<?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>

要在代码中执行此操作,请参阅以下答案:https://stackoverflow.com/a/34095/30225

您需要做的是使用Office 2007 docx的等效文件,或者只生成2003文档并让人们在2007年打开它。