在 OpenOffice 中将 Calc(Excel) 数据转换为 XML

问题描述 投票:0回答:4

我需要将 OpenOffice Excel 数据转换为 XML。我的机器上有 Apache OpenOffice 4.1.1(不是 MS-Office)。

样本数据。 (第一行是标签)

CustData FirstName  MiddleName  LastName   EMail             PhoneNumber
           abe       x          Park      [email protected]       2323232323
           poppy     y          Kaith     [email protected]     2323232323

需要结果为:

<CustData>
        <FirstName>abe</FirstName>  
        <MiddleName>x</MiddleName>
        <LastName>Park</LastName>   
        <EMail>[email protected]</EMail>             
        <PhoneNumber>2323232323</PhoneNumber>
</CustData>
<CustData>
       <FirstName>poppy</FirstName>  
       <MiddleName>y</MiddleName>
       <LastName>Kaith</LastName>   
        <EMail>[email protected] </EMail>             
        <PhoneNumber>2323232323</PhoneNumber>
</CustData>
xml openoffice-calc
4个回答
6
投票

Openoffice 和 Libreoffice Calc 能够通过

XSLT
Export Filters
转换其 XML。要使用示例数据执行此操作,请执行以下操作:

首先创建以下XSL文件并将其另存为

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>

现在打开 Calc 并选择

Tools
-
XML Filter Settings
:

enter image description here

选择

New
并填充对话框
General
:

enter image description here

在寄存器中

Transformation
选择
SampleDataExportFilter.xsl
XSLT for export

enter image description here

使用

OK
确认,并使用
Close
确认 XML 过滤器设置。

现在创建以下 Calc 文件:

enter image description here

使用

File
-
Export
,您现在应该能够使用
File type
CustData (.xml)
将电子表格数据导出为 XML。


1
投票

您可以下载OpenXmlSDK Open XML SDK 它包含生产力工具,可以帮助您发现Excel文档的结构,例如: enter image description here

然后使用任何可用的 XSLT 教程来了解将一种 XML 结构转换为另一种结构的方法

教程1

Java教程

这也可能对你有帮助:

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


0
投票

很棒的提示! 在 Calc 6.0.7.3 中发现一个错误,如果两个连续单元格的值相同,它将跳过第二个单元格的值并使用下一个单元格的值! 该行的所有后续值也会移动一列,并且在行内累积。 如果我重新组织列,以便并排列中没有相同的字段值,并更改 .xsl 中的单元格引用 # 进行匹配,则效果很好。


0
投票

这不是 LibreOffice 中的错误 - 这是示例 XSLT 中的错误 stackoverflow.com/a/32829668/1397376(这很棒而且简单,只是 不覆盖 ODF 极端情况,例如为重复单元生成的 )。

迈克,您能帮忙解决这个问题或指导在这种情况下在哪里进行挖掘吗?使用此示例导出 XSLT 时,我有相同的“非错误”行为,提供了错误的单元格索引。 (由于缺乏声誉,我无法直接回复您的帖子)。

提前谢谢您!

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.