如何使用xslt将Excel XML转换为HTML?

时间:2013-09-27 07:07:52

标签: html xml excel xslt

我有Excel XML格式的xls文件(http://www.microsoft.com/en-us/download/confirmation.aspx?id=101),如何使用xslt转换为HTML或如何生成xslt-file到Excel?

2 个答案:

答案 0 :(得分: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();
    }

}

最好的记录, 彼得

答案 1 :(得分: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>