membaca file EXCEL(xml) dengan PHP

pernah mencoba membaca file excel dengan php? kok rasanya sulit ya.

tapi kalau kita mau membaca file excel dengan format xml ternyata ga sulit, mau tau gemana caranya?

buat sebuah file di Ms Excel dan simpan dengan format xml, maka kita akan mendapatakan file yang isinya seperti ini:

disana terdapat beberapa tag, tapi cukup dilihat aja tag “row”, “cell” dan “data”

setelah itu buat sebuah file PHP:

<?php

$xml_value;
$rc=1;
$cc=1;
$doc = new DOMDocument();
$doc->load(‘book1.xml’);

$rows = $doc->getElementsByTagName(‘Row’);
foreach($rows as $row[$rc])
{
$cells = $row[$rc]->getElementsByTagName(‘Cell’);
foreach($cells as $cell[$cc])
{
$data = $cell[$cc]->getElementsByTagName(‘Data);
$value = $data->item(0)->nodeValue;
$xml_value[$rc][$cc] = $value;
$cc ++;
$c_max = $cc;
}
$rc ++;
$cc = 1;
}
?>
<table border=“1″>
<?php

foreach($xml_value as $row)
{
echo “<tr>”;
for($a=1;$a<$c_max;$a++)
{
?>
<td><?php echo $row[$a];?></td>
<?php
}
echo “</tr>”;
}
?>
</table>

ok sekarang coba jalankan script diatas

selamat mencoba….

file penuh bisa diunduh disini

Advertisement

About azis

nothing special, just like u no more View all posts by azis

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.