用XML編寫EXCEL檔案,XML的寫法注意事項,可以C#+xslt匯出Excel

weixin_34119545發表於2012-09-01

1,檔案資訊:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>

2,正文部分應包含在<Workbook></Workbook>之間:

  <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
     xmlns:v="urn:schemas-microsoft-com:vml"
     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">
……………………
</Workbook>

3, 文件屬性選項應包含在Workbook標識中,並處於Worksheet範圍之外:

      <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
        <Author>DuDu</Author>
        <LastAuthor>DuDu</LastAuthor>
        <LastPrinted>2012-08-31T06:18:54Z</LastPrinted>
        <Created>2012-08-31T06:21:45Z</Created> 
        <LastSaved>2012-08-31T06:18:51Z</LastSaved>
        <Version>14.00</Version>
      </DocumentProperties>

4,ExcelWorkbook 記錄了一些環境變數:

      <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
        <WindowHeight>7830</WindowHeight>
        <WindowWidth>19395</WindowWidth>
        <WindowTopX>600</WindowTopX>
        <WindowTopY>90</WindowTopY>
        <ProtectStructure>False</ProtectStructure>
        <ProtectWindows>False</ProtectWindows>
      </ExcelWorkbook>


5,在Worksheet之外可以定義多個Style,所有Style應位於<Styles></Styles>之間,這些Style在書寫各單元格的時候會用到:

      <Styles>
        <Style ss:ID="Default" ss:Name="Normal">
          <Alignment ss:Vertical="Center"/>
          <Borders/>
          <Font ss:FontName="宋體" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
          <Interior/>
          <NumberFormat/>
          <Protection/>
        </Style>
        <Style ss:ID="m83836928">
          <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
          <Borders>
            <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
             ss:Color="#000000"/>
            <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
             ss:Color="#000000"/>
            <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
             ss:Color="#000000"/>
            <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
             ss:Color="#000000"/>
          </Borders>
          <Font ss:FontName="宋體" x:CharSet="134" ss:Color="#000000"/>
        </Style>
      </Styles>

6,Worksheet標識之間定義了一個工作表,一個檔案可以定義多個Worksheet:

<Worksheet>
        <xsl:attribute name="ss:Name">出庫單<xsl:value-of select="Delivery/erpOutNo"/></xsl:attribute>
        <Table ss:ExpandedColumnCount="8" x:FullColumns="1"
         x:FullRows="1" ss:StyleID="s15" ss:DefaultColumnWidth="54"
         ss:DefaultRowHeight="13.5">
…………………………
</Worksheet>

 

7,Worksheet之內有個Tables,定義了表的內容:

<Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"   x:FullRows="1">

……

</Table>

注意:注意每行的列數和總的行數應該和<Table>中定義的一致,不然Excel匯出後會報錯“檔案損壞” 


8,每一個<Row></Row>之間定義的就是表格一行的內容,每個<Cell></Cell>則對應一個單元格,注意每行的列數和總的行數應該和<Table>中定義的一致:

   <Row ss:AutoFitHeight="0" ss:Height="63.75">
    <Cell ss:StyleID="s63"><Data ss:Type="String">A1</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">B1</Data></Cell>
   </Row>

 

10,Table定義完之後,還可以用<>定義一些設定:

        <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
          <PageSetup>
            <Header x:Margin="0.19"/>
            <Footer x:Margin="0.51"/>
            <PageMargins x:Bottom="0.51" x:Left="0.17" x:Right="0.17"
             x:Top="0.28000000000000003"/>
          </PageSetup>
          <Print>
            <ValidPrinterInfo/>
            <PaperSizeIndex>256</PaperSizeIndex>
            <VerticalResolution>0</VerticalResolution>
          </Print>
          <Selected/>
          <DoNotDisplayGridlines/>
          <Panes>
            <Pane>
              <Number>3</Number>
              <ActiveRow>8</ActiveRow>
              <ActiveCol>2</ActiveCol>
            </Pane>
          </Panes>
          <ProtectObjects>False</ProtectObjects>
          <ProtectScenarios>False</ProtectScenarios>
        </WorksheetOptions>

相關文章