0

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?

Paul Attuck
  • 2,229
  • 4
  • 23
  • 26
  • 1
    Why don't you want to use an excel reader library to read excel data, yet somehow expect a "magic" parser to exist... what do you think excel reader libraries are? If you want it simple, manually use MS Excel to "save as" CSV, then fgetcsv() in your script – Mark Baker Jan 24 '12 at 11:23
  • i want only get data from excel, not edit etc – Paul Attuck Jan 24 '12 at 11:25
  • Any library that is an "Excel Reader" only gives you the ability to get the data, not to edit.... "Excel Writer" libraries give you the ability to write.... and very few libraries give you both read and write, i.e. edit – Mark Baker Jan 24 '12 at 11:26
  • 1
    http://stackoverflow.com/questions/3930975/alternative-for-php-excel Gives you a list of libraries for reading/writing Excel files... pick a reader from the list and use that – Mark Baker Jan 24 '12 at 11:28
  • @MarkBaker thanks, please add new answer for this question:) – Paul Attuck Jan 24 '12 at 13:08

2 Answers2

1

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

Ronald Borla
  • 586
  • 4
  • 19
  • however, there are desktop applications which can batch convert Excel files to CSV files, just Google it.. btw, it's easier to parse CSV files, like the one in your code – Ronald Borla Jan 24 '12 at 11:39
1

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.

Community
  • 1
  • 1
Mark Baker
  • 209,507
  • 32
  • 346
  • 385