Click here to Skip to main content
Click here to Skip to main content

XSLT to transform Excel XML spreadsheet to CSV or HTML table

By , 15 Jul 2003
 

Introduction

One day, I needed to create a conversion of MS Excel saved XML spreadsheet to CSV file. I believe the MS Office Web Component (OWC) ver. 10 spreadsheet component can also expose XML data.

Practical use

Here is an example web page showing how to use OWC spreadsheet with it.

The Script

<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>

The Body

<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>

How to Use it

Simply unzip and look at book.xml.

ASP.NET

Here is a piece of code I use to transform Excel XML to 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()

Reference

-- May the code be with you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

jemodurn
Web Developer
United States United States
Member
Frank is a Sr. UI Software Engineer specialized in MFC and Business Applications.
 
He has worked in BROADBASE, KANA, Macromeda and Siebel Systems.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralTransformation Not Always CorrectmemberCyberKnet28 Mar '05 - 6:45 
This transformation does not work in Office XP's Excel workbooks exported to XML in the case that you have empty cells in between your data cells. In that case data in columns after the empty cells will be shifted forward the number of columns that were empty.
 
Example XML data follows:
 
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlnsBlush | :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">
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
</ExcelWorkbook>
<Worksheet ss:Name="Empty_Cells">
   <Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="3" x:FullColumns="1"
   x:FullRows="1">
   <Row>
      <Cell><Data ss:Type="Number">1</Data></Cell>
      <Cell><Data ss:Type="Number">2</Data></Cell>
      <Cell><Data ss:Type="Number">3</Data></Cell>
   </Row>
   <Row>
      <Cell><Data ss:Type="Number">1</Data></Cell>
      <Cell ss:Index="3"><Data ss:Type="Number">3</Data></Cell>
   </Row>
   <Row>
      <Cell ss:Index="2"><Data ss:Type="Number">2</Data></Cell>
      <Cell><Data ss:Type="Number">3</Data></Cell>
   </Row>
   </Table>
</Worksheet>
</Workbook>
 
I haven't looked much into a solution, I'm not up to date with XML/XSLT/XPath... just found this article through Google and noticed that it did not work for me.
 
Cheers,
 
CyberKnet
 
Video meliora, proboque; Deteriora sequor
Generalxml to csvsussPablo Nuñez15 Dec '03 - 5:51 
I need to convert a ADO-XML into an Excel-XML, has you some xsl to do it¿?
 
Tanks, Regards
GeneralNot a lot of credibilitymemberDirk Vandenheuvel16 Jul '03 - 22:55 
You know writing MS with a $ sign isn't really a very mature sign and lacks credibility. This is not a fanboy website and you should try to show some maturity, especially since you are submitting articles to a very professional audience.
GeneralRe: Not a lot of credibilitymemberJohn M. Drescher17 Jul '03 - 3:38 
I don't mind it and I don't believe it shows a lack of maturity or anything like that.

 
John
GeneralRe: Not a lot of credibilitymemberfolderMonkey17 Jul '03 - 6:13 
You read his artical, might as well use his code or idea. Then you kind flame him for credibility. For Credibility try search the same info on MSDN. Dead | X|
GeneralRe: Not a lot of credibilitymemberjemodurn17 Jul '03 - 6:30 
Sorry guys.. I never thought this artical scroe No.1 on Yahoo if you search "xslt excel xml csv" I will make it better in the next few days.
 
And for the offended. I should have put "#define M$ Microsoft" on the first line.Blush | :O
GeneralRe: Not a lot of credibilitymemberJohn M. Drescher17 Jul '03 - 10:54 
jemodurn wrote:
I should have put "#define M$ Microsoft"
 
Laugh | :laugh:
 
John
GeneralRe: Not a lot of credibilitymemberzakriah12 Mar '07 - 8:39 
I don't think that's being very fair. It's just a simple example which true programmers wouldn't just cut and paste but get an idea of what needs to be done.
QuestionCan not download the source zip filememberChemutury16 Jul '03 - 8:26 
Looks like the jemodurn forgot to put the source file.
Jemodurn so many are waiting for your source zip file.
Poke tongue | ;-P
AnswerRe: Can not download the source zip filememberjemodurn16 Jul '03 - 15:33 
okay.. I fix it..

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 16 Jul 2003
Article Copyright 2003 by jemodurn
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid