Calc_Reader.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Utility class to access OpenDocument spreadsheets read-only
  4. *
  5. * Requires PHP 5.2
  6. *
  7. * @version $Id: Calc_Reader.php,v 1.3 2007-08-06 16:32:48 marand Exp $
  8. * @license CeCILL 2.0
  9. * @copyright 2005-2006 Ouest Systemes Informatiques
  10. */
  11. $erCalc_Reader = error_reporting(E_ALL | E_STRICT);
  12. /**
  13. * Utility class to access OpenDocument spreadsheets read-only
  14. */
  15. class Calc_Reader
  16. {
  17. const NS_TABLE = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
  18. const NS_OFFICE = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
  19. /**
  20. * @var DOMDocument $xml
  21. */
  22. public $xml;
  23. /**
  24. * @var DOMXPath $xpath
  25. */
  26. public $xpath;
  27. function __construct($path)
  28. {
  29. $this->xml = new DOMDocument();
  30. $this->xml->preserveWhiteSpace = FALSE;
  31. $this->xml->load($path);
  32. $this->xpath = new DOMXPath($this->xml);
  33. $this->xpath->registerNameSpace('office', Calc_Reader::NS_OFFICE);
  34. $this->xpath->registerNameSpace('table', Calc_Reader::NS_TABLE);
  35. }
  36. /**
  37. * Return the rows in the spreadsheet as a DOMNodeList
  38. * WARNING: this means ALL the rows, in all the sheets, not just the first sheet
  39. *
  40. * @param string $namedRange Optional name of range, notably Sheet name
  41. * @return DOMNodeList
  42. */
  43. function getRows ($namedRange = NULL)
  44. {
  45. $query = empty($namedRange)
  46. ? '//table:table-row'
  47. : '//table:table[@table:name="' . $namedRange . '"]/table:table-row';
  48. $ret = $this->xpath->query($query);
  49. unset($query);
  50. return $ret;
  51. }
  52. }
  53. error_reporting($erCalc_Reader);
  54. unset ($erCalc_Reader);