65.9K
CodeProject 正在变化。 阅读更多。
Home

XSLT 将 Excel XML 电子表格转换为 CSV 或 HTML 表

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.72/5 (15投票s)

2003 年 7 月 7 日

viewsIcon

173070

downloadIcon

1843

使用 XSLT 将 Excel XML 电子表格转换为 CSV 或 HTML 表格。

引言

有一天,我需要创建一个将 MS Excel 保存的 XML 电子表格转换为 CSV 文件的转换。 我相信 MS Office Web Component (OWC) ver. 10 电子表格组件也可以暴露 XML 数据。

实际应用

这是一个示例网页,展示了如何使用 OWC 电子表格。

脚本

<script language="JavaScript">

    function action()
    {
        //get the xmldata
        var strXML = Spreadsheet1.XMLData
    
        // create xml object
        var xml = new ActiveXObject("Microsoft.XMLDOM")
        xml.async = false
        xml.load(strXML)

        // Load XSL
        var xsl = new ActiveXObject("Microsoft.XMLDOM")
        xsl.async = false
        xsl.load("book.xsl")

        // Transform
        form1.csvValue.value = xml.transformNode(xsl)
    
        // csv submit to backend 
        // alert(form1.xmlValue.value) 
        form1.submit()

    }
</script>

主体

<html><BODY>
  <h3> Payroll Input Form </h3>
  <P>
   <OBJECT id="Spreadsheet1" style="WIDTH: 100%; HEIGHT: 80%" 
           classid="clsid:0002E551-0000-0000-C000-000000000046" >
    <PARAM NAME="DataType" VALUE="XMLDATA">
    <PARAM NAME="xmlURL" VALUE="book_no_xsl.xml">
   </OBJECT>
  </P>
  <form id="form1" method="post" runat="server">
   <P>
    <input type="button" onclick="action()">
   </P>
   <input id="csvValue" type="hidden" name="csvValue">
  </form>
</BODY></html>

如何使用它

只需解压缩并查看 book.xml

ASP.NET

这是我用来将 Excel XML 转换为 CSV 的一段代码。

'Create a new XslTransform object.
Dim xslt As New XslTransform
'Load the stylesheet.
xslt.Load(Server.MapPath(".") & "excel2csv.xsl")

Dim doc As New XmlDocument
'xmldata is string, use doc.Load(fileName) for file.
doc.LoadXml(xmlData)

'Create an XmlTextWriter which outputs to a file.
Dim fileName As String
fileName = Server.MapPath(".") & "book.csv"

Dim writer As XmlWriter = New XmlTextWriter(fileName, Nothing)
'Transform the data and send the output to the console.

xslt.Transform(doc, Nothing, writer, Nothing)
writer.Close()

参考

-- 愿代码与你同在。

© . All rights reserved.