跳轉到內容

Apache Ant/將 Excel 轉換為 XML

來自華夏公益教科書,開放的書籍,為開放的世界

您希望從二進位制 Excel 文件中自動提取格式良好的 XML 檔案。

我們將在構建目標內使用 Java Ant 任務。

輸入檔案

[編輯 | 編輯原始碼]

我們將建立一個示例 Microsoft Excel 檔案,它有兩列,如下所示

Screen image for spreadsheet input
電子表格輸入的螢幕影像

將其儲存到名為“sample.xls”的檔案中。

接下來,下載 Apache Tika jar 檔案並將其放在您的本地硬碟驅動器上。

您可以從以下位置獲取下載:http://tika.apache.org/download.html 主要 Tika jar 檔案大約 27MB。

我將 tika jar 檔案放在 D:\Apps\tika 中,但您可以更改此位置。

建立一個名為“build.xml”的檔案

<project name="extract-xml-from-xsl" default="extract-xml-from-xsl">
    <description>Sample Extract XML from Excel xsl file with Apache Tika</description>
    <property name="lib.dir" value="D:\Apps\tika"/>
    <property name="input-file" value="sample.xls"/>
    
    <target name="extract-xml-from-xsl">
        <echo message="Extracting XML from Excel file: ${input-file}"/>
        <java jar="${lib.dir}/tika-app-1.3.jar" fork="true" failonerror="true"
            maxmemory="128m" input="${input-file}" output="sample.xml">
            <arg value="-x" />
        </java>
    </target> 
</project>

<java> 任務將執行 tika。引數“-x”(表示 XML)將從輸入中提取 XML。

其他命令列選項列在以下位置:http://tika.apache.org/1.3/gettingstarted.html

現在,開啟您的 DOS 或 UNIX shell 並 cd 到包含您的構建檔案的目錄。在命令 shell 中鍵入“ant”。

$ ant
Buildfile: D:\ws\doc-gen\trunk\build\tika\build.xml

extract-xml-from-xsl:
     [echo] Extracting XML from Excel file: sample.xls

BUILD SUCCESSFUL
Total time: 1 second

示例輸出

[編輯 | 編輯原始碼]

請注意,輸出是一個格式良好的 HTML 檔案,其中包含一個表格。

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta name="meta:last-author" content="Dan" />
        <meta name="meta:creation-date" content="2013-03-04T17:20:19Z" />
        <meta name="dcterms:modified" content="2013-03-04T17:22:01Z" />
        <meta name="meta:save-date" content="2013-03-04T17:22:01Z" />
        <meta name="Last-Author" content="Dan" />
        <meta name="Application-Name" content="Microsoft Excel" />
        <meta name="dc:creator" content="Dan" />
        <meta name="Last-Modified" content="2013-03-04T17:22:01Z" />
        <meta name="Author" content="Dan" />
        <meta name="dcterms:created" content="2013-03-04T17:20:19Z" />
        <meta name="date" content="2013-03-04T17:22:01Z" />
        <meta name="modified" content="2013-03-04T17:22:01Z" />
        <meta name="creator" content="Dan" />
        <meta name="Creation-Date" content="2013-03-04T17:20:19Z" />
        <meta name="meta:author" content="Dan" />
        <meta name="extended-properties:Application" content="Microsoft Excel" />
        <meta name="Content-Type" content="application/vnd.ms-excel" />
        <meta name="Last-Save-Date" content="2013-03-04T17:22:01Z" />
        <title></title>
    </head>
    <body>
        <div class="page"><h1>Sheet1</h1>
            <table>
                <tbody>
                    <tr>
                        <td>Name</td>
                        <td>Phone</td>
                    </tr>
                    <tr>
                        <td>Peg</td>
                        <td>123</td>
                    </tr>
                    <tr>
                        <td>Dan</td>
                        <td>456</td>
                    </tr>
                    <tr>
                        <td>John</td>
                        <td>789</td>
                    </tr>
                    <tr>
                        <td>Sue</td>
                        <td>912</td>
                    </tr>
                </tbody>
            </table>
        </div>

</html>
華夏公益教科書