.net XSLT pour créer des feuilles de travail

J'essaie d'exporter beaucoup de données pour excel.
J'ai besoin de séparer datatables entre différentes feuilles de calcul.
Je préfère ne pas dépendre de packages comme celui-ci , alors j'ai pensé à XSLT.

Est-ce que quelqu'un sait comment créer des feuilles de travail avec XSLT?

Il est possible d'utiliser XSLT pour cela, mais vous devrez produire datatables au format XML Microsoft Excel, qui est supporté par les versions de Microsoft Excel 2002.

Microsoft XML Spreadsheet Reference

Vous pouvez également find un exemple utile sur Wikipedia

Formats XML Microsoft Office

La façon dont le encoding XSLT dépend généralement de la structure du XML que vous souhaitez transformer. Voici un exemple très simple, qui se transforme en une feuille:

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

Ensuite, utilisez le XSLT suivant pour le transformer en XML Excel

 <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:atsortingbute name="ss:ExpandedColumnCount"> <xsl:value-of select="count(row[1]/cell)"/> </xsl:atsortingbute> <xsl:atsortingbute name="ss:ExpandedRowCount"> <xsl:value-of select="count(row)"/> </xsl:atsortingbute> <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:atsortingbute name="ss:Type">Number</xsl:atsortingbute> </xsl:when> <xsl:otherwise> <xsl:atsortingbute name="ss:Type">Ssortingng</xsl:atsortingbute> </xsl:otherwise> </xsl:choose> <xsl:value-of select="."/> </Data> </Cell> </xsl:template> </xsl:stylesheet> <xsl: sinon <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:atsortingbute name="ss:ExpandedColumnCount"> <xsl:value-of select="count(row[1]/cell)"/> </xsl:atsortingbute> <xsl:atsortingbute name="ss:ExpandedRowCount"> <xsl:value-of select="count(row)"/> </xsl:atsortingbute> <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:atsortingbute name="ss:Type">Number</xsl:atsortingbute> </xsl:when> <xsl:otherwise> <xsl:atsortingbute name="ss:Type">Ssortingng</xsl:atsortingbute> </xsl:otherwise> </xsl:choose> <xsl:value-of select="."/> </Data> </Cell> </xsl:template> </xsl:stylesheet> 

Bien que cet exemple ne crée qu'une seule feuille de calcul, vous devriez voir comment il est direct de créer plusieurs feuilles. Il suffit de produire un autre élément de feuille de calcul pour chaque feuille de travail dans le document dont vous avez besoin.

Il peut être utile de créer une feuille de calcul manuellement dans Microsoft Excel, puis Enregistrer le format XML Excel Mircosoft, puis l'examiner dans le Bloc-notes pour voir le XML qu'il crée. Cela pourrait être utile pour voir comment il fonctionne comme le formatting ou les largeurs des colonnes.