Convert Calc(Excel) Data in XML in OpenOffice

2019-02-17 16:03发布

问题:

I have a requirement to convert OpenOffice Excel data into XML . I have Apache OpenOffice 4.1.1 in my machine (not MS- Office).

Sample Data. (First Row is of Tags )

CustData FirstName  MiddleName  LastName   EMail             PhoneNumber
           abe       x          Park      abe@mail.com       2323232323
           poppy     y          Kaith     Poppy@mail.com     2323232323

Need Result as :

<CustData>
        <FirstName>abe</FirstName>  
        <MiddleName>x</MiddleName>
        <LastName>Park</LastName>   
        <EMail>abe@mail.com</EMail>             
        <PhoneNumber>2323232323</PhoneNumber>
</CustData>
<CustData>
       <FirstName>poppy</FirstName>  
       <MiddleName>y</MiddleName>
       <LastName>Kaith</LastName>   
        <EMail>Poppy@mail.com </EMail>             
        <PhoneNumber>2323232323</PhoneNumber>
</CustData>

回答1:

Openoffice and Libreoffice Calc is able transforming its XML via XSLT with Export Filters. To do so with your example data, do the following:

At first create the following XSL file and save it as SampleDataExportFilter.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" exclude-result-prefixes="office table text">

 <xsl:template match="/">
  <root>
   <xsl:apply-templates select="/*/office:body" />
  </root>
 </xsl:template>

 <xsl:template match="office:body">
  <xsl:apply-templates />
 </xsl:template>

 <xsl:template match="office:spreadsheet">
  <xsl:apply-templates />
 </xsl:template>

 <xsl:template match="office:spreadsheet/table:table">

   <xsl:for-each select="table:table-row[position() &gt; 1]">

   <CustData>
    <FirstName><xsl:value-of select="table:table-cell[2]/text:p" /></FirstName> 
    <MiddleName><xsl:value-of select="table:table-cell[3]/text:p" /></MiddleName>
    <LastName><xsl:value-of select="table:table-cell[4]/text:p" /></LastName>   
    <EMail><xsl:value-of select="table:table-cell[5]/text:p" /></EMail>            
    <PhoneNumber><xsl:value-of select="table:table-cell[6]/text:p" /></PhoneNumber>
   </CustData>

   </xsl:for-each>

 </xsl:template>
</xsl:stylesheet>

Now open Calc and select Tools - XML Filter Settings:

Select New and fill the dialog General:

In the register Transformation select the SampleDataExportFilter.xsl as XSLT for export:

Confirm with OK and the XML Filter Settings with Close.

Now create the following Calc file:

With File - Export you shold now be able to export the spreadsheet data using the File type CustData (.xml) as XML.



回答2:

You can download OpenXmlSDK Open XML SDK It contains Productivity Tools which could help you to discover structure of excel documents, for example:

And then use any available XSLT tutorial to find out about approaches of conversion one XML structure to another

Tutorial 1

Java tutorial

And also this might help you:

https://github.com/foglcz/xsl-excel-engine