Paul Attuck
Paul Attuck

Reputation: 2269

Simple parse excel to object or variables, same as with XML or CSV

For CSV is:

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>

For XML:

<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.

if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');

    print_r($xml);
} else {
    exit('Failed to open test.xml.');
}
?>

This is very simple way to parsing. Is possible this also with EXCEL? I dont want use excel readers etc. I would like simple parser, same as for xml or csv. How is the best way for this? Maybe parse excel to csv or xml, but how?

Upvotes: 0

Views: 770

Answers (2)

Mark Baker
Mark Baker

Reputation: 212452

There is no one Excel format... there are two common formats: BIFF .xls files (used up to Excel 2003) and OfficeOpenXML .xlsx files (for both Excel 2007 and Excel 2010), not to mention SpreadsheetML, the rarely-used alternative format for Excel 2003. Nor are these formats simple like CSV or even "vanilla" XML: even the OfficeOpenXML format comprises a whole series of interrelated XML files in a zipped directory hierarchy.

Then, you can also factor in the formatting and other facilities that are pesent in a spreadsheet file (multiple worksheets, hidden/merged columns/rows, styling, images, charts, etc. This also means that it is not a trivial task to parse the files and present them in a simple manner.

There are a number of PHP libraries (and other alternatives) available for accessing spreadsheet data and/or for creating/editing spreadsheet files from within PHP. I'm responsible for the development of one of those libraries (PHPExcel), and have compiled a list of the alternatives in answer to previous questions here on StackOverflow.

If you only want to read spreadsheet data, then select one of the readers from the list: if you want to create new spreadsheets, then pick a writer. If you want to edit existing workbooks, then you really need one of the few libraries/options that supports both reading and writing. And watch out for the supported file formats... not all libraries support both BIFF .xls and .xlsx.

Upvotes: 1

Ronald Borla
Ronald Borla

Reputation: 586

Excel files are not just simple text files.. CSV and XML files are purely text files.. Therefore, you would need a library which can really parse Excel files

Upvotes: 1

Related Questions