2011-10-09 96 views
2

我想輸出大量的數據到Excel。
我需要在不同的工作表之間分隔數據的可能性。
我不喜歡像this這樣的軟件包,所以我想到了XSLT。.net XSLT創建工作表

有誰知道如何用XSLT創建工作表?

回答

2

爲此可以使用XSLT,但必須以Microsoft Excel XML格式輸出數據,這是Microsoft Excel 2002以後版本支持的格式。

Microsoft XML Spreadsheet Reference

您還可以在維基百科

找到有用的樣品

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> 

雖然這個例子只創建一個工作表,你應該能夠看到創建多個工作表的方式非常簡單。只需爲您需要的文檔中的每張工作表輸出另一個工作表元素。

它可以在Microsoft Excel中手動創建一個電子表格有用的,然後另存爲的Mircosoft Excel XML格式,然後在記事本看看它,看它創建的XML。這可以用於查看它如何執行格式化或列寬等操作。