Convert xml file into excel file using php

April 25, 2013

PHP

Here we are describing convert xml file into excel file using php.Simple way to convert xml file into excel file with php.If you have basic knowledge of xml and php(:D) this is very easy.My another post is showing how to read Excel file and save details to database using php. If you not good familiar with xml read this PHP Creating XML file post and Read XML file using PHP post to get and better idea.

Okay.First you need to create you xml file. I’ll show you my xml file format for a example.
Library.xml

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?>
<library>

    <book>
        <title>PHP and MySQL</title>
        <author>Miguel Alvarez</author>
        <publisher>WebHole</publisher>
        <price>10.00</price>
    </book>
    
    <book>
        <title>PHP jQuery Cookbook</title>
        <author>Vijay Joshi</author>
        <publisher>Reilly Media</publisher>
        <price>20.99</price>
    </book>
    
    <book>
        <title>JAVA 123</title>
        <author>WIlliam Vega</author>
        <publisher>WebHole</publisher>
        <price>15.25</price>
    </book>
    
</library>

How to read this xml file?

Select Code
1
2
3
4
5
$url = 'Library.xml'; // xml file location with file name
if (file_exists($url)) {
    $xml = simplexml_load_file($url);
    print_r($xml);
}

How to convert xml file into excel file?

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=spreadsheet.xls" );// put your file name here
$url = 'Library.xml'; // xml file location with file name
if (file_exists($url)) {
    $xml = simplexml_load_file($url);
    
    echo 'Title'."\t" . 'Author'."\t" . 'Publisher'."\t" . 'Price'."\t\n";
    foreach($xml->book as $books)
    {
        echo $books->title."\t" . $books->author."\t" . $books->publisher."\t" . $books->price."\n";
    }
}
?>

That’s only.
Remember this is simple example to show how to convert xml file into excel file using php.
Here is the full source code to download.

[like-to-dl theme="blue" url="http://481f240e.linkbucks.com" like_url="https://www.facebook.com/pages/WebExplorarcom/308199725882057?ref=hl" message="Click Like to Start Download full source code(2 KiB)"]

VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
Convert xml file into excel file using php, 5.0 out of 5 based on 1 rating
, ,

About Sumith Harshan

Sumith Harshan is the Managing Director of the WebXtreams(Pvt) Ltd in Sri lanka.Specializes in Java Script, JQuery, HTML4/5, CSS2/3, PHP, CodeIgniter, CakePHP, J2SE, J2EE, XML, Hacking & Security, Mobile Interface Developing, Magento, Wordpress, Joomla, Drupal and PHPBB.

View all posts by Sumith Harshan
  • Vignesh Waran

    you are awesome …thanks working great, and i need excel to xml using php.thanks

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)