如何将XML转换成MS文档?转换成、如何将、文档、XML

2023-09-04 06:08:39 作者:怎知春色几许

我有一些XML和我需要将其转换为Microsoft Office文档(2007年)。什么是做到这一点的最好和最快的方法是什么?我可以使用C#或Java做。我见过的尖顶。我能做到这一点,但它是相当昂贵的,是有别的选择吗?难道微软提供的东西?

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?

推荐答案

您可以使用XSLT - 而且是一个很好的样本的此处基督教格尔的OneNotes。

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

以这个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样式表:

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>

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

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>

要做到这一点在code看到这样的回答: http://stackoverflow.com/a/34095/30225

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

您需要做什么或者使用等效的Office 2007的docx或只产生了2003文档,让人们在2007年将其打开。

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.