【问题标题】:.net XSLT to create worksheets.net XSLT 创建工作表
【发布时间】:2011-10-09 12:55:04
【问题描述】:

我正在尝试将大量数据导出到 Excel。
我需要能够在不同工作表之间分离数据。
我不想依赖像this 这样的包,所以我想到了XSLT。

有人知道如何使用 XSLT 创建工作表吗?

【问题讨论】:

    标签: .net xml excel xslt


    【解决方案1】:

    可以为此使用 XSLT,但您必须以 Microsoft Excel XML 格式输出数据,从 Microsoft Excel 2002 版本开始支持该格式。

    Microsoft XML Spreadsheet Reference

    您还可以在 Wikipedia 上找到有用的示例

    Microsoft Office XML Formats

    XSLT 的编码方式通常取决于您希望转换的 XML 的结构。这是一个非常简单的示例,它转换为一张纸:

    <data>
       <row>
          <cell>1.1</cell><cell>12</cell>
       </row>
       <row>
          <cell>2.1</cell><cell>2.2</cell>
       </row>
    </data>
    

    然后,使用以下 XSLT 将其转换为 Excel XML

    <xsl:stylesheet version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
       <xsl:template match="/data">
          <xsl:processing-instruction name="mso-application">
             <xsl:text>progid="Excel.Sheet"</xsl:text>
          </xsl:processing-instruction>
    
          <Workbook 
             xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
             xmlns:o="urn:schemas-microsoft-com:office:office" 
             xmlns:x="urn:schemas-microsoft-com:office:excel" 
             xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
             xmlns:html="http://www.w3.org/TR/REC-html40">
             <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
                <Author>Bob</Author>
                <Created>2001-01-01T12:00:00Z</Created>
                <Version>1.0</Version>
             </DocumentProperties>
             <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
                <WindowHeight>8835</WindowHeight>
                <WindowWidth>11340</WindowWidth>
                <WindowTopX>480</WindowTopX>
                <WindowTopY>120</WindowTopY>
                <ProtectStructure>False</ProtectStructure>
                <ProtectWindows>False</ProtectWindows>
             </ExcelWorkbook>
             <Styles>
                <Style ss:ID="Default" ss:Name="Normal">
                   <Alignment ss:Vertical="Bottom"/>
                   <Borders/>
                   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11"/>
                   <Interior/>
                   <NumberFormat ss:Format="#,##0.00"/>
                   <Protection/>
                </Style>
             </Styles>
             <Worksheet ss:Name="Sheet1">
                <ss:Table x:FullColumns="1" x:FullRows="1">
                   <xsl:attribute name="ss:ExpandedColumnCount">
                      <xsl:value-of select="count(row[1]/cell)"/>
                   </xsl:attribute>
                   <xsl:attribute name="ss:ExpandedRowCount">
                      <xsl:value-of select="count(row)"/>
                   </xsl:attribute>
                   <xsl:apply-templates select="row"/>
                </ss:Table>
                <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
                   <PageSetup>
                      <Header x:Margin="0.3"/>
                      <Footer x:Margin="0.3"/>
                      <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
                   </PageSetup>
                   <Print>
                      <ValidPrinterInfo/>
                      <PaperSizeIndex>9</PaperSizeIndex>
                      <HorizontalResolution>600</HorizontalResolution>
                      <VerticalResolution>0</VerticalResolution>
                   </Print>
                   <Selected/>
                   <ProtectObjects>False</ProtectObjects>
                   <ProtectScenarios>False</ProtectScenarios>
                </WorksheetOptions>
             </Worksheet>
          </Workbook>
       </xsl:template>
    
       <xsl:template match="row" xmlns="urn:schemas-microsoft-com:office:spreadsheet">
          <Row>
             <xsl:apply-templates select="cell"/>
          </Row>
       </xsl:template>
    
       <xsl:template match="cell" xmlns="urn:schemas-microsoft-com:office:spreadsheet">
          <Cell>
             <Data>
                <xsl:choose>
                   <xsl:when test="number(.) =.">
                      <xsl:attribute name="ss:Type">Number</xsl:attribute>
                   </xsl:when>
                   <xsl:otherwise>
                      <xsl:attribute name="ss:Type">String</xsl:attribute>
                   </xsl:otherwise>
                </xsl:choose>
                <xsl:value-of select="."/>
             </Data>
          </Cell>
       </xsl:template>
    </xsl:stylesheet>
    

    虽然此示例仅创建一个工作表,但您应该能够看到创建多个工作表是多么简单。只需为您需要的文档中的每个工作表输出另一个 Worksheet 元素。

    在 Microsoft Excel 中手动创建电子表格会很有用,然后另存为 Mircosoft Excel XML 格式,然后在记事本中查看它创建的 XML。这对于查看它如何处理格式或列宽等操作很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 2022-08-18
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多