2

Microsoft Word 2007 ドキュメントにインポートする必要がある XML ファイルがあります。

WordprocessingML テーブルを構築する XSL ファイルがあります。

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

<w:style w:type="table" w:styleId="TableStyle">
    <w:name w:val="Table Style"/>
    <w:tblPr>
        <w:tblBorders>
            <w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/>
        </w:tblBorders>
    </w:tblPr>
</w:style>  

<xsl:template match="list">

    <xsl:processing-instruction name="mso-application">
        <xsl:text>progid="Word.Document"</xsl:text>
    </xsl:processing-instruction>

    <w:tbl>
        <w:tblPr>
            <w:tblStyle w:val="TableStyle"/>
        </w:tblPr>
        <w:tblGrid>
            <w:gridCol w:w="100" />
            <w:gridCol w:w="200" />
            <w:gridCol w:w="1024" />
        </w:tblGrid>
        <w:tr>
            <w:tc><w:p><w:r><w:t>ID</w:t></w:r></w:p></w:tc>
            <w:tc><w:p><w:r><w:t>Description</w:t></w:r></w:p></w:tc>   
        </w:tr> 
        <xsl:apply-templates select="items/item">
            <xsl:sort select="id" order="ascending"/>
        </xsl:apply-templates>
    </w:tbl>

</xsl:template>

<xsl:template match="items/item">
    <w:tr>
        <w:tc><w:p><w:r><w:t>
            <xsl:value-of select="id"/>
        </w:t></w:r></w:p></w:tc>
        <w:tc><w:p><w:r><w:t>
            <xsl:value-of select="description"/>
        </w:t></w:r></w:p></w:tc>
    </w:tr>
</xsl:template>
</xsl:stylesheet>

Word の INCLUDETEXT フィールドを使用して XML ファイルをインポートし、変換を適用します。

{INCLUDETEXT  "E:\\Spinner\\Documents\\data.xml" \t "E:\\Spinner\\Documents\\stylesheet.xsl"}

実際にデータをインポートする限り、これは問題なく機能します。必要なデータを含む基本的なテーブルが表示されます。

ただし、表には書式設定がありません。境界線、グリッド線、陰影などはありません。Word は、XSL ファイル (<w:gridCol w:w="100" />など) で指定した列幅を無視し、独自に設定します。

「Table Grid」や「Medium Shading 1 - Accent 3」など、ドキュメントに既に存在するスタイルを (できれば) 使用して書式を設定する必要があります。<w:tblStyle w:val="TableGrid"/>ただし、Word ドキュメントに既に存在するスタイル ( ) または新しく定義された XSL ファイル スタイル ( )に対して、Word に実際にスタイルを適用させることはできません<w:tblStyle w:val="TableStyle"/>

誰かポインタはありますか?

4

1 に答える 1

3

このようなものがもっと必要ですが、列幅を調整する必要があると思います(おそらくセルの境界を考慮するため)

<?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="list">
  <xsl:processing-instruction name="mso-application">
  <xsl:text>progid="Word.Document"</xsl:text>
  </xsl:processing-instruction>
  <w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"  w:macrosPresent="no" w:embeddedObjPresent="no" 
w:ocxPresent="no" xml:space="preserve">
    <w:styles>
      <w:style w:type="table" w:styleId="TableStyle">
        <w:name w:val="Table Style"/>
        <w:tblPr>
          <w:tblBorders>
             <w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/>
          </w:tblBorders>
        </w:tblPr>
      </w:style>
    </w:styles>
    <w:body>
      <w:tbl>
        <w:tblPr>
            <w:tblStyle w:val="TableStyle"/>
        </w:tblPr>
        <w:tblGrid>
            <w:gridCol w:w="100" />
            <w:gridCol w:w="1024" />
        </w:tblGrid>
        <w:tr>
            <w:tc><w:p><w:r><w:t>ID</w:t></w:r></w:p></w:tc>
            <w:tc><w:p><w:r><w:t>Description</w:t></w:r></w:p></w:tc>   
        </w:tr> 
        <xsl:apply-templates select="items/item">
            <xsl:sort select="id" order="ascending"/>
        </xsl:apply-templates>
      </w:tbl>
    </w:body>
  </w:wordDocument>
</xsl:template>

<xsl:template match="items/item">
    <w:tr>
        <w:tc><w:p><w:r><w:t>
            <xsl:value-of select="id"/>
        </w:t></w:r></w:p></w:tc>
        <w:tc><w:p><w:r><w:t>
            <xsl:value-of select="description"/>
        </w:t></w:r></w:p></w:tc>
    </w:tr>
</xsl:template>
</xsl:stylesheet>
于 2012-08-14T17:36:34.790 に答える