我有一张xml - Excel电子表格 - 如下:
<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel">
<Styles>
<Style ss:ID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Alignment ss:Horizontal="Right" />
<Font ss:FontName="Aharoni" ss:Size="11" ss:Bold="1" />
</Style>
<Style ss:ID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-Number-False-Right">
<Alignment ss:Horizontal="Right" />
<Font ss:FontName="Aharoni" ss:Size="11" ss:Bold="1" />
<NumberFormat ss:Format="#,##0.00" />
</Style>
<Style ss:ID="Style-[FontFamily: Name=Aharoni]-11-True-False-RedAndGreenStyle-Number-False-Right">
<Alignment ss:Horizontal="Right" />
<Font ss:FontName="Aharoni" ss:Size="11" ss:Bold="1" />
<NumberFormat ss:Format="[Color 10][>0]#,##0.00;[Black][=0]#,##0.00;[Red]#,##0.00" />
</Style>
<Style ss:ID="Style-[FontFamily: Name=Aharoni]-11-True-False-GreenStyle-Number-False-Right">
<Alignment ss:Horizontal="Right" />
<Font ss:FontName="Aharoni" ss:Size="11" ss:Bold="1" ss:Color="#00B000" />
<NumberFormat ss:Format="#,##0.00" />
</Style>
<Style ss:ID="Style-[FontFamily: Name=Aharoni]-11-True-False-RedStyle-Number-False-Right">
<Alignment ss:Horizontal="Right" />
<Font ss:FontName="Aharoni" ss:Size="11" ss:Bold="1" ss:Color="#FF0000" />
<NumberFormat ss:Format="#,##0.00" />
</Style>
</Styles>
<Worksheet ss:Name="PAPERS" ss:RightToLeft="1">
<Table>
<Column ss:Width="87.49609375" />
<Column ss:Width="50" />
<Column ss:Width="92.04541015625" />
<Column ss:Width="94.19384765625" />
<Column ss:Width="52.18115234375" />
<Column ss:Width="50" />
<Column ss:Width="53.09423828125" />
<Column ss:Width="141.99658203125" />
<Column ss:Width="55.24267578125" />
<Row>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">COL1</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">COL2</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">COL3</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">COL4</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">COL5</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">COL6</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">COL7</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">COL8</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">COL9</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
</Row>
<Row>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">CELL1</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-Number-False-Right">
<Data ss:Type="Number">3040</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-RedAndGreenStyle-Number-False-Right">
<Data ss:Type="Number">-0.750897812602024</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">CELL4</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-Number-False-Right">
<Data ss:Type="Number">3063</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">CELL6</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">CELL7</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String">CELL8</Data>
<NamedCell ss:Name="Print_Titles" />
</Cell>
<Cell ss:StyleID="Style-[FontFamily: Name=Aharoni]-11-True-False-NoneColor-String-False-Right">
<Data ss:Type="String" />
<NamedCell ss:Name="Print_Titles" />
</Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Panes />
<DisplayRightToLeft>True</DisplayRightToLeft>
<PageSetup>
<Layout Orientation="Portrait" />
</PageSetup>
</WorksheetOptions>
<Names>
<NamedRange ss:Name="Print_Titles" ss:RefersTo="=PAPERS!R1" />
</Names>
</Worksheet>
</Workbook>
我想通过使用xslt变换将其转换为An html,我不知道如何将其转换为设计它的样式,以及如何设置列的宽度,并通过worksheetoption设置任何单元格的方向?
我的第一个xslt(我知道它不好......):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:template match="Workbook/Worksheet/Table">
<HTML>
<BODY>
<TABLE border='1' style='table-layout:fixed' width='600'>
<xsl:for-each select="Row">
<TR>
<xsl:for-each select="Cell">
<TD><xsl:value-of select='Data'/></TD>
</xsl:for-each>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
首先:将数据存入您的表格。为此,您需要尊重源文档使用的名称空间。尝试:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
exclude-result-prefixes="ss">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<table border='1'>
<xsl:for-each select="ss:Workbook/ss:Worksheet/ss:Table/ss:Row">
<tr>
<xsl:for-each select="ss:Cell">
<td>
<xsl:value-of select='ss:Data'/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
下一步:将列宽添加到表中:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
exclude-result-prefixes="ss">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<table border='1'>
<colgroup>
<xsl:for-each select="ss:Workbook/ss:Worksheet/ss:Table/ss:Column">
<col width="{@ss:Width}"/>
</xsl:for-each>
</colgroup>
<xsl:for-each select="ss:Workbook/ss:Worksheet/ss:Table/ss:Row">
<tr>
<xsl:for-each select="ss:Cell">
<td>
<xsl:value-of select='ss:Data'/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
最后,我们要将每个单元格与其样式相关联并获得对齐:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
exclude-result-prefixes="ss">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="style" match="ss:Style" use="@ss:ID" />
<xsl:template match="/">
<table border='1'>
<colgroup>
<xsl:for-each select="ss:Workbook/ss:Worksheet/ss:Table/ss:Column">
<col width="{@ss:Width}"/>
</xsl:for-each>
</colgroup>
<xsl:for-each select="ss:Workbook/ss:Worksheet/ss:Table/ss:Row">
<tr>
<xsl:for-each select="ss:Cell">
<td align="{key('style', @ss:StyleID)/ss:Alignment/@ss:Horizontal}">
<xsl:value-of select='ss:Data'/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
通过这种方式,您可以获取更多样式属性并使用它们。或者,您可以生成内部CSS样式表并将表元素指向它。
另请注意,您可以从Excel导出HTML文档并节省大量工作。