export-contacts-xls-example.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  5. * Copyright (C) 2009-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file scripts/company/export-contacts-xls-example.php
  22. * \ingroup company
  23. * \brief Script file to export contacts into an Excel file
  24. */
  25. $sapi_type = php_sapi_name();
  26. $script_file = basename(__FILE__);
  27. $path=dirname(__FILE__).'/';
  28. // Test if batch mode
  29. if (substr($sapi_type, 0, 3) == 'cgi') {
  30. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  31. exit(-1);
  32. }
  33. if (! isset($argv[1]) || ! $argv[1]) {
  34. print "Usage: $script_file now\n";
  35. exit(-1);
  36. }
  37. $now=$argv[1];
  38. require_once $path."../../htdocs/master.inc.php";
  39. //require_once PHP_WRITEEXCEL_PATH."/class.writeexcel_workbook.inc.php";
  40. //require_once PHP_WRITEEXCEL_PATH."/class.writeexcel_worksheet.inc.php";
  41. require_once PHPEXCEL_PATH."/PHPExcel.php";
  42. //require_once PHPEXCEL_PATH."/PHPExcel/Writer/Excel2007.php";
  43. require_once PHPEXCEL_PATH."/PHPExcel/Writer/Excel5.php";
  44. // Global variables
  45. $version=DOL_VERSION;
  46. $error=0;
  47. /*
  48. * Main
  49. */
  50. @set_time_limit(0);
  51. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  52. dol_syslog($script_file." launched with arg ".join(',',$argv));
  53. $fname = DOL_DATA_ROOT.'/export-contacts.xls';
  54. //$objPHPExcel = new writeexcel_workbook($fname);
  55. $objPHPExcel = new PHPExcel();
  56. $objPHPExcel->getProperties()->setCreator("Dolibarr script");
  57. $objPHPExcel->getProperties()->setLastModifiedBy("Dolibarr script");
  58. $objPHPExcel->getProperties()->setTitle("Test Document");
  59. $objPHPExcel->getProperties()->setSubject("Test Document");
  60. $objPHPExcel->getProperties()->setDescription("Test document, generated using PHP classes.");
  61. //$page = &$objPHPExcel->addworksheet('Export Dolibarr');
  62. $objPHPExcel->setActiveSheetIndex(0);
  63. $objPHPExcel->getActiveSheet()->setTitle('Contacts');
  64. //$page->set_column(0,4,18); // A
  65. $sql = "SELECT distinct c.lastname, c.firstname, c.email, s.nom as name";
  66. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  67. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on s.rowid = c.fk_soc";
  68. $resql=$db->query($sql);
  69. if ($resql)
  70. {
  71. $num = $db->num_rows($resql);
  72. print "Lines ".$num."\n";
  73. $i = 0;
  74. $j = 1;
  75. $objPHPExcel->getActiveSheet()->SetCellValue('A1', $langs->trans("Firstname"));
  76. $objPHPExcel->getActiveSheet()->SetCellValue('B1', $langs->trans("Lastname"));
  77. $objPHPExcel->getActiveSheet()->SetCellValue('C1', $langs->trans("Email"));
  78. $objPHPExcel->getActiveSheet()->SetCellValue('D1', $langs->trans("ThirdPart"));
  79. while ($i < $num)
  80. {
  81. $obj = $db->fetch_object($resql);
  82. $objPHPExcel->getActiveSheet()->SetCellValue('A'.($i+2), $obj->firstname);
  83. $objPHPExcel->getActiveSheet()->SetCellValue('B'.($i+2), $obj->lastname);
  84. $objPHPExcel->getActiveSheet()->SetCellValue('C'.($i+2), $obj->email);
  85. $objPHPExcel->getActiveSheet()->SetCellValue('D'.($i+2), $obj->name);
  86. $j++;
  87. $i++;
  88. }
  89. }
  90. //$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  91. $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
  92. $objWriter->save($fname);
  93. //$objPHPExcel->close();
  94. print 'File '.$fname.' was generated.'."\n";
  95. exit(0);