Calc_Reader.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.4 2007-09-02 20:50:12 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 a DOMNodeList of the worksheets available in the document
  38. *
  39. * @return DOMNodeList
  40. */
  41. function getCalcSheets()
  42. {
  43. $sq = '//table:table';
  44. $list = $this->xpath->query($sq);
  45. unset($sq);
  46. return $list;
  47. }
  48. /**
  49. * Return the names of the worksheets available in the document
  50. *
  51. * @return array
  52. */
  53. function getCalcSheetNames()
  54. {
  55. $dnlSheets = $this->getCalcSheets();
  56. $ret = array();
  57. foreach ($dnlSheets as $node)
  58. {
  59. $ret[] = $node->getAttributeNS(Calc_Reader::NS_TABLE, 'name');
  60. }
  61. unset($dnlSheets);
  62. return $ret;
  63. }
  64. /**
  65. * Return the rows in the spreadsheet as a DOMNodeList
  66. * WARNING: this means ALL the rows, in all the sheets, not just the first sheet
  67. *
  68. * @param string $namedRange Optional name of range, notably Sheet name
  69. * @return DOMNodeList
  70. */
  71. function getRows ($namedRange = NULL)
  72. {
  73. $query = empty($namedRange)
  74. ? '//table:table-row'
  75. : '//table:table[@table:name="' . $namedRange . '"]/table:table-row';
  76. $ret = $this->xpath->query($query);
  77. unset($query);
  78. return $ret;
  79. }
  80. }
  81. error_reporting($erCalc_Reader);
  82. unset ($erCalc_Reader);