0

我有 Excel XML 格式的 xls 文件(http://www.microsoft.com/en-us/download/confirmation.aspx?id=101)。

如何使用 xslt 转换为 HTML 或如何将 xslt 文件生成到 Excel?

4

2 回答 2

0

您不能使用 XSLT 从 EXCEL 生成 HTML。您必须先将 Excel 数据转换为 XML 格式,然后使用 XSLT 生成 HTML。

要从 EXCEL 获取 XML,您可以使用 Java:http: //jexcelapi.sourceforge.net/ 我相信其他编程语言也可以。

您可以使用以下 java 代码然后将您的 EXCEL 转换为 XML:

public static void convertXLS() {

    File file = new File("C:/tesco_invoices_zwe02.xls");
    Workbook wb;
    try {
        wb = Workbook.getWorkbook(file);
        String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"
                + "<ns0:" + "bla" + " " + "xmlns:ns0=\"" + "bla" + "\">";
        Cell[] cells;
        Cell[] cellNames;
        cellNames = wb.getSheet(0).getRow(0);
        for (int j = 1; j < wb.getSheet(0).getRows(); j++) {
            xmldata = xmldata + "<Recordset>";
            cells = wb.getSheet(0).getRow(j);
            for (int i = 0; i < wb.getSheet(0).getColumns(); i++) {

                String cellcontent = cells[i].getContents();
                String cellcontent2 = cellcontent.trim();
                
                xmldata = xmldata
                        + "<"+ cellNames[i].getContents()+ ">"+ cellcontent2 + "</" + cellNames[i].getContents() + ">" + "\n";
            }
            xmldata = xmldata + "</Recordset>";
            
        }
        
        xmldata = xmldata + "</ns0:" + "bla" + ">";
        wb.close();
        System.out.println(xmldata);
    } catch (BiffException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
于 2013-09-27T07:49:59.603 回答
0

XSLT

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
                version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:element name="style">
    <xsl:for-each select="ss:Workbook/ss:Styles/ss:Style">
    #<xsl:value-of select="@ss:ID"/>
    {
        <xsl:if test="ss:Alignment/@ss:Horizontal">
            text-align: <xsl:value-of select="ss:Alignment/@ss:Horizontal"/>;
        </xsl:if>
        <xsl:if test="ss:Alignment/@ss:Vertical">
            vertical-align: <xsl:value-of select="ss:Alignment/@ss:Vertical"/>;
        </xsl:if>
        <xsl:if test="ss:Alignment/@ss:Vertical">
            vertical-align: <xsl:value-of select="ss:Alignment/@ss:Vertical"/>;
        </xsl:if>
        <xsl:if test="ss:Font/@ss:Bold=1">
            font-weight: bold;
        </xsl:if>
        <xsl:if test="ss:Font/@ss:Color">
            color:<xsl:value-of select="ss:Font/@ss:Color"/>;
        </xsl:if>

        <xsl:if test="ss:Font/@ss:StrikeThrough=1">
            text-decoration:line-through
        </xsl:if>
        <xsl:if test="ss:Font/@ss:Underline='Single'">
            text-decoration:underline
        </xsl:if>
        <xsl:if test="ss:Interior/@ss:Color">
            background-color:<xsl:value-of select="ss:Interior/@ss:Color"/>;
        </xsl:if>
        <xsl:for-each select="ss:Borders/ss:Border">
            <xsl:if test="@ss:Color">
                border-<xsl:value-of select="@ss:Position"/>-color:<xsl:value-of select="@ss:Color"/>;
            </xsl:if>
            <xsl:if test="@ss:Weight">
                border-<xsl:value-of select="@ss:Position"/>-width:<xsl:value-of select="@ss:Weight"/>px;
            </xsl:if>
            <xsl:if test="@ss:LineStyle">
                border-<xsl:value-of select="@ss:Position"/>-style:<xsl:value-of select="@ss:LineStyle"/>;
            </xsl:if>
        </xsl:for-each>
}
    </xsl:for-each>
</xsl:element>
    <xsl:element name="table">
        <xsl:for-each select="ss:Workbook/ss:Worksheet/ss:Table/ss:Row">
            <xsl:element name="tr">

                <xsl:for-each select="ss:Cell">
                    <xsl:element name="td">
                        <xsl:attribute name="id">
                            <xsl:value-of select="@ss:StyleID"/>
                        </xsl:attribute>
                        <xsl:if test="@ss:MergeAcross">
                            <xsl:attribute name="colspan">
                                <xsl:value-of select="@ss:MergeAcross+1"/>
                            </xsl:attribute>
                        </xsl:if>
                        <xsl:value-of select="ss:Data"/>
                    </xsl:element>
                </xsl:for-each>
            </xsl:element>
        </xsl:for-each>
    </xsl:element>
</xsl:template>

于 2013-09-27T18:25:38.693 回答